题目

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

最短路径 用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. Template_5模板拾遗1

    1,typename和class模板参数作为类的时候只能用classtemplate<typename T, template<typename ELEM> class CONT = ...

  2. ubuntu 常用参数设置

        在Linux下,对于参数的设置,一般来说,都遵循这个规律.每一个功能程序,一定对于一个对于名字的配置文件.     涉及到多用户的功能配置,一定有一个全局的配置文件,对所有用户都生效,而每个用 ...

  3. jacob 给word加印的功能

    花了两天时间,参考了一些资料,总算是处理好了这样一个技术点. 关键的心得如下: 使用jacob,重点不是jacob本身,而是office的一些API资料.比如需要知道光标的移动, 包括上下左右的mov ...

  4. <Linux下echo指令>

    echo这个命令我们最常见的还是在shell脚本中的使用,if语句,for语句,case语句....这些都不是对echo命令的全面了解.下面还有很多其他echo的参数: 来自本人的日常生活,和对资料查 ...

  5. jQuery插件使用大全

    1.jQuery datepicker日历插件使用说明 http://wenku.baidu.com/view/12804e1e59eef8c75fbfb3e3 2.jqueryFileUpload插 ...

  6. 通信协议之HTTP,UDP,TCP协议

    1.UDP,TCP,HTTP之间的关系 tcp/ip是个协议组,它可以分为4个层次,即网路接口层,网络层,传输层,以及应用层, 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协 ...

  7. 玄机宝盒v1.6.1.1

    最新版本:玄机宝盒v1.6.1.1 玄机宝盒v1.6.1.1 04-14/2016 给你的将是无与伦比的体验http://bbs.msdn5.com/thread-15-1-1.html(出处: 玄机 ...

  8. php 文件上传一例简单代码

    1.程序文件 <?php //判断临时文件存放路径是否包含用户上传的文件 if(is_uploaded_file($_FILES["uploadfile"]["tm ...

  9. Linq to Entities

    首先要添加一个ADO.NET实体数据模型 添加一个Entities 对象,其用法和linqtosql类似例如: StudentInfoEntities2 entity = new StudentInf ...

  10. IO流的异常处理

    在IO流的异常处理时应该注意以下几点: 1.在外边建立引用,在Try内进行初始化(FileWriter fw = null;) 2.文件的路径使用必须是双斜杠,转义(fw = new FileWrit ...