POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S、E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以S的起点为当前所对着的路径为正方向,如果正方向的左边能走的话,就走左边,不能就按正方向走,不行的话就就往回走,如此反复,记录步数,并输出,第二个整数也是如此,只不过搜的方向改成正方向的右边。第三个就是最短路,
分析:前两个用DFS求出,最短路直接BFS解决,,
单就沿着左走看一下:
当前方向 检索顺序
Sx=n-1 ↑ : ← ↑ → ↓
Sy=0 → : ↑ → ↓ ←
Sx=0 ↓ : → ↓ ← ↑
Sy=n-1 ← : ↓ ← ↑ →
如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。
这个题的麻烦处理就在于怎么按什么样的顺序dfs
在网上搜题解感觉这种想法特别好,不繁琐,代码还简洁
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; int dx[]={,-,,};
int dy[]={-,,,}; int dl[][]={{,-},{-,},{,},{,}};
int dr[][]={{,},{-,},{,-},{,}}; int sx,sy,ex,ey,n,m;
char G[][]; struct node
{
int x,y,s;
}; int dfs(int x,int y,int d,int step,int dir[][])
{
for(int i=;i<;++i)
{
int j=((d-+)%+i)%;
int nx=x+dir[j][];
int ny=y+dir[j][];
if(nx==ex&&ny==ey)
return step+;
if(nx<||ny<||nx>n||ny>m||G[nx][ny]=='#')
continue;
return dfs(nx,ny,j,step+,dir);
}
} int BFS(int sx,int sy)
{
bool vis[][];
memset(vis, false, sizeof(vis));
queue<node>q;
node a;
a.x=sx,a.y=sy,a.s=;
q.push(a);
vis[sx][sy]=true;
while(!q.empty())
{
node p=q.front();
q.pop();
if(p.x==ex&&p.y==ey)
return p.s;
node p1;
for(int i=;i<;++i) {
p1.x=p.x+dx[i];
p1.y=p.y+dy[i];
p1.s=p.s+;
if(p1.x<||p1.x>n||p1.y<||p1.y>m||vis[p1.x][p1.y])
continue;
if(G[p1.x][p1.y]!='#')
{
vis[p1.x][p1.y]=true;
q.push(p1);
}
}
}
return -;
} int main()
{
int T,d1,d2;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=;i<n;++i)
{
scanf("%s",G[i]);
for(int j=;j<m;++j)
{
if(G[i][j]=='S')
{
sx=i;
sy=j;
}
else if(G[i][j]=='E')
{
ex=i;
ey=j;
}
}
}
if(sx==)
{
d1=;
d2=;
}
else if(sx==n-)
{
d1=;
d2=;
}
else if(sy==)
{
d1=;
d2=;
}
else if(sy==m-)
{
d1=;
d2=;
}
printf("%d ",dfs(sx,sy,d1,,dl));
printf("%d ",dfs(sx,sy,d2,,dr));
printf("%d\n",BFS(sx,sy));
}
return ;
}
POJ3083 Children of the Candy Corn(Bfs + Dfs)的更多相关文章
- POJ-3083 Children of the Candy Corn (BFS+DFS)
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...
- POJ3083 Children of the Candy Corn(搜索)
题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- POJ 3083:Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- poj3083 Children of the Candy Corn 深搜+广搜
这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
随机推荐
- 使用Aspose.Cells利用模板导出Excel(C#)
前言 随着互联网的流行,web项目逐渐占据主流.我相信大部分人开发项目的过程中都写过上传以及导出Excel和Word的功能,本文仅讨论导出Excel.C#中有很多第三方组件支持导出Excel,比如:N ...
- ip完整验证详情
不想跳坑就看一下 之前一直不太会写正则表达式,很多要用到正则表达式的都直接百度,像上次要用正则表达式验证是否是合法的ip地址,然后就上网找,结果就是没找到一个对的,今天就为大家贡献一下,写个对的,并做 ...
- LeetCode 119. Pascal's Triangle II (杨辉三角之二)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- H5前端上传文件的几个解决方案
目前,几个项目中用到了不同的方法,总结一下分享出来. 第一种,通过FormData来实现. 首先,添加input控件file. <input type="file" name ...
- EJBCA安装教程+postgresql+wildfly10
1. 安装环境说明 笔者在本机的虚拟机下进行的安装,数据库已经装好了的. ubuntu16.04 x64 postgresql:9 wildfly10 2. 安装前准备 下载必要软件包(直接到官网下载 ...
- 关于CSS3 object-position/object-fit属性的使用
object-position/object-fit属性一般用在替换元素上. 什么叫替换元素? 不是所有元素都叫"替换元素".在CSS中,"替换元素"指的是: ...
- Javascript的RegExp对象(转载自网络)
正则表达式是一个描述字符模式的对象. JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. '***************** ...
- CodeForces - 846F Random Query(期望)
You are given an array a consisting of n positive integers. You pick two integer numbers l and r fro ...
- JAVASE高级2
反射概述 什么是反射? 反射的的概念是有smith1982年首次提出的,zhuy主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. JAVA反射机制是运行状态中,对于任意一个类,都能够知道这 ...
- linux-less
linux-less less是more命令的进化版.拥有与more命令一样的向前向后向下向上的数据查看功能,同时less还可以在查看内容中进行快速查找,关于数据向上向下操作,可以看看这个http:/ ...