题意:

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

分析:

  • 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. js设计模式-发布/订阅模式

    一.前言 发布订阅模式,基于一个主题/事件通道,希望接收通知的对象(称为subscriber)通过自定义事件订阅主题,被激活事件的对象(称为publisher)通过发布主题事件的方式被通知. 就和用户 ...

  2. bind - 将一个名字和一个套接字绑定到一起

    SYNOPSIS 概述 #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct s ...

  3. 谈谈JVM垃圾回收机制及垃圾回收算法

    一.垃圾回收机制的意义 Java语言中一个显著的特点就是引入了垃圾回收机制,使c++程序员最头疼的内存管理的问题迎刃而解,它使得Java程序员在编写程序的时候不再需要考虑内存管理.由于有个垃圾回收机制 ...

  4. MFC线程获取主窗口句柄

    CWnd* h_q = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄

  5. (独孤九剑)--PHP简介与现况

    (1)为什么学习PHP? 1.好就业: 2.入门简单,学习周期短,两个月即可: 3.学习编程思路,使编程习惯更加规范: 4.大公司直招: 5.处理大并发数据: 6.开源,所以更加安全 (2)PHP是什 ...

  6. 根据截至日期格式获取倒计时&&时间戳转日期格式

    //时间戳转日期格式,传入时间戳必须为数字类型function currentDate(shijianchuo) { var date = new Date(shijianchuo); var y = ...

  7. 零基础入门学习Python(24)--递归:汉诺塔

    知识点 这节课主要讲解用递归的方法,实现汉诺塔的解答 对于游戏的玩法,我们可以简单分解为三个步骤: 1) 将前63个盘子从X移动到Y上. 2) 将最底下的第64个盘子从X移动到Z上. 3) 将Y上的6 ...

  8. token 的生成杂谈

    背景 很多时候我们需要用 token 来作为一些标识, 比如: 一个用户登录后的认证标识. 实现方式 md5 的方式: $v = 1; // 自己定义的 需要hash 的value 值 $key = ...

  9. 18Spring后置通知

    Spring后置通知,和前置通知类似,直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalc ...

  10. mysql数据库导出导入

    MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...