最短用BFS即可。关于左手走和右手走也很容易理解,走的顺序是左上右下。

值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样。

所以写一个左手走法就好了。贴代码,0MS

#include <cstdio>
#include <cstring>
#include <deque>
using namespace std; int mp[][];
const int DIR[][]={ {,-},{-,},{,},{,} }; bool flag;
void DFS(int x,int y,int dir,int step)
{
if(mp[x][y])
{
flag=true;
printf("%d ",step);
}
for(int i=,a,b;i<=;i++)
{
if(mp[ a=x+DIR[(i+dir)%][] ][ b=y+DIR[(i+dir)%][] ]>=)
DFS(a,b,(dir+i)%,step+);
if(flag) return;
}
} struct Point
{
int x,y;
int step;
} p,q; void BFS(int x,int y)
{
p.x=x;
p.y=y;
p.step=;
mp[x][y]=-;
deque<Point> dq;
dq.push_back(p); while()
{
p=dq.front();
dq.pop_front(); for(int i=;i<;i++)
{
q.step=p.step+;
q.x=p.x+DIR[i][];
q.y=p.y+DIR[i][];
if(mp[q.x][q.y]==)
{
mp[p.x][q.y]=-;
dq.push_back(q);
}
if(mp[q.x][q.y]>)
{
dq.clear();
printf("%d",q.step);
return;
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(mp,-,sizeof(mp)); int w,h;
int stax,stay,endx,endy;
char str[]; scanf("%d%d",&w,&h);
for(int i=;i<=h;i++)
{
scanf("%s",str+);
for(int k=;k<=w;k++)
{
if(str[k]=='.')
mp[i][k]=;
else if(str[k]=='S')
{
stax=i;
stay=k;
}
else if(str[k]=='E')
{
endx=i;
endy=k;
}
}
} flag=false;
mp[stax][stay]=;
mp[endx][endy]=;
DFS(stax,stay,,); flag=false;
mp[stax][stay]=;
mp[endx][endy]=;
DFS(endx,endy,,); mp[endx][endy]=;
BFS(stax,stay);
puts("");
}
}

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. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  4. poj 3083 Children of the Candy Corn(DFS+BFS)

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

  5. 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 ...

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

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

  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. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

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

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

随机推荐

  1. C#基础(四)——ref与out的区别

    1.ref传进去的参数必须进行初始化,out不必int i;SomeMethod( ref i );//语法错误SomeMethod( out i );//通过 2.ref传进去的参数在函数内部可以直 ...

  2. Magento Api 记录

    magento api 首次接触 (-) /** * magento Api 身份验证 调用示例 * Example of simple product POST using Admin accoun ...

  3. wamp——利用phpmyadmin修改数据库密码

    一.wamp版本         我采用的是 WampServer2.2a-x32.exe 这个版本的安装程序. 二.问题描述         安装wamp后,mysql数据库默认密码为空,但是由于某 ...

  4. phpmyadmin 4.x 版本无法看到登录框的处理

    由于个人dreamhost即将到期问题,购买了一台VPS. 配置了一个CentOS 6.4 Linux 服务器,用Nginx+php-fpm搭建的环境. 这些都是废话,下面是重点: 当搭建后配置php ...

  5. 图论(四)------非负权有向图的单源最短路径问题,Dijkstra算法

    Dijkstra算法解决了有向图G=(V,E)上带权的单源最短路径问题,但要求所有边的权值非负. Dijkstra算法是贪婪算法的一个很好的例子.设置一顶点集合S,从源点s到集合中的顶点的最终最短路径 ...

  6. 简单的powershell 批量生成身份证复印件

    用Powshell 脚本批量完成,代码比较简单,出生日期,图片我都没有改,图片用于测试,附件. cls; $path = Split-Path -Parent $MyInvocation.MyComm ...

  7. CentOS 6.6x64下编译gcc-4.7.4

    最近使用老版本的gcc发现一些问题,于是想尝试升级. 看了一些教程之后进行尝试,发现各类教程均会有一些小问题,于是在此记录一下本人的过程. 编译过程中参考的文章有如下几篇,在此表示感谢: http:/ ...

  8. JUnit测试工具在项目中的用法

    0:33 2013/6/26 三大框架整合时为什么要对项目进行junit测试: |__目的是测试配置文件对不对,能跑通就可以进行开发了 具体测试步骤: |__1.对hibernate进行测试 配置hi ...

  9. MySQL 5.7 启用查询日志

    MySQL版本:5.7 新版本的 my.ini 文件改动了,导致原先启用查询日志的方法不再适用 新版本的启用方法如下: 1. 修改 C:\ProgramData\MySQL\MySQL Server ...

  10. <一> jQuery 简单介绍

    jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. 可以通过下面的标记把 jQuery 添加到网页中: <head> <script ty ...