POJ 3083 Children of the Candy Corn dfs+bfs
2012-11-07 14:19
639 查看
题目连接http://poj.org/problem?id=3083
题目大意:一看就是基本的迷宫问题,一个是优先往左走,一个优先往右走,还有一个是求最短。
额。。。。好久没写bfs= =...上来bfs没加vis= =。。。。然后dfs又写错,因为是左右思路相同,我是直接复制的,找了好久一直是re= =、你妹妹的。。。
用一个全局变量保存朝向,用一个%4来保存结果
View Code
题目大意:一看就是基本的迷宫问题,一个是优先往左走,一个优先往右走,还有一个是求最短。
额。。。。好久没写bfs= =...上来bfs没加vis= =。。。。然后dfs又写错,因为是左右思路相同,我是直接复制的,找了好久一直是re= =、你妹妹的。。。
用一个全局变量保存朝向,用一个%4来保存结果
View Code
#include <stdio.h> #include <string.h> char map[100][100]; int m,n,sx,sy,dx,dy; int dir; int lx[4] = {1,0,-1,0}; int ly[4] = {0,-1,0,1}; int rx[4] = {-1,0,1,0}; int ry[4] = {0,-1,0,1}; int leap = 0; struct node { int x,y,step; }q[100000]; int is_map(int x,int y) { if(x >= 0 &&x < n && y >= 0 && y < m) return 1; return 0; } int dfs(int is_left,int x,int y,int step) { if(is_map(x,y) == 0) return 0; if(map[x][y] == '#') return 0; if(map[x][y] == 'E') return step+1; if(is_left) { dir = (dir+3)%4; while(1) { int tx,ty; tx = x+lx[dir]; ty = y+ly[dir]; leap = dfs(1,tx,ty,step+1); if(leap) break; dir = (dir+1)%4; } } else { dir = (dir+3)%4; while(1) { int tx,ty; tx = x+rx[dir]; ty = y+ry[dir]; leap = dfs(0,tx,ty,step+1); if(leap > 0) break; dir = (dir+1)%4; } } return leap; } void bfs() { int vis[45][45] = {0}; int f,r,i; f = r = 0; q[f].x = sx; q[r].y = sy; q[r].step = 1; r++; while(f<r) { struct node temp; temp = q[f++]; for(i = 0;i < 4;i++) { struct node now; now.x = temp.x+lx[i]; now.y = temp.y+ly[i]; now.step = temp.step+1; if(is_map(now.x,now.y) && !vis[now.x][now.y]) { if(map[now.x][now.y] == 'E') { printf("%d\n",now.step); return; } vis[now.x][now.y] = 1; if(map[now.x][now.y] != '#') q[r++] = now; } } } } int main() { int i,j,t,lsum,rsum; leap = 0; scanf("%d",&t); while(t--){ scanf("%d %d",&m,&n); for(i = 0;i < n;i++) { scanf("%s",map[i]); } for(i = 0;i < n;i++) for(j = 0;j < m;j++) { if(map[i][j] == 'S') sx = i,sy = j; if(map[i][j] == 'E') dx = i,dy = j; } dir = 0; leap = 0; lsum = dfs(1,sx,sy,0); dir = 0; rsum = dfs(0,sx,sy,0); printf("%d %d ",lsum,rsum); bfs(); } return 0; }
相关文章推荐
- 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 (BFS+顺时针逆时针DFS)
- POJ 3083 Children of the Candy Corn(BFS+DFS)
- POJ3083-Children of the Candy Corn(bfs+dfs)
- 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(BFS+DFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- 【POJ】3083- A Children of the Candy Corn DFS、BFS、迷宫靠墙走
- POJ 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn (BFS+DFS)
- [POJ 3083]Children of the Candy Corn[DFS+BFS]
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- POJ 3083 Children of the Candy Corn(DFS + BFS)
- POJ3083——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 (BFS+顺时针逆时针DFS)
- Poj 3083 Children of the Candy Corn (DFS+BFS)