当前位置:网站首页>2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫
2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫
2022-08-03 03:50:00 【WA_自动机】
G - 迷宫
最短路问题,两两建边,如果目标点为花那么边权为 1 − 1 = 0 1-1=0 1−1=0 ,否则为 1 1 1 固定源点与汇点,最短路的各种算法都可以写,这道题用双端队列比较好写,但是我用的是优先队列。
优先队列
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
const int N = 2010;
char g[N][N];
int f[N][N];
int n,m;
int dx[]={
0,1,0,-1};
int dy[]={
-1,0,1,0};
void bfs()
{
memset(f,0x3f,sizeof f);
f[1][1]=0;
priority_queue<PIII,vector<PIII>,greater<PIII> > Q;
Q.push({
0,{
1,1}});
while(Q.size())
{
PIII t=Q.top();Q.pop();
for(int i=0;i<4;i++)
{
int x=t.second.first+dx[i],y=t.second.second+dy[i];
if(x<1||x>n||y<1||y>m) continue;
if(g[x][y]=='#') continue;
if(f[x][y]!=0x3f3f3f3f) continue;
if(g[x][y]=='.') f[x][y]=min(f[x][y],f[t.second.first][t.second.second]+1);
if(g[x][y]=='*') f[x][y]=min(f[x][y],f[t.second.first][t.second.second]);
Q.push({
f[x][y],{
x,y}});
}
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>(g[i]+1);
bfs();
if(f[n][m]>=0x3f3f3f3f/2) cout<<"-1"<<endl;
else cout<<f[n][m]<<endl;
return 0;
}
双端队列
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N = 2010;
char g[N][N];
int f[N][N];
int n,m;
int dx[]={
0,1,0,-1};
int dy[]={
-1,0,1,0};
void bfs()
{
memset(f,0x3f,sizeof f);
f[1][1]=0;
deque<PII> Q;
Q.push_front({
1,1});
while(!Q.empty())
{
PII t=Q.front();Q.pop_front();
for(int i=0;i<4;i++)
{
int tx=t.first+dx[i],ty=t.second+dy[i];
if(tx<1||tx>n||ty<1||ty>m) continue;
if(g[tx][ty]=='#') continue;
int d=(g[tx][ty]=='.');
if(f[tx][ty]>f[t.first][t.second]+d)
{
f[tx][ty]=f[t.first][t.second]+d;
if(d==0) Q.push_front({
tx,ty});
else Q.push_back({
tx,ty});
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>(g[i]+1);
bfs();
if(f[n][m]>=0x3f3f3f3f/2) cout<<"-1"<<endl;
else cout<<f[n][m]<<endl;
return 0;
}
边栏推荐
- 9 椭圆曲线密码体制
- HI3521D 烧录128M nand flash文件系统过程-一定要注意flash的容量
- Chinese valentine's day??To the liver is the way!!!!!Auto. Js special position control method
- 谷粒商城一些疑问总结
- 工程水文学试题库
- DC-6靶场下载及渗透实战详细过程(DC靶场系列)
- Have bosses know date field flinksql is synchronized to the use of the null on how to deal with
- 问下有用sql server flink-sql-connector-sqlserver-cdc-2
- 肖sir__面试接口测试
- Jincang Database Pro*C Migration Guide (3. KingbaseES Pr*oc Compatibility with Oracle Pro*c)
猜你喜欢
随机推荐
阿里面试官:聊聊如何格式化Instant
基于 jetpack compose,使用MVI架构+自定义布局实现的康威生命游戏
DOM破环和两个实验的复现
肖sir__面试接口测试
银微转债,洁特转债上市价格预测
Auto.js Pro 计算脚本运行时间
Guys, I don't understand a bit: why the documentation of oracle-cdc writes that the connector can be done exactly-o
ClickHouse—高级
js的组成及js样式
Auto.js Pro 编写第一个脚本hello world
Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?
自考六级雅思托福备战之路
Auto.js Pro write the first script hello world
肖sir___面试就业课程____app
Summary of some questions about the grain mall
How to write test cases in software testing technology (2)
【剑指offer】——股票的最大利润
工程水文学试题库
AF-DNAT
Mysql如何建立索引实现语句优化