题意:

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

分析:

  • 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. html5改良的input元素的种类

    1.url类型.email类型.date类型.time类型.datetime类型.datetime-local类型. month类型.week类型.number类型.range类型.search类型. ...

  2. 安装JPype时出现的 Unable to find vcvarsall.bat

    解决方案,在网上找到的,mark一下,亲测有效 C:/Python31/Lib/distutils目录下的msvc9compiler.py中 修改MSVCCompiler函数:vc_env = que ...

  3. Eclipse打包多渠道包(库工程版)

    请先移步多渠道打包http://www.cnblogs.com/bhm666/p/6438776.html 自从上次使用了Gradle打渠道包后,遇到了各种各样的问题,不过也是小问题,仍然在几个项目上 ...

  4. MySQL——sql注入

    https://blog.csdn.net/lin_tuer/article/details/54809330 https://github.com/mysqljs/mysql#escaping-qu ...

  5. swift 语言评价

    杂而不精,一团乱麻!模式乱套,不适合作为一门学习和研究语言. 谢谢 LZ 介绍,看完之后更不想用 Swift 了.从 C++那里抄个 V-Table 来很先进嘛?别跟 C++一样搞什么 STL 就好了 ...

  6. laravel学习:容器绑定与解析

    1.在服务容器中注册类(bind) $this->app->bind('sender','MailSender');//$this->app成为服务容器.   2.从服务容器生成类( ...

  7. jQuery 点击 星星评分

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java SE、Java EE、Java ME 三者区别

    现在一个个来分析 1. Java SE(Java Platform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 ...

  9. 小b重排字符串

    2485 小b重排字符串 2 秒 262,144 KB 5 分 1 级题   小b有一个字符串S,现在她希望重排列S,使得S中相邻字符不同. 请你判断小b是否可能成功. 样例解释:将"aab ...

  10. 获取当前时间(日期格式) && 获取当前加一年的时间(日期格式)

    获取当前时间,日期格式function currentDate() { var date = new Date(); var y = date.getFullYear(); var m = date. ...