POJ 3083:Children of the Candy Corn
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11015 | Accepted: 4744 |
Description
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
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
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
题意是求从S到E的最短路,有意思的是这次还要求两个问题,一个是沿着左手边走的最短路,一个是沿着右手边走的最短路。
仔细思考的话这题不难,就是自己感觉写得比较繁琐,分各种情况和方向考虑,应该可以优化成更简便的代码。
另外这个题被分到深搜里面,我一开始写的也是深搜,但最终结果是广搜更简单一些嘛。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; #define up 1
#define down 2
#define left 3
#define right 4 int row,col;
char value[50][50];
int bushu[50][50]; int dfs_left(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(dir); int temp1,temp2,temp3;
while(x.size())
{
i=x.front();
j=y.front();
temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp3==up)
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1; }
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
}
}
} }
else if(temp3==down)
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1; }
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==left)
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1; }
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==right)
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1; }
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
}
}
}
}
}
return 0;
} int dfs_right(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(dir); int temp1,temp2,temp3;
while(x.size())
{
i=x.front();
j=y.front();
temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp3==up)
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
}
}
} }
else if(temp3==down)
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1; }
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==left)
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1; }
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==right)
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
}
}
}
}
}
return 0;
} int dfs(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y; x.push(i);
y.push(j); while(x.size())
{
i=x.front();
j=y.front(); x.pop();
y.pop(); if(value[i+1][j]=='.'&&!bushu[i+1][j])
{
x.push(i+1);
y.push(j);
bushu[i+1][j]=bushu[i][j]+1;
}
if(value[i][j+1]=='.'&&!bushu[i][j+1])
{
x.push(i);
y.push(j+1);
bushu[i][j+1]=bushu[i][j]+1;
}
if(value[i-1][j]=='.'&&!bushu[i-1][j])
{
x.push(i-1);
y.push(j);
bushu[i-1][j]=bushu[i][j]+1;
}
if(value[i][j-1]=='.'&&!bushu[i][j-1])
{
x.push(i);
y.push(j-1);
bushu[i][j-1]=bushu[i][j]+1;
}
if(value[i+1][j]=='E'||value[i][j+1]=='E'||value[i-1][j]=='E'||value[i][j-1]=='E')
return bushu[i][j]+1;
}
return 0;
} void solve()
{
int i,j;
for(i=2;i<col;i++)
{
if(value[1][i]=='S')
{
cout<<dfs_left(1,i,down)+1<<" ";
cout<<dfs_right(1,i,down)+1<<" ";
cout<<dfs(1,i,down)+1<<endl;
return;
}
} for(i=2;i<col;i++)
{
if(value[row][i]=='S')
{
cout<<dfs_left(row,i,up)+1<<" ";
cout<<dfs_right(row,i,up)+1<<" ";
cout<<dfs(row,i,up)+1<<endl;
return;
}
}
for(i=2;i<row;i++)
{
if(value[i][1]=='S')
{
cout<<dfs_left(i,1,right)+1<<" ";
cout<<dfs_right(i,1,right)+1<<" ";
cout<<dfs(i,1,right)+1<<endl;
return;
}
}
for(i=2;i<row;i++)
{
if(value[i][col]=='S')
{
cout<<dfs_left(i,col,left)+1<<" ";
cout<<dfs_right(i,col,left)+1<<" ";
cout<<dfs(i,col,left)+1<<endl;
return;
}
} } int main()
{
int Test,i,j;
scanf("%d",&Test); while(Test--)
{
scanf("%d%d",&col,&row);
for(i=1;i<=row;i++)
{
scanf("%s",value[i]+1);
}
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3083: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的步数 ...
- 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 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
随机推荐
- Ubuntu安装Orcale
Linux_Ubuntu安装oracle总结 ---------转自 https://www.2cto.com/database/201305/215338.html 话说我花了一晚上才在ubuntu ...
- Zabbix——自动监控
zabbix简介 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管 ...
- 2019 OI日记
// 我觉得记日记是个好习惯吧 毕竟指不定哪天就学不下去了 就AFO了 就没有梦了 // [置顶]活跃于你谷普及训练场.ybt(没底气说全部).loj(提高基础部分) //优先级从前往后 因为 ...
- 前端构建工具gulp超详细配置, 使用教程(图文)
流程 1. 输入命令(可以使用git bash或者命令控制台cmd) npm install -g gulp 安装全局gulp命令 2. 创建一个项目文件夹, 当前项目文件夹下输入命令npm init ...
- [U53204] 树上背包的优化
题目链接 本文旨在介绍树上背包的优化. 可见例题,例题中N,M∈[1,100000]N,M \in [1,100000]N,M∈[1,100000]的数据量让O(nm2)O(nm^2)O(nm2)的朴 ...
- 习题两则的简化(利用for循环)
习题一.打印26个英文字母 public class PrintChars { public static void main(String[] args) { char ch = 'a'; int ...
- 04.Delphi通过接口IInterface实现多重继承
IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 uSayHello代码如下 unit uSayHello; interface uses SysUtils, ...
- Java 定时循环运行程序
Timer 和 ScheduledExecutorSeruvce 都能执行定时的循环任务,有函数 scheduleAtFixedRate.但是,如果任务运行时间较长,超过了一个周期时长,下一个任务就会 ...
- redis学习(三)
一.Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 二.Redis 命令 1 ...
- Linux应用可通过USB访问Android设备-Chrome OS 75版发布
导读 谷歌已经为支持的Chromebook设备发布了Chrome OS 75操作系统,这是一个主要版本,增加了各种新功能,最新安全补丁和其他改进. 对于大多数Chromebook设备,Chrome O ...