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)地图 ...
随机推荐
- springmvc学习(三)
第一点---------使用 @RequestMapping 映射请求• Ant 风格资源地址支持 3 种匹配符:?:匹配文件名中的一个字符 *:匹配文件名中的任意字符 **:** 匹配多层路径 @R ...
- struts2自定义拦截器与cookie整合实现用户免重复登入
目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...
- asp.net导出excel较为简约的代码
ds为数据源 Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachme ...
- CentOS7 firewall的使用
# 查看区域 firewall-cmd --get-zones # 查看默认区域 firewall-cmd --get-default-zone # 给区域添加永久性服务 firewall-cmd - ...
- bat文件的妙用1-一键开启所有开发软件
每天早上来的第一件事情,就是打开电脑,然后开一堆的软件 1.wamp 开发环境 2.钉钉 通讯工具 3.PHPstrom 开发工具 4.nodejs.bat Nodejs的扩展(node D:/w ...
- Array.prototype.map()详解
今天在地铁上看到这样一个小例子: ["1","2","3"].map(parseInt); 相信很多人和我一样,觉得输出的结果是[1,2,3 ...
- HttpWebRequest中的SendChunked
MSDN上说:When SendChunked is true, the request sends data to the Internet resource in segments. The In ...
- sublime主题推荐
PS:之前在CSDN上写的文章,现在转到博客园~ 寒假的时候发现一个sublime主题,我觉得很赞哦~~推荐给大家~~ 下载方式 step1:ctrl+shift+p 调出command palet ...
- Oracle 分析函数 "ORA-30485: 在窗口说明中丢失 ORDER BY 表达式"
跟顺序有关的几个分析函数row_number.rank.dense_rank.lead和lag的over窗口里,都必须有order_by_clause.其他几个如:first_value.last_v ...
- SDC(5)–FPGA系统级同步输入的约束
此时计算最大/最小输入延时时,是不应该考虑FPGA本身的tSU/tH的,因为数据进来之后不一定马上送到DFF. 例如: 此时约束如下: