这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标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. redis客户端连接到服务器的步骤

    和大多数客户端连接到服务器一样,redis-cli连接到服务器也主要分为两个阶段,请求连接阶段和数据传送阶段.具体来讲redis-cli做的事情有: 1.以socket方式建立连接: 2,选择相应的数 ...

  2. tomcat多实例的部署

    解压部署tomcat程序创建2个实例的工作目录mkdir -p /usr/local/tomcat8_instance/tomcat1mkdir -p /usr/local/tomcat8_insta ...

  3. PKU 1019 Number Sequence(模拟,思维)

    题目 以下思路参考自discuss:http://poj.org/showmessage?message_id=176353 /*我的思路: 1.将长串数分成一个个部分,每个部分是从1到x的无重复的数 ...

  4. 关于js开发中保留小数位计算函数(以向上取整或向下取整的方式保留小数)

    前端工作中经常遇到数字计算保留小数问题,由于不是四舍五入的方式不能使用toFixed函数,本文采用正则表达式匹配字符串的方式,解决对数字的向上或向下保留小数问题: 1.向上保留小数(只要目标小数位后有 ...

  5. [POI2005]BAN-Bank Notes

    [POI2005]BAN-Bank Notes POI真好玩.. 如果没有记录方案的话就是一个简单的二进制或单调队列优化多重背包的问题. 但是非常难受的是要记录方案. 而且空间只给了\(64MB\), ...

  6. ajax跨域 (转)

    题纲 关于跨域,有N种类型,本文只专注于ajax请求跨域(,ajax跨域只是属于浏览器”同源策略”中的一部分,其它的还有Cookie跨域iframe跨域,LocalStorage跨域等这里不做介绍), ...

  7. BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)

    手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ...

  8. 利用Socket 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈

    首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下. 错误的代码如下: package com.TCP.java; import java.io.File; impor ...

  9. 转载 - KMP算法祥解

    出处:http://www.cppblog.com/oosky/archive/2006/07/06/9486.html KMP字符串模式匹配详解 来自CSDN     A_B_C_ABC 网友 KM ...

  10. javascript的函数、创建对象、封装、属性和方法、继承

    转自原文javascript的函数.创建对象.封装.属性和方法.继承 一,function 从一开始接触到js就感觉好灵活,每个人的写法都不一样,比如一个function就有N种写法 如:functi ...