POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
2012-08-04 23:43
651 查看
题目:http://poj.org/problem?id=3083
搜索好题。学会了一种控制搜索方向的方法。
求S到E的最短距离很简单,直接BFS就可以了。关键就是处理怎么样保证沿左墙壁走和沿右墙壁走。
会用了顺时针、逆时针dfs这个神奇又好用的东西,很方便~~~
自己也不会总结了。。。。。。就在代码中标出来吧。。。。。。
搜索好题。学会了一种控制搜索方向的方法。
求S到E的最短距离很简单,直接BFS就可以了。关键就是处理怎么样保证沿左墙壁走和沿右墙壁走。
会用了顺时针、逆时针dfs这个神奇又好用的东西,很方便~~~
自己也不会总结了。。。。。。就在代码中标出来吧。。。。。。
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring> using namespace std; int w,h; char s[40][40]; int sx,sy,ex,ey; int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; int num[40][40]; void bfs() { int vis[40][40]; memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); queue < pair<int , int> > Q; while(!Q.empty()) Q.pop(); num[sx][sy]=1; Q.push(make_pair(sx,sy)); vis[sx][sy]=1; while(!Q.empty()) { int kx=Q.front().first; int ky=Q.front().second; Q.pop(); if (kx==ex && ky==ey) break; for (int i=0;i<4;i++) { int px=kx+dx[i]; int py=ky+dy[i]; if (s[px][py]!='#' && !vis[px][py]) { Q.push(make_pair(px,py)); num[px][py]=num[kx][ky]+1; vis[px][py]=1; } } } return ; } int dfs_anticlockwise(int x,int y,int d) //逆时针dfs,即左优先,d要与实际反向,具体结合下面那行代码理解下。。。。。。 { if (x==ex && y==ey) return 1; int px,py,temp; for (int i=1;i<=4;i++) { temp=(d+i)%4; //决定逆时针方向的核心代码 px=x+dx[temp]; py=y+dy[temp]; if (px>=0 && px<h && py>=0 && py<w && s[px][py]!='#') break; } int step=dfs_anticlockwise(px,py,(temp+2)%4)+1; return step; } int dfs_clockwise(int x,int y,int d) //顺时针dfs,即右优先,d与实际反向 { if (x==ex && y==ey) return 1; int px,py,temp; for (int i=1;i<=4;i++) { temp=(4+d-i)%4; //决定顺时针方向的核心代码 px=x+dx[temp]; py=y+dy[temp]; if (px>=0 && px<h && py>=0 && py<w && s[px][py]!='#') break; } int step=dfs_clockwise(px,py,(temp+2)%4)+1; return step; } int main() { int n; cin>>n; while(n--) { cin>>w>>h; for (int i=0;i<h;i++) for (int j=0;j<w;j++) { cin>>s[i][j]; if (s[i][j]=='S') { sx=i; sy=j; } if (s[i][j]=='E') { ex=i; ey=j; } } int d=0; bfs(); int ans1=dfs_anticlockwise(sx,sy,d); int ans2=dfs_clockwise(sx,sy,d); cout<<ans1<<" "<<ans2<<" "<<num[ex][ey]<<endl; } return 0; }
相关文章推荐
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- POJ bfs+dfs之3083-Children of the Candy Corn
- 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)
- 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(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 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(dfs+bfs)
- poj 3083 Children of the Candy Corn (dfs+bfs)