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. Tomcat Manager用户配置详解

      Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Ma ...

  2. 关于使用dotnetbar开发winform程序在用户电脑上部署时问题

    1.首先要安装两个软件

  3. HDFS使用0

    创建上传文件的:

  4. struts2视频学习笔记 18(自定义拦截器)

    课时18 自定义拦截 因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defa ...

  5. svn resolve/merge

    svn merge http://svn.a.com/branches/20150129_168954_sales-impr_1 svn resolve --accept working web/sr ...

  6. Servlet页面跳转实现方法的区别

    一直对Servlet页面跳转的几种方式理解的糊里糊涂的,今天在网上搜了一把,找到一遍比较好的,记下来,以后看看. Servlet页面跳转分两部分,一是发生在Servlet,一是在JSP,其实JSP也就 ...

  7. 二模 (2) day2

    第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...

  8. IBatis.net动态SQL语句(六)

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...

  9. Linux-如何查看登陆shell的类型

    输入一个系统不认识的命令(如#ig)获得系统提示 aix/#ig ksh ig not found #echo $ (适用sh/ksh) aix/#echo $ ksh #echo $SHELL(用户 ...

  10. Hibernate4 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...