最少步数
时间限制:3000 ms | 内存限制:65535 KB
难度:4
- 描述
-
这有一个迷宫,有0~8行和0~8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,10表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
- 输入
- 第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。 - 输出
- 输出最少走几步。
- 样例输入
-
2 3 1 5 7 3 1 6 7
- 样例输出
-
12 11
题解:dfs带回溯;找最小步数;还可以用广搜BFS,以及用优先队列优化; - 代码:
-
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y) x<y?x:y 4 const int MAXN=10; 5 const int INF=1<<30; 6 int map[MAXN][MAXN]={ 7 {1,1,1,1,1,1,1,1,1}, 8 {1,0,0,1,0,0,1,0,1}, 9 {1,0,0,1,1,0,0,0,1}, 10 {1,0,1,0,1,1,0,1,1}, 11 {1,0,0,0,0,1,0,0,1}, 12 {1,1,0,1,0,1,0,0,1}, 13 {1,1,0,1,0,1,0,0,1}, 14 {1,1,0,1,0,0,0,0,1}, 15 {1,1,1,1,1,1,1,1,1} 16 }; 17 int disx[5]={0,-1,0,1}; 18 int disy[5]={1,0,-1,0}; 19 int a,b,c,d,min; 20 void dfs(int x,int y,int t){int nx,ny; 21 if(x==c&&y==d){ 22 min=MIN(min,t); 23 return ; 24 } 25 for(int i=0;i<4;i++){ 26 nx=x+disx[i];ny=y+disy[i]; 27 if(t+1<min&&!map[nx][ny]){ 28 map[nx][ny]=1; 29 dfs(nx,ny,t+1); 30 map[nx][ny]=0; 31 } 32 } 33 return ; 34 } 35 int main(){ 36 int T; 37 /* for(int x=0;x<9;x++){ 38 for(int y=0;y<9;y++)printf("%d ",map[x][y]); 39 puts(""); 40 }*/ 41 scanf("%d",&T); 42 while(T--){min=INF; 43 scanf("%d%d%d%d",&a,&b,&c,&d); 44 map[a][b]=1; 45 dfs(a,b,0); 46 map[a][b]=0; 47 printf("%d ",min); 48 } 49 return 0;}
广搜:
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 const int INF=0xfffffff; 6 int disx[4]={0,1,-1,0}; 7 int disy[4]={1,0,0,-1}; 8 struct Node{ 9 int nx,ny,step; 10 }; 11 queue<Node>dl; 12 Node a,b; 13 int x,y,ex,ey,T,mi; 14 int map[10][10]; 15 void bfs(){ 16 map[x][y]=1; 17 a.nx=x;a.ny=y;a.step=0; 18 dl.push(a); 19 while(!dl.empty()){ 20 a=dl.front(); 21 dl.pop(); 22 map[a.nx][a.ny]=1; 23 if(a.nx==ex&&a.ny==ey){ 24 if(a.step<mi)mi=a.step; 25 map[ex][ey]=0; 26 } 27 for(int i=0;i<4;i++){ 28 b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+1; 29 if(!map[b.nx][b.ny]&&b.step<=mi&&b.nx>=0&&b.ny>=0&&a.nx<9&&b.ny<9)dl.push(b); 30 } 31 } 32 } 33 int main(){ 34 scanf("%d",&T); 35 while(T--){int m[10][10]={ 36 {1,1,1,1,1,1,1,1,1}, 37 {1,0,0,1,0,0,1,0,1}, 38 {1,0,0,1,1,0,0,0,1}, 39 {1,0,1,0,1,1,0,1,1}, 40 {1,0,0,0,0,1,0,0,1}, 41 {1,1,0,1,0,1,0,0,1}, 42 {1,1,0,1,0,1,0,0,1}, 43 {1,1,0,1,0,0,0,0,1}, 44 {1,1,1,1,1,1,1,1,1} 45 }; 46 memcpy((int *)map,(int *)m,sizeof(m[0][0])*100); 47 scanf("%d%d%d%d",&x,&y,&ex,&ey); 48 mi=INF; 49 bfs(); 50 printf("%d ",mi); 51 } 52 return 0; 53 }
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 const int INF=0xfffffff; 6 int disx[4]={0,1,-1,0}; 7 int disy[4]={1,0,0,-1}; 8 struct Node{ 9 int nx,ny,step; 10 friend bool operator < (Node a,Node b){ 11 return a.step > b.step; 12 } 13 }; 14 priority_queue<Node>dl; 15 Node a,b; 16 int x,y,ex,ey,T,mi; 17 int map[10][10]; 18 void bfs(){ 19 map[x][y]=1; 20 a.nx=x;a.ny=y;a.step=0; 21 dl.push(a); 22 while(!dl.empty()){ 23 a=dl.top(); 24 dl.pop(); 25 map[a.nx][a.ny]=1; 26 if(a.nx==ex&&a.ny==ey){ 27 if(a.step<mi)mi=a.step; 28 map[ex][ey]=0; 29 } 30 for(int i=0;i<4;i++){ 31 b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+1; 32 if(!map[b.nx][b.ny]&&b.step<=mi&&b.nx>=0&&b.ny>=0&&a.nx<9&&b.ny<9)dl.push(b); 33 } 34 } 35 } 36 int main(){ 37 scanf("%d",&T); 38 while(T--){int m[10][10]={ 39 {1,1,1,1,1,1,1,1,1}, 40 {1,0,0,1,0,0,1,0,1}, 41 {1,0,0,1,1,0,0,0,1}, 42 {1,0,1,0,1,1,0,1,1}, 43 {1,0,0,0,0,1,0,0,1}, 44 {1,1,0,1,0,1,0,0,1}, 45 {1,1,0,1,0,1,0,0,1}, 46 {1,1,0,1,0,0,0,0,1}, 47 {1,1,1,1,1,1,1,1,1} 48 }; 49 memcpy((int *)map,(int *)m,sizeof(m[0][0])*100); 50 scanf("%d%d%d%d",&x,&y,&ex,&ey); 51 mi=INF; 52 bfs(); 53 printf("%d ",mi); 54 } 55 return 0; 56 }