fuz 1205(小鼠迷宫问题)
2013-05-08 12:05
495 查看
/* 首先 广搜,找到最短距离 然后 深搜,寻找方法数 */ #include<stdio.h> #include<string.h> //#include<math.h> #include<queue> #include<algorithm> #include<iostream> using namespace std; #define N 100 int abs(int x) { if(x<0) return -x; return x; } struct node { int x,y,step; }; int dir[4][2]={-1,0,0,1,0,-1,1,0}; int visted ; int map ; int n,m; int x1,y1,x2,y2; int num; int minstep; int bfs() { queue<node>qq; node s,cur,next; s.x=x1; s.y=y1; s.step=0; visted[x1][y1]=1; qq.push(s); while(!qq.empty()) { cur=qq.front(); qq.pop(); if(cur.x==x2 && cur.y==y2) return cur.step; for(int i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m && visted[next.x][next.y]==0 ) { next.step = cur.step + 1; qq.push(next); visted[next.x][next.y]=1; } } } return -1; } void dfs(int x3,int y3,int step) { if(x3==x2 && y3==y2 && step==minstep) { num++; return ; } if(abs(x3-x2)+abs(y3-y2)+step>minstep) return ; for(int i=0;i<4;i++) { int x=x3+dir[i][0]; int y=y3+dir[i][1]; if(x>=1 && x<=n && y>=1 && y<=m && map[x][y]==0) { map[x][y]=1; dfs(x,y,step+1); map[x][y]=0; } } } int main() { int k,x,y; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(visted,0,sizeof(visted)); memset(map,0,sizeof(map)); while(k--) { scanf("%d%d",&x,&y); visted[x][y]=1; map[x][y]=1; } scanf("%d%d",&x1,&y1); scanf("%d%d",&x2,&y2); minstep=-1; minstep=bfs(); if(minstep==-1) { printf("No Solution!\n"); } else { printf("%d\n",minstep); num=0; dfs(x1,y1,0); printf("%d\n",num); } } return 0; }
相关文章推荐
- FZU 1205(小鼠迷宫问题)BFS+DFS的基本综合运用(同一题目中体现两种搜索方法的特点)
- Foj 1205 小鼠迷宫问题
- [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)
- 小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】
- FZU 1205 小鼠迷宫问题
- 第一题:小鼠迷宫问题(方法:广搜)
- 小鼠迷宫问题
- ytu 1980:小鼠迷宫问题(DFS 深度优先搜索)
- nku1215: 小鼠迷宫问题
- 小鼠迷宫问题 SDUT1157 ACM算法设计
- 小鼠迷宫问题---1157
- FOJ 1205 小鼠迷宫问题
- FOJ1205 小鼠迷宫问题 (BFD+递推)
- SDUT1157:小鼠迷宫问题(bfs+dfs)
- SDUT 1157 小鼠迷宫问题(水题,DFS + 回溯)
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
- 小鼠迷宫问题
- fjnu 1354 小鼠迷宫问题
- 小鼠迷宫问题uva1157
- FOJ 1205 小鼠迷宫问题