poj 3083 Children of the Candy Corn(搜索)
2013-02-05 13:31
351 查看
http://poj.org/problem?id=3083
题意:迷宫沿着左墙走,和沿着右强的距离以及最短路径的距离分别是多少,当向左走的时候1的位置就是面向左,向右走1的位置面向右,这样可以省去很多事
分析:DFS+BFS
先确定前一位置的方向,再决定下一位置是朝哪个方向旋转
题意:迷宫沿着左墙走,和沿着右强的距离以及最短路径的距离分别是多少,当向左走的时候1的位置就是面向左,向右走1的位置面向右,这样可以省去很多事
分析:DFS+BFS
先确定前一位置的方向,再决定下一位置是朝哪个方向旋转
#include<stdio.h> #include<string.h> #include<queue> using namespace std; const int MAXN=50; int m,n; int flag,ans1,ans2,ans3; char map[MAXN][MAXN]; int cnt[MAXN][MAXN]; int vis[MAXN][MAXN]; struct Node { int x,y; } s,e; void DFS_L(int x,int y,int step,int face) { if(flag) return ; if(x==e.x && y==e.y) { ans1=step; flag=1; return ; } switch(face) { case 1://左 if(map[x+1][y]) DFS_L(x+1,y,step+1,4); else if(map[x][y-1]) DFS_L(x,y-1,step+1,1); else if(map[x-1][y]) DFS_L(x-1,y,step+1,2); else if(map[x][y+1]) DFS_L(x,y+1,step+1,3); break; case 2://上 if(map[x][y-1]) DFS_L(x,y-1,step+1,1); else if(map[x-1][y]) DFS_L(x-1,y,step+1,2); else if(map[x][y+1]) DFS_L(x,y+1,step+1,3); else if(map[x+1][y]) DFS_L(x+1,y,step+1,4); break; case 3://右 if(map[x-1][y]) DFS_L(x-1,y,step+1,2); else if(map[x][y+1]) DFS_L(x,y+1,step+1,3); else if(map[x+1][y]) DFS_L(x+1,y,step+1,4); else if(map[x][y-1]) DFS_L(x,y-1,step+1,1); case 4://下 if(map[x][y+1]) DFS_L(x,y+1,step+1,3); else if(map[x+1][y]) DFS_L(x+1,y,step+1,4); else if(map[x][y-1]) DFS_L(x,y-1,step+1,1); else if(map[x-1][y]) DFS_L(x-1,y,step+1,2); break; } } void DFS_R(int x,int y,int step,int face) { if(flag) return ; if(x==e.x && y==e.y) { ans2=step; flag=1; return ; } switch(face) { case 1://右 if(map[x+1][y]) DFS_R(x+1,y,step+1,4); else if(map[x][y+1]) DFS_R(x,y+1,step+1,1); else if(map[x-1][y]) DFS_R(x-1,y,step+1,2); else if(map[x][y-1]) DFS_R(x,y-1,step+1,3); break; case 2://上 if(map[x][y+1]) DFS_R(x,y+1,step+1,1); else if(map[x-1][y]) DFS_R(x-1,y,step+1,2); else if(map[x][y-1]) DFS_R(x,y-1,step+1,3); else if(map[x+1][y]) DFS_R(x+1,y,step+1,4); break; case 3://左 if(map[x-1][y]) DFS_R(x-1,y,step+1,2); else if(map[x][y-1]) DFS_R(x,y-1,step+1,3); else if(map[x+1][y]) DFS_R(x+1,y,step+1,4); else if(map[x][y+1]) DFS_R(x,y+1,step+1,1); case 4://下 if(map[x][y-1]) DFS_R(x,y-1,step+1,3); else if(map[x+1][y]) DFS_R(x+1,y,step+1,4); else if(map[x][y+1]) DFS_R(x,y+1,step+1,1); else if(map[x-1][y]) DFS_R(x-1,y,step+1,2); break; } } int BFS() { int i; queue<Node> Q; Q.push(s); Node head,next; while(!Q.empty()) { head=Q.front(); Q.pop(); if(!vis[head.x-1][head.y] && map[head.x-1][head.y]) { next.x=head.x-1; next.y=head.y; Q.push(next); vis[next.x][next.y]=1; cnt[next.x][next.y]=cnt[head.x][head.y]+1; } if(!vis[head.x+1][head.y]&& map[head.x+1][head.y]) { next.x=head.x+1; next.y=head.y; Q.push(next); vis[next.x][next.y]=1; cnt[next.x][next.y]=cnt[head.x][head.y]+1; } if(!vis[head.x][head.y-1]&& map[head.x][head.y-1]) { next.x=head.x; next.y=head.y-1; Q.push(next); vis[next.x][next.y]=1; cnt[next.x][next.y]=cnt[head.x][head.y]+1; } if(!vis[head.x][head.y+1] && map[head.x][head.y+1]) { next.x=head.x; next.y=head.y+1; Q.push(next); vis[next.x][next.y]=1; cnt[next.x][next.y]=cnt[head.x][head.y]+1; } vis[head.x][head.y]=1; if(e.x==head.x && e.y==head.y) return cnt[head.x][head.y]; } return cnt[head.x][head.y]; } int main() { int T,i,j; scanf("%d",&T); while(T--) { memset(map,0,sizeof(map)); scanf("%d%d",&m,&n); for(i=1; i<=n; i++) { scanf("%s",map[i]+1); for(j=1; map[i][j]; j++) { if(map[i][j]=='.') map[i][j]=1; else if(map[i][j]=='#') map[i][j]=0; if(map[i][j]=='S') { s.x=i; s.y=j; map[i][j]=1; } else if(map[i][j]=='E') { e.x=i; e.y=j; map[i][j]==1; } } } flag=0; DFS_L(s.x,s.y,1,1); flag=0; DFS_R(s.x,s.y,1,1); memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis)); ans3=BFS(); printf("%d %d %d\n",ans1,ans2,ans3+1); } return 0; }
相关文章推荐
- POJ 3083 Children of the Candy Corn (简单的搜索)
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
- POJ-3083 Children of the Candy Corn 解题报告(搜索) 广搜深搜走迷宫
- POJ-3083 Children of the Candy Corn 恶心搜索
- poj 3083 Children of the Candy Corn(广度搜索)
- poj 3083 Children of the Candy Corn 搜索
- poj 3083 Children of the Candy Corn
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ-3083 Children of the Candy Corn 解题报告
- POJ3083-Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn
- 【POJ】3083- A Children of the Candy Corn DFS、BFS、迷宫靠墙走
- POJ 3083 Children of the Candy Corn(BFS+DFS)
- poj 3083--Children of the Candy Corn