HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn

时间:2023-01-21 21:39:51

Children of the Candy Corn

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 20   Accepted Submission(s) : 10
Problem Description
The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 
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
Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 
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
For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
 
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
 
Sample Output
37 5 5
 
17 17 9
 
Source
PKU
 
 
 
BFS,感觉这道题真挺麻烦的,写得很长....
 
 
#include<iostream>
#include<cstring>
using namespace std; int w,h,stepl=1,stepr=1,step[40][40],r,c;
char maze[40][41]; void forward(int &i,int &j,int k)
{
if(k==1)
i--;
else if(k==2)
j++;
else if(k==3)
i++;
else if(k==4)
j--;
} bool judge(int k)
{
int i=r,j=c;
forward(i,j,k);
if(maze[i][j]=='#')
return false;
else
return true;
} int leftside(int k)
{
k--;
if(k==0)
k=4;
while(!judge(k))
{
k++;
if(k==5)
k=1;
}
forward(r,c,k);
stepl++;
if(maze[r][c]=='E')
return stepl;
return leftside(k);
} int rightside(int k)
{
k++;
if(k==5)
k=1;
while(!judge(k))
{
k--;
if(k==0)
k=4;
}
forward(r,c,k);
stepr++;
if(maze[r][c]=='E')
return stepr;
return rightside(k);
} int BFS()
{
int que[1600*2][2];
int front=0,rear=1;
bool f[40][41]={false};
que[0][0]=r;
que[0][1]=c;
f[r][c]=true;
while(front<rear)
{
int t=step[que[front][0]][que[front][1]];
if(que[front][0]-1>=0&&maze[que[front][0]-1][que[front][1]]!='#'&&!f[que[front][0]-1][que[front][1]])
{
que[rear][0]=que[front][0]-1;
que[rear][1]=que[front][1];
step[que[rear][0]][que[rear][1]]=t+1;
if(maze[que[rear][0]][que[rear][1]]=='E')
return step[que[rear][0]][que[rear][1]];
f[que[rear][0]][que[rear][1]]=true;
rear++;
}
if(que[front][1]+1<w&&maze[que[front][0]][que[front][1]+1]!='#'&&!f[que[front][0]][que[front][1]+1])
{
que[rear][0]=que[front][0];
que[rear][1]=que[front][1]+1;
step[que[rear][0]][que[rear][1]]=t+1;
if(maze[que[rear][0]][que[rear][1]]=='E')
return step[que[rear][0]][que[rear][1]];
f[que[rear][0]][que[rear][1]]=true;
rear++;
}
if(que[front][0]+1<h&&maze[que[front][0]+1][que[front][1]]!='#'&&!f[que[front][0]+1][que[front][1]])
{
que[rear][0]=que[front][0]+1;
que[rear][1]=que[front][1];
step[que[rear][0]][que[rear][1]]=t+1;
if(maze[que[rear][0]][que[rear][1]]=='E')
return step[que[rear][0]][que[rear][1]];
f[que[rear][0]][que[rear][1]]=true;
rear++;
}
if(que[front][1]-1>=0&&maze[que[front][0]][que[front][1]-1]!='#'&&!f[que[front][0]][que[front][1]-1])
{
que[rear][0]=que[front][0];
que[rear][1]=que[front][1]-1;
step[que[rear][0]][que[rear][1]]=t+1;
if(maze[que[rear][0]][que[rear][1]]=='E')
return step[que[rear][0]][que[rear][1]];
f[que[rear][0]][que[rear][1]]=true;
rear++;
}
front++;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
int i,j,si,sj,k;
stepl=1;
stepr=1;
memset(step,0,sizeof(step));
cin>>w>>h;
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='S')
{
si=i;
sj=j;
}
}
}
r=si;
c=sj;
if(r-1>=0&&maze[r-1][c]=='.')
k=1;
else if(c+1<w&&maze[r][c+1]=='.')
k=2;
else if(r+1<h&&maze[r+1][c]=='.')
k=3;
else if(c-1>=0&&maze[c-1][r]=='.')
k=4;
cout<<leftside(k)<<' ';
r=si,c=sj;
cout<<rightside(k)<<' ';
r=si,c=sj;
step[r][c]=1;
cout<<BFS()<<endl;
}
}

HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn的更多相关文章

  1. POJ 3083:Children of the Candy Corn(DFS&plus;BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  2. POJ 3083 -- Children of the Candy Corn(DFS&plus;BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  3. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  4. Children of the Candy Corn 分类: POJ 2015-07-14 08&colon;19 7人阅读 评论&lpar;0&rpar; 收藏

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10933   Acce ...

  5. POJ3083——Children of the Candy Corn&lpar;DFS&plus;BFS&rpar;

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  6. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  7. K - Children of the Candy Corn&lpar;待续&rpar;

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

  8. poj3083 Children of the Candy Corn BFS&amp&semi;&amp&semi;DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  9. POJ 3083:Children of the Candy Corn

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11015   Acce ...

  10. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 &plus; bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

随机推荐

  1. 忘记hmailiserver邮件服务器后台登陆密码解决

    进入后台进行hmailiserver的相关设置,发现登陆密码忘记了,如下图:

  2. VMware Workstation不可恢复的错误&colon;&lpar;vmui&rpar;

    虚拟机中部署项目,由于项目的日志是gbk的,就把虚拟机中linux编码改成gbk了,结果问题来了,日志显示中文正常了,但是虚拟机运行一下就出错了,注意虚拟机出错,并没导致linux也挂掉,只是linu ...

  3. android&period;mk文件里的通配符

    比方你有如下目录,要编译Classes目录和Code目录下所有cpp src |-android.mk |-Classes |-A.cpp |-B.cpp |-....cpp |-Code |-E.c ...

  4. iOS开发之窗口和视图

    视图就是应用程序的界面.视图可以使用nib文件实现,也可以使用代码创建.一个视图也是一个响应器(UIResponder的子类)这意味着一个视图可以与用户交互.因此,视图不只是用户可看到的界面,也是可以 ...

  5. 在 &period;NET Core 中结合 HttpClientFactory 使用 Polly(上篇)

    译者:王亮作者:Polly 团队原文:http://t.cn/EhZ90oq 译者序一:前两天写了一篇文章 .NET Core 开源项目 Polly 介绍,在写这篇文章查看 Polly 资料时,看到了 ...

  6. 结对项目&colon;ATM

    一:结对项目名称:ATM 二:源代码及单元测试代码网址链接:https://github.com/tpp531853660 三:结对人的博客链接:http://www.cnblogs.com/Joan ...

  7. 题目1015:还是A&plus;B&lpar;简单判断&rpar;

    题目链接:http://ac.jobdu.com/problem.php?pid=1015 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  8. 《大话设计模式》c&plus;&plus;实现 状态模式

    状态模式包含如下角色: Context: 环境类 State: 抽象状态类 ConcreteState: 具体状态类 2)适用场景: a)状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂 ...

  9. Failed to start docker&period;service&colon; Unit not found&period;

    安装教程参考: https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce-1 https://yq.aliyu ...

  10. Javascript 面向对象实践

    踩到了坑,才能学到东西. 记录我在慢慢的转向模块化遇到的问题以及解决的思路. 1.采用立即执行函数,闭包的方式创建模块 html: <!DOCTYPE html> <html lan ...