POJ 3083.Children of the Candy Corn
来源:互联网 发布:mac间断性声音很大 编辑:程序博客网 时间:2024/05/16 17:25
深搜 + 广搜
解析:在一幅图里面,先按照优先向左的顺序走一次,得出总步数;再按照优先向右的顺序走一次,得出总步数;最后得出最少步数。
AC代码:
#include <stdio.h>#include <stdlib.h>#include <string.h>#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)); scanf("%d%d",&col,&row); for(int i=0;i<row;i++) { scanf("%s",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; scanf("%d",&T); while(T--) { Init(); printf("%d ",DFS(start[0],start[1],d1,dir1)); printf("%d ",DFS(start[0],start[1],d2,dir2)); printf("%d\n",BFS()); } return 0;}
- POJ 3083.Children of the Candy Corn
- POJ 3083.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-3081-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
- 免费/1元钱MySQL数据库
- 强制日志归档
- Android 多线程网络下载图片
- [LINUX常用命令]查看自己哪个目录占用空间多
- 另类看设计模式[来自酷壳网]
- POJ 3083.Children of the Candy Corn
- oracle缩小回滚表空间大小undotbs01.dbf [linux下]
- python challenge <三>
- java_Socket通信_聊天程序
- MS SQL Server和Oracle对数据库事务处理的差异性
- Android_01
- 17.从零开始学习C语言--多级指针
- 查看内存信息
- linux使用dhclient自动获取IP