题意:

给定迷宫图,求出一个人从入口进,从出口出,所走过的最短路径以及分别沿着左手边和右手边的墙走出迷宫所走过的方格数。

分析:

  • 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的更多相关文章

  1. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  2. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  3. 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 ...

  4. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  5. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  6. POJ 3083:Children of the Candy Corn

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11015   Acce ...

  7. HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn

    Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Jav ...

  8. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  9. K - Children of the Candy Corn(待续)

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

随机推荐

  1. 架构师细说 NGINX 的配置及优化

    最近感觉很多东西在运用到一定的程度之后,会发现原来是自己了解到的不够.一方面限于实际运用到的不多,一方面可能是因为一开始没有进行全面认识.遂这里搜集整理了一番NGINX. 一.nginx启动和关闭 c ...

  2. 手动配置wamp环境(1)--apache安装与基本操作

    Apache服务器简介: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 安装apac ...

  3. centOS linux 下nginx编译安装详解

    Nginx的官方网站是 www.nginx.org   Nginx的下载地址是:http://nginx.org/en/download.html   由 于官网的设计非常简洁不大气不上档次,所以我们 ...

  4. 【译】x86程序员手册37-第10章 初始化

    Chapter 10 Initialization 第10章 初始化 After a signal on the RESET pin, certain registers of the 80386 a ...

  5. 边框带阴影 box-shadow

    .chosen-container-active .chosen-single { border: 1px solid #5897fb; -webkit-box-shadow: 0 0 5px rgb ...

  6. 07JavaScript数组与字符串对象

    JavaScript数组与字符串对象 5.1.1数组(Array)对象 <script> //声明一个数组并赋值; var arr = new Array("aa",& ...

  7. BZOJ4832: [Lydsy1704月赛]抵制克苏恩 (记忆化搜索 + 概率DP)

    题意:模拟克苏恩打奴隶战对对方英雄所造成的伤害 题解:因为昨(今)天才写过记忆化搜索 所以这个就是送经验了 1A还冲了个榜 但是我惊奇的发现我数组明明就比数据范围开小了啊??? #include &l ...

  8. docker数据卷的管理和使用

    数据卷的使用,数据库可以保证如果容器出现问题但是数据不丢失的作用,比如MySQL/date下的数据 或者Nginx根目录下的index.html 查看数据卷 [root@docker ~]# dock ...

  9. VMware12 download

    下载地址:http://filehippo.com/zh/download_vmware-workstation-pro/ 许可证地址:http://www.zdfans.com/5928.html

  10. Linux从入门到适应(四):Ubuntu 16.04环境下,安装Nvidia驱动,cuda9.2和 cudnn

    在安装深度学习框架之前,cuda和cudnn是必须要提前安装的,现在按照流程而nvidia驱动的版本和cuda版本有这一些对应关系,所以需要按照版本进行安装,现在说一下如何安装: 1 安装nvidia ...