POJ 3083 Children of the Candy Corn 解题报告
最短用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 解题报告的更多相关文章
- 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 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- 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. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
随机推荐
- Ant 入门
参考: Ant官网 http://ant.apache.org/ 轻量级java ee企业应用实战(李刚) Ant当前版本1.9.6 Ant基于Java 配置环境变 ...
- 07_XPath_02_常用语法
[工程截图] [person.xml] <?xml version="1.0" encoding="UTF-8"?> <students> ...
- 《APUE》第四章笔记(1)
1.引言 本章介绍文件系统的特征和文件的性质.从stat函数开始,逐个说明stat结构的每一个成员以了解文件的所有属性.在此过程中,还将会说明修改这些属性的各个函数,并更详细地查看UNIX文件系统的结 ...
- Linux---文件类型及权限操作
文件类型: 用ls命令查看目录下所属文件时,每行的第一个字母标识着文件对应的文件类型 '-':代表普通文件 'd':代表目录 'c':字符设备文件 'b':块设备文件 's':套接字文件 'l':符号 ...
- 目前IT行业的几个大方向
我简单总结了一下目前it行业的8大方向: 1.嵌入式开发 传统的arm linux开发.新兴的智能硬件.物联网等技术的发展,都让整个方向成为热门领域. 2.游戏开发 cocos2d-x.uni ...
- 算法之合并排序(mergeSort)
合并排序算法在结构上是递归的,采用分治策略:就是将原有的问题划分为 n 个规模较小但结构与原问题相似的子问题,递归地解决这些子问题,然后合并其结果,就得到原问题的解. 合并排序的模式一般如下: 1.分 ...
- 【转】c#文件操作大全(一)
1.创建文件夹//using System.IO;Directory.CreateDirectory(%%1); 2.创建文件//using System.IO;File.Create(%%1); 3 ...
- PHP学习笔记(3) - 奇怪的class与autoload
PHP的class与其他语言有很多不同点.PHP允许很奇葩的在静态方法中调用实例方法,提供了关键字self和static用于访问类自身的静态成员.self永远是指当前的类,而static则可能会变成指 ...
- node.js&mongodb&express 搭建个人博客系统
源码参见于 https://github.com/njaulj/iliujun
- java创建多线程(转载)
转载自:Java创建线程的两个方法 Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对 ...