题意:

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

分析:

  • 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. 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法

    利用Laravel 搭建oauth2 API接口 要求 laravel 5.4以上 安装 $ composer require laravel/passport 在配置文件 config/app.ph ...

  2. java之数据处理,小数点保留位数

    1.返回字符串类型,保留后两位: public static String getRate(Object d) { return String.format("%.2f", d); ...

  3. HTML5应用缓存与Web Workers

    1.什么是应用程序缓存      HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网链接时进行访问. 2.应用缓存的优势      离线浏览   用户可在应用离线时使用它们 ...

  4. flex弹性布局操练2

    上一个是练习的1个内元素的,这次练习两个元素的. ul.box1 { list-style:none; background-color:black; display:flex; justify-co ...

  5. cron - 定期执行指定命令的守护程序 (Vixie Cron)

    总览 cron 描述 Cron 应该由 /etc/rc 或者 /etc/rc.local 启动(译注:有很多发行版与此不同的,如 RedHat6.x 使用 /etc/rc.d/init.d/crond ...

  6. CAD绘制固定矩形标注(网页版)

    js中实现代码说明: function DoFixRectComment() { var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity&q ...

  7. 【计算机网络】2.3 文件传输协议:FTP

    第二章第三节 文件传输协议:FTP 在一个典型的FTP(File Transfer Protocol,文件传输协议)会话中,用户坐在一台主机(本地主机)前面,向一台远程主机传输(或接收来自远程主机的) ...

  8. adb 设备命令

    一.adb 设备命令1.查看机型时,可以使用以下命令$ adb shell getprop ro.product.model 2.如果我们忘记具体系统属性的名字$ adb shell getprop ...

  9. django 标签

    django标签 {% if/for/ifequal/ifnotequal condition %} ...{{ name|first|lower}}{# interpretation:lower t ...

  10. BZOJ 2502 Luogu P4843 清理雪道 最小流

    题意: 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机 ...