【题意】:给你一个8*8的迷宫,(迷宫已知,搜索入门)
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
链接:click here
我用的是简单的深搜,如果用广搜的话注意加优先队列把可能元素加进去。
参考代码:
#include<set> #include<map> #include<cmath> #include<ctime> #include<queue> #include<deque> #include<stack> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define mem(a,b) memset(a,b,sizeof(a)) int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}}; const int maxn=10000; const double eps = 1e-6; //const double Pi = acos(-1.0); double mid,right; int T,a1,a2,b1,b2,cc; int m_ap[][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 x,int y,int s) { if(m_ap[x][y]) return; if(x==b1&&y==b2)//如果是到达目的地,比较最小的路径 { cc=Min(s,cc); return; } s++; m_ap[x][y]=1; dfs(x+1,y,s); dfs(x,y+1,s); dfs(x-1,y,s); dfs(x,y-1,s); m_ap[x][y]=0; } /***************** int shuru()//输入外挂 { int b; bool fushu=0; char ch; while(!((ch=getchar())>='0'&&ch<='9')) { if(ch=='-') { fushu=1; ch=getchar(); break; } } b=(ch-'0'); while(!((ch=getchar())>='0'&&ch<='9')) b=b*10+(ch-'0'); return fushu? -b : b; } ****************/ int main() { //T=shuru(); scanf("%d",&T); while(T--) { cc=999; scanf("%d%d%d%d",&a1,&a2,&b1,&b2); dfs(a1,a2,0); printf("%d\n",cc); } return 0; }