HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
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
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.
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.
#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的更多相关文章
- POJ 3083:Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- 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 ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- POJ 3083:Children of the Candy Corn
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11015 Acce ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- Runner站立会议之个人记录
备注: 为方便记录,此随笔每日更新(因会议在晚上开,所以将昨天今天改成了今天明天) 2016.4.19 站立会议 今天学习到了:文件创建,adt基本知识,分别在虚拟机和手机上运行软件 明天要:继续学 ...
- iOS开发中你是否遇到这些经验问题
前言 小伙伴们在开发中难免会遇到问题, 你是如何解决问题的?不妨也分享给大家!如果此文章其中的任何一条问题对大家有帮助,那么它的存在是有意义的! 反正不管怎样遇到问题就要去解决问题, 在解决问题的同时 ...
- python——使用readline库实现tab自动补全
Input History readline tracks the input history automatically. There are two different sets of funct ...
- 怎么设置 mysql 多主复制
更新 其实本文主要来自www.digitalocean.com ,但是我没有买他们家的 VPS 用来 demo 了.只是用vagrant 来模拟了. 介绍 说说关于通过两台 vps 来扩展 mysql ...
- ANGULARJS 出错解决
先上代码 程序的意思是使用eval更改指令父scope的数据,在调用$digest 这样就出错了,出错信息如下: 解决办法: 增加timeout事件,具体原因还不是很清楚,反正问题目前是解决了.
- ExtJs中实现tree节点,全部是单击展开和收缩效果,和收藏夹点击功能一样
listeners : { click : function(node, c) {// 单击节点事件(node是节点对象) if(!node.isLeaf()){//不是叶子节点 node.singl ...
- redhat enterprixe 5.0 samba 服务器 rpm 安装及配置
Samba是著名的开源软件项目,在Linux/UNIX系统中实现了SMB/CIFS网络协议,因此使得跨平台的文件共享变得容易.在部署Windows.Linux/UNIX混合平台的企业环境时,使用Sam ...
- event.srcElement在火狐(FireFox)下的兼容问题。搜索框获得焦点时默认文字变化
前言: 项目中用到了一个功能,搜索框里有默认的文字,当搜索框获得焦点时里面的默认文字消失,如果失去焦点时搜索框内容为空则让里面的内容回复默认!,. 实现: 很轻松的在网上找到了类似代码 $(" ...
- [Jquery]焦点图轮播效果
$(function(){ var $next=$(".right"); var $prev=$(".left"); var $list_nu ...
- [css]邮件的写法
<style type="text/css"> /* Client-specific Styles */ #outlook a{paddin ...