Children of the Candy Corn--POJ 3083
2010-08-20 21:12
357 查看
1、题目类型:模拟、迷宫、DFS、BFS。
2、解题思路:经典DFS、BFS的运用:(1)根据输入的字符型Maze[][]转换为整型的map[][],并记录开始位置S,结束位置E,以及开始S的方向;(2)先进行左边优先搜索,传入左边优先的条件进行DFS搜索;再进行右边优先搜索,传入右边优先的条件进行DFS搜索,分别记录其移动步数;(3)BFS搜索获得最少移动步数。
3、注意事项:注意DFS中break的条件、BFS中入队列的条件。
4、实现方法:
2、解题思路:经典DFS、BFS的运用:(1)根据输入的字符型Maze[][]转换为整型的map[][],并记录开始位置S,结束位置E,以及开始S的方向;(2)先进行左边优先搜索,传入左边优先的条件进行DFS搜索;再进行右边优先搜索,传入右边优先的条件进行DFS搜索,分别记录其移动步数;(3)BFS搜索获得最少移动步数。
3、注意事项:注意DFS中break的条件、BFS中入队列的条件。
4、实现方法:
#include<iostream> #include<queue> using namespace std; char Maze[45][45]; int row,col,d1,d2; int start[2],end[2],flag[45][45]; int dir1[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; int dir2[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct Point { int x,y; int step; }; void Init() { memset(flag,0,sizeof(flag)); cin>>col>>row; for(int i=0;i<row;i++) { cin>>Maze[i]; for(int j=0;j<col;j++) { if(Maze[i][j]=='S') { start[0]=i; start[1]=j; } if(Maze[i][j]=='E') { end[0]=i; end[1]=j; } if(Maze[i][j]=='#') flag[i][j]=1; } } if(start[0]==0) d1=1; else if(start[1]==row-1) d1=3; else if(end[1]==col-1) d1=2; else d1=0; if(start[0]==0) d2=1; else if(start[1]==row-1) d2=3; else if(end[1]==col-1) d2=0; else d2=2; } int DFS(int x,int y,int d,int dir[][2]) { int step,tmpx,tmpy,tmp; if(x==end[0]&&y==end[1]) return 1; for(int i=0;i<4;i++) { tmp=(d+i)%4; tmpx=x+dir[tmp][0]; tmpy=y+dir[tmp][1]; if(tmpx>=0&&tmpx<row&&tmpy>=0&&tmpy<col&&!flag[tmpx][tmpy]) break; } step=DFS(tmpx,tmpy,(tmp+3)%4,dir)+1; return step; } int BFS() { queue<Point> Q; Point p; memset(flag,0,sizeof(flag)); p.x=start[0]; p.y=start[1]; p.step=1; flag[p.x][p.y]=1; Q.push(p); while(!Q.empty()) { p=Q.front(); Q.pop(); if(p.x==end[0]&&p.y==end[1]) return p.step; for(int i=0;i<4;i++) { Point tmp; tmp.x=p.x+dir1[i][0]; tmp.y=p.y+dir1[i][1]; if(tmp.x>=0&&tmp.x<row&&tmp.y>=0&&tmp.y<col&&!flag[tmp.x][tmp.y]&&Maze[tmp.x][tmp.y]!='#') { flag[tmp.x][tmp.y]=1; tmp.step=p.step+1; Q.push(tmp); } } } return 0; } int main() { int T; cin>>T; while(T--) { Init(); cout<<DFS(start[0],start[1],d1,dir1)<<' ' <<DFS(start[0],start[1],d2,dir2)<<' '; cout<<BFS()<<endl; } return 0; }
相关文章推荐
- 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 (BFS+DFS)
- 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(广度搜索)
- poj3083 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
- 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 中文题意&题解&代码(C++)
- poj 3083 Children of the Candy Corn