这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标)。其他方向也可以这样确定。通过上一步方向可以确定下一步应该从哪个方向开始搜。比如说,是向北走的,就必须先搜西,西不可以走,再搜北,如果北还不可以走,再搜东,最后才是南。其他方向的情况也可以这样推出来。最后走到E点完成了。广搜就是最基础的广搜。这道题做了将近10个小时。中途曾几次准备放弃,但最后还是坚持做完了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std; char maze[40][40];
int n,m,ex,ey,sx,sy,flag,s,cx,cy,df;
int dlx[7]={0,1,0,-1,0,1,0};
int dly[7]={1,0,-1,0,1,0,-1};
int drx[7]={0,-1,0,1,0,-1,0};
int dry[7]={1,0,-1,0,1,0,-1};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int isin(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m;
}
void lfs(int i,int j)
{
int x,y,k;
if(flag) return ;
if(i==ex&&j==ey) {flag=1;}
else
for(k=df;k<7&&!flag;k++)
{
cx=dlx[k];
cy=dly[k];
x=i+dlx[k];
y=j+dly[k]; if(isin(x,y)&&maze[x][y]!='#'){
s++;
if(cx==1) df=0;
//往南的,先搜东
else if(cx==-1) df=2;
//往北的,先搜西
else if(!cx){
if(cy==1) df=3;
//往东的,先搜北
else df=1;
}
//printf("(%d %d) ",x,y);
lfs(x,y);
}
}
}
void rfs(int i,int j)
{
int x,y,k,d;
if(flag) return ;
if(i==ex&&j==ey) {flag=1;}
else
for(k=df;k<7&&!flag;k++)
{
cx=drx[k];
cy=dry[k];
x=i+drx[k];
y=j+dry[k]; if(isin(x,y)&&maze[x][y]!='#'){
s++;
if(cx==1) df=2;
//往南的,先搜西
else if(cx==-1) df=0;
//往北的,先搜东
else if(!cx){
if(cy==1) df=3;
//往东的,先搜南
else df=1;
}
//printf("(%d %d) ",x,y);
rfs(x,y);
}
}
}
struct node
{
int dis,row,col;
};
queue<node> q;
void bfs()
{
int x,y,k,d,i,j;
node t;
while(!q.empty())
{
t=q.front();
q.pop();
d=t.dis;
i=t.row;
j=t.col;
for(k=0;k<4;k++)
{
x=i+dx[k];
y=j+dy[k];
if(maze[x][y]!='#'&&isin(x,y)){
if(x==ex&&y==ey){
s=d+1;
return ;
}
else{
t.dis=d+1;
t.row=x;
t.col=y;
q.push(t);
maze[x][y]='#';
}
} }
}
}
int main()
{
int ca,i,j;
scanf("%d",&ca);
while(ca--)
{
scanf("%d%d",&m,&n);
getchar();
for(i=0;i<n;i++) gets(maze[i]);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(maze[i][j]=='S'){
sx=i;sy=j;
maze[sx][sy]='#';
}
else if(maze[i][j]=='E'){
ex=i;ey=j;
maze[ex][ey]='.';
}
}
}
flag=0;
s=1;
if(!sy) df=3;
else if(!sx) df=0;
else if(sy==m-1) df=1;
else if(sx==n-1) df=2;
lfs(sx,sy);
if(!sy) df=3;
else if(!sx) df=0;
else if(sy==m-1) df=1;
else if(sx==n-1) df=2;
cout<<s<<' ';
flag=0;
s=1;
rfs(sx,sy);
cout<<s<<' ';
while(!q.empty()) q.pop();
node p;
p.dis=1;
p.row=sx;
p.col=sy;
q.push(p);
s=1;
bfs();
cout<<s<<endl;
}
return 0;
}

  

poj3083 Children of the Candy Corn 深搜+广搜的更多相关文章

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

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

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

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

  3. POJ3083 Children of the Candy Corn(Bfs + Dfs)

    题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...

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

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

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

  6. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  7. POJ 3083:Children of the Candy Corn

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

  8. Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏

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

  9. POJ 3083 Children of the Candy Corn bfs和dfs

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

随机推荐

  1. 【转载】Jsp页面传Json数据到服务端,转对象或集合进行数据处理

    需求:1.将页面数据带到服务端并转成对象,2.将页面的集合数据带到服务端转List实现:用ajax请求传递数据,数据格式为json JS方法: testJsonMethod = function(){ ...

  2. SPLAY or SPALY ?

    写在前面: 由我们可爱的Daniel Sleator和Robert Tarjan提出的一种数据结构,平衡树的一种,本质是二叉树. 至于到底是splay还是spaly,我认为可能splay更对一些 毕竟 ...

  3. Python 集合 day3

    集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,集合用{},里面是一个一个元素,不同于key-value形式的字典: 注意:创建一个空集合必须用 s ...

  4. 44.mapping下的数据结构

    主要知识点 1.了解es核心的数据类型 2.了解es默认的mapping方式 3.查看mapping     1.核心的数据类型     string,text byte, short,integer ...

  5. AnimationEvent事件问题

    AnimationEvent事件问题 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/deta ...

  6. 【codeforces 797D】Broken BST

    [题目链接]:http://codeforces.com/contest/797/problem/D [题意] 给你一个二叉树; 然后问你,对于二叉树中每个节点的权值; 如果尝试用BST的方法去找; ...

  7. Nginx学习总结(1)——Nginx入门简介

    本文主要介绍一些Nginx的最基本功能以及简单配置,但不包括Nginx的安装部署以及实现原理.废话不多,直接开始. 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文 ...

  8. (9)使用JdbcTemplate【从零开始学Spring Boot】

    整体步骤: (1)   在pom.xml加入jdbcTemplate的依赖: (2)   编写DemoDao类,声明为:@Repository,引入JdbcTemplate (3)   编写DemoS ...

  9. 百度之星2014资格赛 1003 - Xor Sum

    先上代码: Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  10. 0622centos下coreseek安装及使用方法

    Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索.论坛 ...