poj 3083 Children of the Candy Corn (广搜,模拟,简单)
靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点。
最短路径 用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 (广搜,模拟,简单)的更多相关文章
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- 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 ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- POJ 3083 Children of the Candy Corn 解题报告
最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
随机推荐
- web应用中webapp. root重用问题解决方案
同一个tomcat服务器里面部署两个JavaEE项目,都是用了log4j做日志.并且web.xml里面都监听了日志信息. 启动服务的时候报错. 于是在web.xml添加以下代码: <di ...
- java.util.Vector
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, C ...
- rest介绍
REST介绍 描述 rest即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软 ...
- zedboard 构建嵌入式linux
本文通过五部完成zedboard的嵌入式LINUX搭建,所谓磨刀不五砍材工嘛 1:系统环境搭建 要准备好交叉编译环境 见http://blog.csdn.net/xiabodan/article/de ...
- linux 安装sysstat使用iostat、mpstat、sar、sa(转载)
使用yum安装 #yum install sysstat sysstat的安装包是:sysstat-5.0.5-1.i386.rpm,装完了sysstat-5.0.5-1.i386.rpm后 就会有i ...
- 【Qt】Qt之进程间通信(QProcess)【转】
简述 前几节里,分享了进程通信的几种方式:Windows消息机制.Shared Memory(共享内存),本节讲解下关于进程通信的另外一种方式-QProcess. 简述 命令行参数启动 说明 实现 命 ...
- svn 清空
SVN是目前用得比较多的而且很方便的版本管理体系. 在开发过程中遇到了这样的问题: 有时我们需要一个干净的code版本,没有 .svn 这些文件夹记录的版本传到服务器上使用. 这个时候自己一个个去删除 ...
- Win7开始菜单之【附件】不全解决方案
Win7开始菜单之[附件]不全解决方案 1:打开你的[开始]菜单,转到附件,如果你发现你的附件里的“附件”不是那么全的话,如下图:来吧,我告诉你如何恢复它到最初的模样……哦,或许如果你不急于恢复的话, ...
- sql拆分查询
有这样一个需求: 临时表sql: create table #AA ( ID int, Name nvarchar(20) ) insert #AA select 1,'苏州/上海/温州' union ...
- ARPA
ARPA是英文Advanced Research Projects Agency的缩写,代表美国国防部高级研究计划署.是美国国防部高级研究计划管理局因军事目的而建立的,开始时只连接了4台主机,这便是只 ...