POJ-3081-Children of the Candy Corn
来源:互联网 发布:python的readline 编辑:程序博客网 时间:2024/06/06 03:31
这个题是一个DFS加BFS吧。
大意是说给你一个迷宫,要求你求出从起点到终点的三种最短路径。
第一种:选择尽量沿着左边的墙走,即优先考虑向左走。
第二种:选择尽量沿着右边的墙走,即悠闲考虑向右走。
第三种:无任何限制下的最短路径。
这个题最后所求的步数起点和终点都要算入计数,即其求的是经过多少个方格。
处理的方法是分别对于第一种和第二种进行优先逆时针和顺时针转一次方向进行搜索(保证其最优策略),后面再向相反的方向转回来即可,共需要4次,从而得到搜索的答案。
第三种情况我依然采用的是BFS搜索的方法。
代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#define MAX 45using namespace std;int map[MAX][MAX],use[MAX][MAX],w,h,sx,sy,ex,ey,flag,ansa,ansb,ansc;int movex[4]={0,-1,0,1},movey[4]={-1,0,1,0};bool DFS_Left(int x,int y,int t,int pos){ if(map[x][y]=='#')return false; if(x==ex&&y==ey) {ansa=t;return true; } int t1=(pos+3)%4; for(int i=0;i<4;i++) {int itx=x+movex[t1];int ity=y+movey[t1];if(DFS_Left(itx,ity,t+1,t1)){ flag=1; break;}pos=t1;t1=(pos+1)%4; } return flag;}bool DFS_Right(int x,int y,int t,int pos){ if(map[x][y]=='#')return false; if(x==ex&&y==ey) {ansb=t;return true; } int t1=(pos+1)%4; for(int i=0;i<4;i++) {int itx=x+movex[t1];int ity=y+movey[t1];if(DFS_Right(itx,ity,t+1,t1)){ flag=1; break;}pos=t1;t1=(pos+3)%4; } return flag;}void BFS(int sx,int sy){ memset(use,0,sizeof(use)); queue<int> x,y; x.push(sx); y.push(sy); use[sx][sy]=1; int flag=0,cou=0,lastcou=1,nxtcou=0,ans=0; while(!x.empty()) {int itx=x.front();x.pop();int ity=y.front();y.pop();cou++;for(int i=0;i<4;i++){ int xx=itx+movex[i]; int yy=ity+movey[i]; if(map[xx][yy]=='#'||use[xx][yy])continue; if(xx==ex&&yy==ey) {flag=1;while(!x.empty()) x.pop(),y.pop();ansc=ans;break; } use[xx][yy]=1; x.push(xx); y.push(yy); nxtcou++;}if(flag) break;if(cou==lastcou){ lastcou=nxtcou; cou=nxtcou=0; ans++;} }}int main(){ int cas; scanf("%d",&cas); while(cas--) {scanf("%d%d",&w,&h);for(int i=0;i<=w+1;i++) map[0][i]=map[h+1][i]='#';for(int i=0;i<=h+1;i++) map[i][0]=map[i][w+1]='#';for(int i=1;i<=h;i++){ getchar(); for(int j=1;j<=w;j++) {scanf("%c",&map[i][j]);if(map[i][j]=='S') sx=i,sy=j;else if(map[i][j]=='E') ex=i,ey=j; }}memset(use,0,sizeof(use));use[sx][sy]=1;for(int i=0;i<4;i++) if(DFS_Left(sx,sy,1,i))break;for(int i=0;i<4;i++) if(DFS_Right(sx,sy,1,i))break;BFS(sx,sy);printf("%d %d %d\n",ansa,ansb,ansc+2); } return 0;}
- POJ-3081-Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083Children 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
- 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
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- apache2.4.3,php5.4.11安装笔记,win7系统64位
- 目前存在的问题以及接下来的任务
- [gpu pro2]使用曲面细分技术渲染地形和海水
- mysql利用存储过程插入测试数据
- 03_事件
- POJ-3081-Children of the Candy Corn
- leetcode 80: Spiral Matrix
- JAVA JTextField 只能输入小数(完美)//
- 专业程序员必知的技巧:敲打代码
- 390. Teach others by your example. 躬亲示范
- android下使用google test入门
- leetcode 81: Spiral Matrix II
- java运算符的结合性和优先级
- SQL查找删除重复行