Children of the Candy Corn
来源:互联网 发布:打开xlsx的软件 编辑:程序博客网 时间:2024/04/30 18:04
Description
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
Sample Input
28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########
Sample Output
37 5 517 17 9
//Memory Time
// 0为上,1为左,2为下,3为右;
#include<iostream>
#include<cstring>
using namespace std;
typedef class
{
public:
int r,c;
int depth;
}SE;
SE s,e; //起止点
int Lstep; //左边优先搜索 时从S到E的总步数
int Rstep; //右边优先搜索 时从S到E的总步数
int shortstep; //S到E的最少总步数;
bool maze[41][41]; //记录迷宫的“可行域”与“墙”
void DFS_LF(int i,int j,int d) //左边优先搜索,i,j为当前点坐标,d为当前位置方向
{
Lstep++;
if(i==e.r && j==e.c)
return;
switch(d)
{
case 0:
{
if(maze[i][j-1])
DFS_LF(i,j-1,1);
else if(maze[i-1][j])
DFS_LF(i-1,j,0);
else if(maze[i][j+1])
DFS_LF(i,j+1,3);
else if(maze[i+1][j])
DFS_LF(i+1,j,2);
break;
}
case 1:
{
if(maze[i+1][j])
DFS_LF(i+1,j,2);
else if(maze[i][j-1])
DFS_LF(i,j-1,1);
else if(maze[i-1][j])
DFS_LF(i-1,j,0);
else if(maze[i][j+1])
DFS_LF(i,j+1,3);
break;
}
case 2:
{
if(maze[i][j+1])
DFS_LF(i,j+1,3);
else if(maze[i+1][j])
DFS_LF(i+1,j,2);
else if(maze[i][j-1])
DFS_LF(i,j-1,1);
else if(maze[i-1][j])
DFS_LF(i-1,j,0);
break;
}
case 3:
{
if(maze[i-1][j])
DFS_LF(i-1,j,0);
else if(maze[i][j+1])
DFS_LF(i,j+1,3);
else if(maze[i+1][j])
DFS_LF(i+1,j,2);
else if(maze[i][j-1])
DFS_LF(i,j-1,1);
break;
}
}
return;
}
void DFS_RF(int i,int j,int d) //右边优先搜索,i,j为当前点坐标,d为当前位置方向
{
Rstep++;
if(i==e.r && j==e.c)
return;
switch(d)
{
case 0:
{
if(maze[i][j+1])
DFS_RF(i,j+1,3);
else if(maze[i-1][j])
DFS_RF(i-1,j,0);
else if(maze[i][j-1])
DFS_RF(i,j-1,1);
else if(maze[i+1][j])
DFS_RF(i+1,j,2);
break;
}
case 1:
{
if(maze[i-1][j])
DFS_RF(i-1,j,0);
else if(maze[i][j-1])
DFS_RF(i,j-1,1);
else if(maze[i+1][j])
DFS_RF(i+1,j,2);
else if(maze[i][j+1])
DFS_RF(i,j+1,3);
break;
}
case 2:
{
if(maze[i][j-1])
DFS_RF(i,j-1,1);
else if(maze[i+1][j])
DFS_RF(i+1,j,2);
else if(maze[i][j+1])
DFS_RF(i,j+1,3);
else if(maze[i-1][j])
DFS_RF(i-1,j,0);
break;
}
case 3:
{
if(maze[i+1][j])
DFS_RF(i+1,j,2);
else if(maze[i][j+1])
DFS_RF(i,j+1,3);
else if(maze[i-1][j])
DFS_RF(i-1,j,0);
else if(maze[i][j-1])
DFS_RF(i,j-1,1);
break;
}
}
return;
}
void BFS_MSS(int i,int j) //最短路搜索,s为树的根,E为树的最深叶子求树的深度;
{
bool vist[41][41]={false};
SE queue[1600];
int head,tail;
queue[head=0].r=i;
queue[tail=0].c=j;
queue[tail++].depth=1; //当前树深标记,这是寻找最短路的关键点
vist[i][j]=true;
while(head<tail)
{
SE x=queue[head++];
if(x.r==e.r && x.c==e.c)
{
cout<<x.depth<<endl;//树的深度;
return;
}
if(maze[x.r][x.c-1] && !vist[x.r][x.c-1])
{
vist[x.r][x.c-1]=true;
queue[tail].r=x.r;
queue[tail].c=x.c-1;
queue[tail++].depth=x.depth+1;
}
if(maze[x.r-1][x.c] && !vist[x.r-1][x.c])
{
vist[x.r-1][x.c]=true;
queue[tail].r=x.r-1;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
if(maze[x.r][x.c+1] && !vist[x.r][x.c+1])
{
vist[x.r][x.c+1]=true;
queue[tail].r=x.r;
queue[tail].c=x.c+1;
queue[tail++].depth=x.depth+1;
}
if(maze[x.r+1][x.c] && !vist[x.r+1][x.c])
{
vist[x.r+1][x.c]=true;
queue[tail].r=x.r+1;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
}
return;
}
int main(int i,int j)
{
int test;
cin>>test;
while(test--)
{
int direction; //起点S的初始方向
int w,h; //size of maze
cin>>w>>h;
/*Initial*/
Lstep=1;
Rstep=1;
memset(maze,false,sizeof(maze));
/*Structure the Maze*/
for(i=1;i<=h;i++)
for(j=1;j<=w;j++)
{
char temp;
cin>>temp;
if(temp=='.')
maze[i][j]=true;//可行域;
if(temp=='S')//起始位置;
{
maze[i][j]=true;
s.r=i;//横坐标,
s.c=j;//纵坐标;
if(i==h)//最后一行;
direction=0;//方向为上;
else if(j==w)//最后一列;
direction=1;//方向为左;
else if(i==1)//第一行;
direction=2;//方向为下;
else if(j==1)//第一列;
direction=3;//方向为右;
}
if(temp=='E')//到达终点;
{
maze[i][j]=true;
e.r=i;
e.c=j;
}
}
/*Left First Search*/
switch(direction)
{
case 0: {DFS_LF(s.r-1,s.c,0); break;}
case 1: {DFS_LF(s.r,s.c-1,1); break;}
case 2: {DFS_LF(s.r+1,s.c,2); break;}
case 3: {DFS_LF(s.r,s.c+1,3); break;}
}
cout<<Lstep<<' ';
/*Right First Search*/
switch(direction)
{
case 0: {DFS_RF(s.r-1,s.c,0); break;}
case 1: {DFS_RF(s.r,s.c-1,1); break;}
case 2: {DFS_RF(s.r+1,s.c,2); break;}
case 3: {DFS_RF(s.r,s.c+1,3); break;}
}
cout<<Rstep<<' ';
/*Most Short Step Search*/
BFS_MSS(s.r,s.c);
}
return 0;
}
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- pku 3083 Children of the Candy Corn
- POJ3083--Children of the Candy Corn
- 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083Children of the Candy Corn
- POJ3083 Children of the Candy Corn 搜索
- B - Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- 环境变量配置
- Message 2100 not found; No message file for product=RDBMS, facility=UL
- 2016-09-26
- 【编程语言】Python 指定解释器
- 程序报错:”_SCNetworkReachabilityCreateWithAddress", referenced from:或 "_UTTypeCopyPreferredTagWithClass"
- Children of the Candy Corn
- Oracle 11r2 数据库冷备
- ViewPager之FragmentStatePagerAdapter小结
- Qt 之 QQ系统表情—实现动态显示效果
- Android动画之自定义Evaluator实现弹球效果
- 【Uniy3d之EnhanceScollView选择角色3D循环滚动效果实现】
- Android Studio导入eclipse工程出现“app:mergeDebugResources“
- padding oracle攻击原理分析(附带rsa资料)
- 详解contextConfigLocation