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)地图 ...
随机推荐
- flume+kafka (分区实现 默认单分区) (二)
这篇文章主要在上一篇文章的基础上讲一下 如何自定义flume到kafka的分区 上一节中从下面的地址下载了一个源码 https://github.com/beyondj2ee/flumeng-kafk ...
- 洛谷 P3399 丝绸之路
题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...
- [Guava源码分析]ImmutableCollection:不可变集合
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3888557.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- 真正理解KMP算法
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4403560.html 所谓KMP算法,就是判断一个模式串是否是一个字符串的子串,通常的算法当 ...
- ADO.NET笔记——使用通用数据访问
相关知识: 前面所有示例,君是访问特定的数据库(SQL Server),因此注入SqlConnection.SqlCommand.SqlDataReader.SqlDataAdapter等类名都添加了 ...
- 用JavaScript刷新框架子页面的七种方法
下面以三个页面分别命名为framedemo.html,top.html,button.html为例来具体说明如何做. 其中framedemo.html由上下两个页面组成,代码如下: < !DOC ...
- jqueryGrid 内置的onclickSubmit afterSubmit
$(document).ready(function() { $('#jpgCustomers').jqGrid({ //url from wich data should be requested ...
- 如果在配置中将“system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled”设置为 true,则需要终结点指定相对地址。如果在终结点上指定相对侦听 URI,则该地址可以是绝对地址。若要解决此问题,请为终结点“http://localhost/Service1.svc”指定相对 URI。
问题: 如果在配置中将"system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled"设置为 ...
- WPF之旅(一)- 概述
WPF与之前出现的其他技术相比引入了“内置硬件加速”和“分辨率无关”等创新功能.WPF是一种成熟的技术,它是几个已经发布的.NET平台的一部分,并通过几个版本不断地进行完善(WPF3.0 -> ...
- linux 标准io笔记
三种缓冲 1.全缓冲:在缓冲区写满时输出到指定的输出端. 比如对磁盘上的文件进行读写通常是全缓冲的. 2.行缓冲:在遇到'\n'时输出到指定的输出端. 比如标准输入和标准输出就是行缓冲, 回车后就会进 ...