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 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- Java学习之约瑟夫环的两中处理方法
package day_2; import java.util.Scanner; /** * @author Administrator * 约瑟夫环问题: 设编号为 1,2,3,....n的N个人围 ...
- .net读取xml文件中文乱码问题解决
读取XML的编码方式Encoding.UTF8 要和<?xml version="1.0" encoding="utf-8"?>的encoding ...
- query attr prop区别
大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled = "disabled",checked="checked", ...
- Objective-C( 三、方法的声明与实现)
OC方法的声明与实现 oc方法的声明在@interface中 大括号外@end上面 oc方法的实现在@implementation 中@end上面 OC方法中,一个参数对应一个冒号 方法名: 例 f ...
- netty4 Handler的执行顺序
转载:https://my.oschina.net/jamaly/blog/272385 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通 ...
- Servlet容器如何同时来处理多个请求
工作者线程Work Thread:执行代码的一组线程调度线程Dispatcher Thread:每个线程都具有分配给它的线程优先级,线程是根据优先级调度执行的Servlet采用多线程来处理多个请求同时 ...
- 一些qml资料
qml开发ios应用 http://www.seanyxie.com/qt-qml%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9C%A8ios%E ...
- Codeigniter 集成sphinx搜索 这里采用的是coreseek中文搜索引擎,具体安装请参考官方网站
先上效果图 加入sphinx类库(/application/libraries/sphinx_client.php) 0001 <?php 0002 0003 // 0004 // $Id: s ...
- 转: Jsp9个内置对象详解
1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例. 序号方法说明 objectgetA ...
- P264练习题1.2题
package 集合; import java.util.*; public class fourteen { public static void main(String[] args) { //1 ...