题目

靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点。

最短路径 用bfs。

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 110
char map[MAXN][MAXN];
int n,m;
int xx[]={,,,-};
int yy[]={,,-,};
struct tt
{
int x,y,step;
}; int bfs(int x,int y,int x1,int y1)
{
int ans=,i;
tt front,rear,temp;
queue<tt>q;
while(!q.empty())
q.pop();
front.x=x,front.y=y,front.step=;
ans=;
q.push(front);
i=;
while(!q.empty())
{
temp=q.front();
if(temp.x==x1&&temp.y==y1)return ans;
q.pop();
ans++;
i=(i+)%;
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
q.push(rear);
}
else
for(;;i=(i+)%)
{
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
q.push(rear);
break;
}
}
}
return ans;
} int bfs1(int x,int y,int x1,int y1)
{
tt front,rear,temp;
queue<tt>q;
while(!q.empty())
q.pop();
front.x=x,front.y=y,front.step=;
q.push(front);
map[x][y]='#';
while(!q.empty())
{
temp=q.front();
if(temp.x==x1&&temp.y==y1)return temp.step;
q.pop();
for(int i=;i<;i++)
{
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
rear.step=temp.step+;
q.push(rear);
map[rear.x][rear.y]='#';
}
}
}
return ;
}
int main()
{
int i,t,j,sx,sy,ex,ey,a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=;i<n;i++)
{
scanf("%s",map[i]);
for(j=;j<m;j++)
{
if(map[i][j]=='S')
{
sx=i;sy=j;
}
else if(map[i][j]=='E')
{
ex=i;ey=j;
}
}
}
a=bfs(sx,sy,ex,ey);
b=bfs(ex,ey,sx,sy);
c=bfs1(sx,sy,ex,ey);
printf("%d %d %d\n",a,b,c);
}
return ;
}

poj 3083 Children of the Candy Corn (广搜,模拟,简单)的更多相关文章

  1. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

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

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

  3. poj 3083 Children of the Candy Corn

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

  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)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  6. POJ:3083 Children of the Candy Corn(bfs+dfs)

    http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...

  7. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  8. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

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

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

随机推荐

  1. Ajax乱码问题

    Ajax中文乱码问题   1.js代码 var userName=$("#userName").val(); var url = "user/login.action?u ...

  2. iOS 系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...

  3. 打造简单实用的Thinkphp分页样式(Bootstrap版本)

    先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...

  4. linux的7种运行级别

    Linux有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多用户 ...

  5. 免费GIT托管

    http://www.gitcentral.com http://www.projectlocker.com http://gitfarm.appspot.com http://code.google ...

  6. .Net中的Socket通讯

    .NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...

  7. php正则表达式总结第1弹

    介绍几个我用到的php正则表达式 1. 一篇文章的链接,我需要去掉以 /hotels/打头的链接,可用下面正则 $content = preg_replace('/<a(.*?)href=&qu ...

  8. jQuery取值相加

    实在是太菜了. 这样一个需求: 计算两个text中的值的和,引发了对jQuery中类型转换的知识软肋. 在网上找到了才知道:http://zhidao.baidu.com/link?url=ujw88 ...

  9. hdu 5690 2016"百度之星" - 初赛(Astar Round2A) All X 快速二次幂 || 寻找周期

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5690 题意:m个数字全为x mod k ?= c;其中m <= 1010,0 < c,k ...

  10. vs2010的11个调试技巧和方法

    调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...