标签:style blog http io ar color os sp on
这有一个迷宫,有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,1
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
2 3 1 5 7 3 1 6 7
12 11
#include<stdio.h> int x,y,sum; int vis[9][9]; int map[9][9]={ 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,1 }; void dfs(int i,int j,int ans) { if(i<0||j<0||i>8||j>8||map[i][j]||vis[i][j]||ans>=sum) return; if(i==x&&j==y) { sum=ans; return; } vis[i][j]=1; dfs(i-1,j,ans+1); dfs(i+1,j,ans+1); dfs(i,j+1,ans+1); dfs(i,j-1,ans+1); vis[i][j]=0; } int main() { int test,a,b; scanf("%d",&test); while(test--) { scanf("%d%d%d%d",&a,&b,&x,&y); sum=100; dfs(a,b,0); printf("%d\n",sum); } return 0; }
标签:style blog http io ar color os sp on
原文地址:http://blog.csdn.net/hdd871532887/article/details/41621715