题意:给一个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)的更多相关文章

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

  2. POJ3083 Children of the Candy Corn(搜索)

    题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...

  3. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  4. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  5. poj3083 Children of the Candy Corn BFS&&DFS

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

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

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

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

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

  8. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  9. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

随机推荐

  1. IIS6、7添加反向代理的步骤

    1.安装requestRouter_amd64.msi和rewrite_x64_zh-CN.msi. 打包下载地址:http://files.cnblogs.com/files/wangwust/ii ...

  2. 快速部署PostgreSQL

    PostgreSQL通常也简称Postgres,是一个关系型数据库管理系统,适用于各种Linux操作系统.Windows.Solaris.BSD和Mac OS X.本文基于CentOS7,使用yum源 ...

  3. Redis环境搭建

    一.准备的安装包 windows虚拟机软件:VMware Workstation Pro 12 linux安装文件:CentOS-7-x86_64-Minimal-1511.iso 远程登录软件:pu ...

  4. 将一个实体转换成 Url 参数的形式 ?a=a&b=b

    function toQueryString(obj) { var ret = []; for (var key in obj) { key = encodeURIComponent(key); va ...

  5. 磨刀不误砍柴工!vs2010快捷键大全

    最常用的快捷键: VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctr ...

  6. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  7. 0_Simple__cudaOpenMP

    在OpenMP的多线程程序中,各线程分别调用CUDA进行计算.OpenMP的简单示例. ▶ 源代码: #include <omp.h> #include <stdio.h> # ...

  8. VUE插件-图片濑加载

    1.  cnpm install vue-lazyload 2.main.js import  vue-lazyload from  'vue-lazyload' Vue.use(vue-lazylo ...

  9. Sping IOC 理解(转)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  10. 玩转 HTML5 下 WebGL 的 3D 模型交并补

    建设性的立体几何具有许多实际用途,它用于需要简单几何对象的情况下,或者数学精度很重要的地方,几乎所有的工程 CAD 软件包都使用 CSG(可以用于表示刀具切削,以及零件必须配合在一起的特征).CSG ...