2 条题解
-
0
给个码风正常点的代码吧。
#include<bits/stdc++.h> using namespace std; #define N 1600 int dx[]={1,1,-1,-1}; int dy[]={1,-1,1,-1}; struct nd{int x,y,d,s;}; int n,ax,ay,bx,by; queue<nd>q;char s[N][N]; bool v[N][N],vis[N][N]; bool check(int x,int y) { return (x>0&&x<=n&&y>0&&y<=n); } int main() { scanf("%d%d%d%d%d",&n,&ax,&ay,&bx,&by); for(int i=1;i<=n;i++) { scanf("%s",s[i]+1); for(int j=1;j<=n;j++) if(s[i][j]=='#')v[i][j]=1; } q.push({ax,ay,-1,0}); while(!q.empty()) { nd i=q.front();q.pop(); int x=i.x,y=i.y,d=i.d,s=i.s+1; for(int t=0;t<4;t++) { if(t==d)continue; for(int k=1;;k++) { int xx=x+k*dx[t],yy=y+k*dy[t]; if(!check(xx,yy)||v[xx][yy])break; if(vis[xx][yy])continue; if(xx==bx&&yy==by){printf("%d\n",s);return 0;} vis[xx][yy]=1;q.push({xx,yy,t,s}); } } } puts("-1");return 0; } -
0
啊,好标准一道宽搜啊
思路
迷宫,固定的移动方式,起点到终点最少步数,一眼宽搜。然后……然后没了。
AC代码
#include<bits/stdc++.h> using namespace std; const int N=1510; struct node{int x,y,t;}; char s[N]; int a[N][N],n,stx,sty,edx,edy; void bfs() { queue<node>Q; Q.push({stx,sty,0}); while(!Q.empty()) { node n1=Q.front();Q.pop(); int x=n1.x,y=n1.y,t=n1.t; int step=0; while(++step)//四个方向的大屎山(复制即可) { int xx=x+step,yy=y+step; if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]==-1)break; if(a[xx][yy])continue; a[xx][yy]=t+1; Q.push({xx,yy,t+1}); } step=0; while(++step) { int xx=x+step,yy=y-step; if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]==-1)break; if(a[xx][yy])continue; a[xx][yy]=t+1; Q.push({xx,yy,t+1}); } step=0; while(++step) { int xx=x-step,yy=y+step; if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]==-1)break; if(a[xx][yy])continue; a[xx][yy]=t+1; Q.push({xx,yy,t+1}); } step=0; while(++step) { int xx=x-step,yy=y-step; if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]==-1)break; if(a[xx][yy])continue; a[xx][yy]=t+1; Q.push({xx,yy,t+1}); } continue; } } int main() { scanf("%d%d%d%d%d",&n,&stx,&sty,&edx,&edy); for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=n;j++)a[i][j]=(s[j]=='.')-1; } bfs(); if(a[edx][edy])printf("%d\n",a[edx][edy]); else puts("-1"); return 0; }
- 1
信息
- ID
- 12442
- 时间
- 6000ms
- 内存
- 2048MiB
- 难度
- 6
- 标签
- 递交数
- 33
- 已通过
- 11
- 上传者