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+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+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:19 7人阅读 评论(0) 收藏

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

  5. POJ3083——Children of the Candy Corn(DFS+BFS)

    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(待续)

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

  8. poj3083 Children of the Candy Corn BFS&&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搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

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

随机推荐

  1. 联系 管理 Hibernate4+Spring JPA+SpringMVC+Volecity搭建web应用(三)

    hibernate注解实体类示例 package cn.bdqn.smvc.entity; import java.io.Serializable; import javax.persistence. ...

  2. Thread 总结

    进程:是一个正在执行的程序 每一个进程执行都有一个执行顺序.该顺序是一个执行路劲,后者叫一个控制单元. 线程:就是进程中的一个独立控制单元. 线程在控制着进程的执行 一个进程中至少有个一个线程 Jav ...

  3. 用SQL语句操作数据库

    —―有一天,当你发觉日子特别的艰难,那可能是这次的收获将特别的巨大.—―致那些懈怠的岁月 本章任务: 学生数据库中数据的增加.修改和删除 目标: 1:使用T-SQL向表中插入数据 2:使用T-SQL更 ...

  4. 实现IEnumberable接口和IEnumberator

    class BookEnum : IEnumerator //实现foreach语句内部,并派生 { public Book[] _book; //实现数组 ;//设置“指针” public Book ...

  5. C#识别验证码技术-Tesseract

    相信大家在开发一些程序会有识别图片上文字(即所谓的OCR)的需求,比如识别车牌.识别图片格式的商品价格.识别图片格式的邮箱地址等等,当然需求最多的还是识别验证码.如果要完成这些OCR的工作,需要你掌握 ...

  6. ASP.NET文章目录导航

    ASP.NET文章目录导航 ASP.NET-[读书笔记]-原创:ASP.Net状态管理读书笔记--思维导图 (2013-12-25 10:13) ASP.NET-[潜在危险]-从客户端中检测到有潜在危 ...

  7. FZU 2093 找兔子 状压DP

    题目链接:找兔子 n的范围是[1, 15],可以用0 到 (1<<n)-1 的数表示全部状态,用dp[i] = t表示到达状态i的最少时间是t,对于每个点,如果它能到达的所有点在t秒时都已 ...

  8. FZU 2032 Log函数问题 模拟小数加法

    题目链接:Log函数问题 2 / 49 Problem G FZU 2032 Log函数问题 不知道为什么...比赛时高精度难倒了一票人...成功搞出大新闻... 试了一下直接double相加超时,然 ...

  9. 转: ORACLE索引介绍和使用

    1.什么是索引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据: Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引: 索引由根节点.分支节点 ...

  10. HDU 4405 Aeroplane chess 概率DP 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...