这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标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. 单链表每k个节点为一组进行反转(最后不满k个时不反转)

    public class LinkReverse2 { public static Node mhead=null; public static Node mtail=null; public sta ...

  2. Java8新特性-接口中的静态方法与默认方法

    今天上午在读<Effective Java>时,有这样一句话:”接口中“不能有静态方法,于是联想起面试时老是被问接口相关的东西,决定总结一下,谁知道这一总结,就发现了自己知识的一大漏洞.  ...

  3. Es6获取数据

    $(function(){ $.ajax({ //请求方式 type:"GET", //文件位置 url:"js/data.json", //返回数据格式为js ...

  4. Linux—Ubuntu14.0.5安装mongo

    1.安装mongo sudo apt-get install mongo 2.如果遇到找不到安装包运行,那就更新资源列表 sudo apt-get update 3.安装成功会自动运行mongo pg ...

  5. python爬虫07 | 有了 BeautifulSoup ,妈妈再也不用担心我的正则表达式了

    我们上次做了 你的第一个爬虫,爬取当当网 Top 500 本五星好评书籍 有些朋友觉得 利用正则表达式去提取信息 太特么麻烦了 有没有什么别的方式 更方便过滤我们想要的内容啊 emmmm 你还别说 还 ...

  6. 1069. The Black Hole of Numbers

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  7. Shell入门基础

    Shell的Helloworld #!/bin/bash echo "helloworld taosir" 执行方式 方式一:用 bash 或 sh 的相对或绝对路径(不用赋予脚本 ...

  8. Spring 单例模式和多例模式

    1.Spring中的对象默认都是 单例模式. 2.使用 @Scope("prototype") 注解来使对象成为多例模式. 3.通过@Autowired 注入的Service 或者 ...

  9. elasticsearch 权威指南排序阅读笔记(六)

    默认排序 默认查询是通过_source 准确性权重来排序 字段排序 { "query":{ "match":{ "productName": ...

  10. 洛谷 P1692 部落卫队

    P1692 部落卫队 题目描述 原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突.几乎每个居民都有他的仇敌.部落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍, ...