当前位置:网站首页>2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
2022-08-03 04:04:00 【WA_ automata】
G - 迷宫
最短路问题,Built two edge,If the target point for spend so edge right for 1 − 1 = 0 1-1=0 1−1=0 ,否则为 1 1 1 Fixed source point and the sink,The short circuit of various algorithms can write,This problem is written in deque is better,But I was using a priority queue.
优先队列
#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;
}
边栏推荐
猜你喜欢
随机推荐
关于#sql#的问题,如何解决?
【剑指offer】——16.数值的整数次方
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
肖sir___面试就业课程____app
我的“眼睛”就是尺!
HI3521D 烧录128M nand flash文件系统过程-一定要注意flash的容量
【leetcode热题Hot100】——LRU缓存
ORACLE中文乱码
ClickHouse - Getting Started
金仓数据库 Pro*C 迁移指南(3. KingbaseES Pr*oc 对 Oracle Pro*c 的兼容)
Redis-Redisson介绍和用途
Pro * C Jin Cang database migration guide (4) KingbaseES Pro * C migration guide)
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
ESP8266-Arduino编程实例-MAX6675冷端补偿K热电偶数字转换器驱动
ESP8266-Arduino编程实例-MCP3008-ADC转换器驱动
安装ambari
v-text指令:设置标签内容
t条件判断语句与if循环
工程制图-齿轮