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)地图 ...
随机推荐
- Dorado事件的参数
onClick,onSuccess 事件一般只有2个参数(self,arg),其实参数是可以添加的.可以把控件的ID直接放到参数里面来,然后在事件编辑里直接通过ID作控件对象,直接设值就好了,不要输入 ...
- vbscript multiple line syntax
Vbscript 如何将输出内容换行? ' VbCrLf represetns Carriage return–linefeed combination, for more information s ...
- ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused
工具/原料:Ubuntu 在采用scp在不同机器之间进行文件拷贝时出现标题所示的错误,原因可能是: 1.sshd 未启动 2.sshd 未安装 3.防火墙 4需重新启动ssh 服务 查看sshd进程方 ...
- 使用json方式实现省市两级下拉级联菜单[原创]
本文为博主原创,转载请注明. 首先看一下实现后的效果图: 当然,要完成这个实验,mysql必须与数据库连接,这里选用navicat for mysql这款软件,它与mysql的契合度是很高的,配置环境 ...
- Swift 学习笔记1
最近在看Swift,努力在看相关的文档以及书籍,因为Swift3.0的更新,以及它开源了,所以打算写一些关于Swift的相关文章.让Swift能够更好的被我理解
- Sublime Text 3插件安装方法
安装Sublime Tex 3t插件的方法: 按快捷键Ctrl + ~ 调出console 粘贴以下代码到console并回车: import urllib.request,os; pf = 'Pac ...
- div重叠不变形
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- sbrk and coreleft
一.sbrk 函数来源:TC2.0.Linux 函数名: sbrk 功 能: 增加程序可用数据段空间,增加大小由参数 incr决定 . 返回值:函数调用成功返回一指针,指向新的内存空间.函数调用失败则 ...
- NGUI系列教程四(自定义Atlas,Font)
今天我们来看一下怎么自定义NGUIAtlas,制作属于自己风格的UI.第一部分:自定义 Atlas1 . 首先我们要准备一些图标素材,也就是我们的UI素材,将其导入到unity工程中.2. 全选我们需 ...
- 原生 JavaScript 图片裁剪效果
图片裁剪程序效果如下,可鼠标操作. 拖动左边小方框时在右侧实时显示对应的裁剪图片,同时左侧的拖动框里图片完全显示,拖动框外部图片模糊显示.8个控制点可以对显示区域大小进行控制. HTML 和 CS ...