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 ...
随机推荐
- Elasticsearch学习入门
一.关于Elasticsearch 1.特点 Elasticsearch基于全文搜索引擎 Apache Lucene ,由Java开发而来,面向API进行搜索, Restful 风格,分布式文件存储. ...
- Python测试进阶——(6)Bash脚本启动Python监控程序并传递PID
用HiBench执行Hadoop——Sort测试用例,进入 /HiBench-master/bin/workloads/micro/sort/hadoop 目录下,执行命令: [root@node1 ...
- CentOS7 部署 Jupyter Notebook
socket.error: [Errno 99] Cannot assign requested address 修改这几处: 1.hostname 参考:https://www.cnblogs.co ...
- Centos上安装FastDFS
更新yum源 cd /etc/yum.repos.d wget http://mirrors.aliyun.com/repo/Centos-7.repo yum update 安装gcc(编译时需要) ...
- delphi对ZIP解压
Delphi 对GZIP解压 作者:admin 来源:未知 日期:2010/5/9 13:08:46 人气:获取失败 标签: QQ空间新浪微博腾讯微博腾讯朋友QQ收藏百度空间百度贴吧更多0 呵呵,终于 ...
- php+ajax 实现无限树列表
首先介绍我实现的是xhprof插件的日志转为无限树状图,先看效果图: 废话不多说,直接看代码:(辛辛苦苦敲了好久才搞定,逻辑比较多,新手多揣摩) 控制器: 1 <?php 2 3 namespa ...
- python Scipy积分运算大全(integrate模块——一重、二重及三重积分)
python中Scipy模块求取积分的方法: SciPy下实现求函数的积分的函数的基本使用,积分,高等数学里有大量的讲述,基本意思就是求曲线下面积之和. 其中rn可认为是偏差,一般可以忽略不计,wi可 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-fast-forward
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 在excel表格里,为所有数字添上绿色小三角
在excel表格里,为所有数字添上绿色小三角的方法有4种: 1. 为一个单元格添加:直接在单元格里添加一个英文的逗号 2. 为一列数据添加:选中要添加绿色小三角的列,选择 数据-->分列--&g ...
- HDU1007 Quoit Design掷环游戏
Quoit Design 看懂题意以后发现就是找平面最近点对间距离除以2. 平面上最近点对是经典的分治,我的解析 直接上代码 #include<bits/stdc++.h> using n ...