POJ 3083_Children of the Candy Corn
题意:
给定迷宫图,求出一个人从入口进,从出口出,所走过的最短路径以及分别沿着左手边和右手边的墙走出迷宫所走过的方格数。
分析:
- bfs求最短路
- 对于沿左右两边的墙走的情况,记录好行走的方向及相对应的左/右边墙的方向坐标
- 注意判断前方和左/右是否为墙,若前方为墙,则进行逆时针旋转,若左/右方不为墙,则应直接向左/右方向走。
- 注意考虑前方和左/右边坐标不在迷宫内的情况。
代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<queue>
using namespace std;
const int maxn =55;
int w, h, cnt = 1;
int ei, ej, si, sj;
char c[maxn][maxn];
int d[maxn][maxn];
typedef pair<int, int> pii;
pii t[4];
map<pii,pii>m[2];
int in(int x, int y)
{
if(0<=x&&x<h&&0<=y&&y<w) return 1;
else return 0;
}
void dfs(int x,int y, pii dir, int a)
{
if(x==ei&&y==ej) return ;
pii temp=m[a][dir];
if(in(x+temp.first, y+temp.second)&&c[x+temp.first][y+temp.second]=='#'){
if(in(x+dir.first, y+dir.second)&&c[x+dir.first][y+dir.second]!='#'&&c[x+dir.first][y+dir.second]!='S') {
// cout<<x+dir.first<<' '<<y+dir.second<<endl;
cnt++;
dfs(x+dir.first, y+dir.second,dir,a);
}else dfs(x, y,m[1-a][dir],a);
}else{
if(in(x+temp.first, y+temp.second)){
// cout<<p.x+temp.first<<' '<<p.y+temp.second<<endl;
cnt++;
dfs(x+temp.first, y+temp.second, temp,a);
}else dfs(x, y,m[1-a][dir],a);
}
return ;
}
void bfs()
{
queue<pii>q;
q.push(make_pair(si, sj));
memset(d, 0,sizeof(d));
d[si][sj]=1;
while(!q.empty()){
pii temp = q.front();q.pop();
if(temp.first == ei&&temp.second == ej) break;
for(int i = 0; i < 4; i++){
int x = temp.first+t[i].first, y = temp.second+t[i].second;
if(in(x, y)&&c[x][y]!='#'&&d[x][y] ==0){
q.push(make_pair(x, y));
d[x][y]=d[temp.first][temp.second]+1;
}
}
}
return;
}
int main (void)
{
int n;scanf("%d",&n);
t[0]=make_pair(-1,0);t[1]=make_pair(0,1);t[2]=make_pair(1,0);t[3]=make_pair(0,-1);
for(int i = 0; i < 4; i++){
m[0][t[i]]=t[(i+3)%4];
m[1][t[i]]=t[(i+1)%4];
}
while(n--){
cnt = 1;
scanf("%d%d",&w,&h);
for(int i = 0; i < h;i++){
scanf("%s",c[i]);
for(int j = 0; j < w; j++){
if(c[i][j]=='S') si = i, sj = j;
if(c[i][j]=='E') ei = i, ej = j;
}
}
dfs(si,sj,t[0],0);
printf("%d ",cnt);
cnt = 1;
dfs(si,sj,t[0],1);
printf("%d ",cnt);
bfs();
printf("%d\n",d[ei][ej]);
}
}
POJ 3083_Children of the Candy Corn的更多相关文章
- 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 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- 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
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11015 Acce ...
- HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Jav ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
随机推荐
- AJPFX关于Java Object类常用方法小总结
java.lang.Object java.lang包在使用的时候无需显示导入,编译时由编译器自动导入. Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类. Object类 ...
- Apache Tomcat 之路(一 基本概念)
关于apache tomcat 基本概念(https://tomcat.apache.org/tomcat-7.0-doc/index.html) 1.tomcat 是servlet/jsp 容器,对 ...
- Kotlin – CharSequence IsNullOrBlank() vs IsNullOrEmpty()
本文摘自:http://blog.farifam.com/2018/01/28/kotlin-charsequence-isnullorblank-vs-isnullorempty/ Koltin p ...
- No-2.注释
01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...
- vue 中slot 的具体用法
子组件 <template> <div class="slotcontent"> <ul> <!--<slot></sl ...
- centos6 磁盘与文件系统管理
一.磁盘管理 磁盘构成 1.圆形磁盘 2.磁盘读取头 3.机械手臂 4.主轴马达 运作原理 数据存储在具有磁性物质的圆形磁盘上,读写操作主要是通过机械手臂上的磁盘读取头来达成,实际运作时,主轴马达让磁 ...
- Jquery 动态添加元素后,获取不到元素对象情况
- c++基础_字母图形
#include <iostream> #include <algorithm> using namespace std; int main(){ ,m=,c; cin> ...
- python第一章计算机基础
第一章 计算机基础 1.1 硬件 计算机基本的硬件由:CPU / 内存 / 主板 / 硬盘 / 网卡 / 显卡 / 显示器 等组成,只有硬件但硬件之间无法进行交流和通信. 1.2 操作系统 操作系统用 ...
- (十)python3 生成器
生成器(generator):在 Python 中,不必创建完整的 list,从而节省大量的空间.一边循环一边计算的机制. 创建一个 generator,有很多种方法.第一种方法很简单,只要把一个列表 ...