Poj 3083 Children of the Candy Corn
2012-04-06 23:03
316 查看
题目连接:http://poj.org/problem?id=1077
题目描述:给出一个迷宫,#表示墙,S表示起点,E表示终点。求从S到E的优先靠左边的墙走、优先靠右边的墙走和S到E的最短路径。
解法:求优先靠左靠右的路径,用dfs,求最短路径用bfs即可。
dfs中可以用nx = x+move[(i+4)%4][0];ny = y+move[(i+4)%4][1];处理很巧妙
View Code
题目描述:给出一个迷宫,#表示墙,S表示起点,E表示终点。求从S到E的优先靠左边的墙走、优先靠右边的墙走和S到E的最短路径。
解法:求优先靠左靠右的路径,用dfs,求最短路径用bfs即可。
dfs中可以用nx = x+move[(i+4)%4][0];ny = y+move[(i+4)%4][1];处理很巧妙
View Code
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<queue> #define see(x) cout<<#x<<":"<<x<<endl; using namespace std; const int inf = 9999999; const int N = 50; char map ; typedef struct{ int x, y, dis; }Node; const int move[4][4] ={{0,-1},{-1,0},{0,1},{1,0}}; bool vis = {0}; int r, c; queue<Node> q; int bfs(){ int i; Node temp, temp1; int minn = inf; while(!q.empty()){ temp = q.front(); q.pop(); if(map[temp.x][temp.y]=='E'){ if(temp.dis<minn){ minn = temp.dis; } } else{ for(i=0;i<4;i++){ temp1.x = temp.x+move[i][0]; temp1.y = temp.y+move[i][1]; temp1.dis = temp.dis+1; if(temp1.x>=0&&temp1.x<r&&temp1.y>=0&&temp1.y<c&&map[temp1.x][temp1.y]!='#'&&!vis[temp1.x][temp1.y]){ vis[temp1.x][temp1.y] = 1; q.push(temp1); } } } } return minn; } int dfs(int x, int y, int dir, char mark){ int i, ndir, nx, ny; if(map[x][y]=='E'){ return 1; } if(mark=='l'){ for(i=dir-1;i<dir+3;i++){ nx = x+move[(i+4)%4][0]; ny = y+move[(i+4)%4][1]; ndir = (i+4)%4; if(nx>=0&&nx<r&&ny>=0&&ny<c&&map[nx][ny]!='#'){ // cout<<"nx: "<<nx<<" ny: "<<ny<<" "; see(ndir) return dfs(nx,ny,ndir,mark)+1; } } } if(mark=='r'){ for(i=dir+1;i>dir-3;i--){ nx = x+move[(i+4)%4][0]; ny = y+move[(i+4)%4][1]; ndir = (i+4)%4; if(nx>=0&&nx<r&&ny>=0&&ny<c&&map[nx][ny]!='#'){ return dfs(nx,ny,ndir,mark)+1; } } } return 0; } int main(){ int t, lp, rp, sp; int i, j, k, dir; Node s; scanf("%d",&t); while(t--){ scanf("%d%d",&c,&r); for(i=0;i<r;i++) scanf("%s",map[i]); for(i=0;i<r;i++){ for(j=0;j<c;j++){ if(map[i][j]=='S'){ s.x=i; s.y=j; s.dis=1; break; } } } if(s.x==0){ dir=3; } else if(s.x==1){ dir=1; } else if(s.y==0){ dir = 2; } else{ dir = 0; } lp = dfs(s.x,s.y,dir,'l'); vis[s.x][s.y] = 1; rp = dfs(s.x,s.y,dir,'r'); memset(vis,0,sizeof(vis)); vis[s.x][s.y] = 1; s.dis = 1; q.push(s); sp = bfs(); printf("%d %d %d\n",lp,rp,sp); } return 0; }
相关文章推荐
- poj 3083 Children of the Candy Corn bfs
- POJ 3083 Children of the Candy Corn (简单的搜索)
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- POJ3083--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
- 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+BFS)
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- POJ3083--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
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of theCandy Corn(DFS and BFS)