题目大意在代码中:
1 /* 2 * @Descripttion: 3 * @version: 4 * @Author: ZKYAAA 5 * @Date: 2020-04-20 20:45:20 6 * @LastEditors: 请叫我ZK谕啊啊啊 7 * @LastEditTime: 2020-04-20 21:05:53 8 */ 9 //一个矩形方格,给出起点和终点,求到达终点的最短路径长度 10 #include <bits/stdc++.h> 11 using namespace std; 12 const int MAXN=100; 13 int a[MAXN][MAXN]; 14 int vis[MAXN][MAXN]; 15 struct point{ 16 int x; 17 int y; 18 int step; 19 }; 20 queue<point> r; //申请队列 21 22 int dirx[4]={0,1,0,-1}; //四个方向左下右上 23 int diry[4]={1,0,-1,0}; 24 25 int main(){ 26 /* 27 5 4 28 1 1 2 1 29 1 1 1 1 30 1 1 2 1 31 1 2 1 1 32 1 1 1 2 33 1 1 4 3 34 */ 35 int n,m,startx,starty,p,q; 36 cin>>n>>m; 37 for(int i=1;i<=n;i++) 38 for(int j=1;j<=m;j++) 39 cin>>a[i][j]; 40 cin>>startx>>starty>>p>>q; 41 42 //BFS 43 point start; 44 start.x=startx; 45 start.y=starty; 46 start.step=0; 47 r.push(start); //起点入队 48 vis[startx][starty]=1; 49 int flag=0; 50 while(!r.empty()){ 51 int x=r.front().x,y=r.front().y; 52 if(x==p&&y==q){ 53 flag=1; 54 printf("%d",r.front().step); 55 break; 56 } 57 for(int k=0;k<4;k++){ 58 int dx,dy; 59 dx=x+dirx[k]; 60 dy=y+diry[k]; 61 if(a[dx][dy]==1&&vis[dx][dy]==0){ 62 //入队 63 point temp; 64 temp.x=dx; 65 temp.y=dy; 66 temp.step=r.front().step+1; 67 r.push(temp); 68 vis[dx][dy]=1; 69 } 70 } 71 r.pop(); //拓展完了需要把队首元素出队 72 } 73 if(flag==0) 74 printf("NO answer"); 75 return 0; 76 }