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 ...
随机推荐
- An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist. Its class, com.google.gson.GsonBuilder, is available from the foll
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/G:/sharp/repo ...
- vux 中 this.$vux.loading undefined 的问题
时间:2018-04-03 摘要:this.$vux.loading 报 undefined 今天在使用 事件触发 vux 的 loading 组件时,发现无法触发成功,显示 undefined 然 ...
- R 《回归分析与线性统计模型》page93.6
rm(list = ls()) #数据处理 library(openxlsx) library(car) library(lmtest) data = read.xlsx("xiti4.xl ...
- css的响应式布局和动画
把响应式布局和动画放在一起写是因为他们有个共同点@符号 先讲讲响应式布局@media 响应式布局==曾经==非常的流行,这种布局方式可以做出一也兼容一切设备的页面,但是当页面的功能越来越多,css文件 ...
- Jdk8中Stream流的使用,让你脱离for循环
学习要求: 知道一点儿函数式接口和Lambda表达式的基础知识,有利于更好的学习. 1.先体验一下Stream的好处 需求:给你一个ArrayList用来保存学生的成绩,让你打印出其中大于60的成绩. ...
- NFS挂载共享文件夹
修改rcS启动脚本,使开发板初始化完成,自动挂载共享文件夹 修改开发板ip,使之与虚拟机处于同一网段(二者可以互ping) 挂载虚拟机的共享文件夹 rcS 1 ifconfig eth ...
- B - Stacks of Flapjacks UVA - 120
BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...
- 数据结构——KMP(串)
KMP一个非常经典的字符串模式匹配算法,虽然网上有很多kmp算法的博客,但是为了更好的理解kmp我还是自己写了一遍(这个kmp的字符串存储是基于堆的(heap),和老师说的定长存储略有不同,字符串索引 ...
- HYSBZ - 1588 营业额统计 (伸展树)
题意:营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营 ...
- POJ 3614:Sunscreen 贪心+优先队列
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5378 Accepted: 1864 Descrip ...