这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标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. Swoole server函数列表(转载)

    swoole_server::__construct swoole_server::set swoole_server::on swoole_server::addlistener swoole_se ...

  2. 【第一课】kaggle初识

    Evernote Export Crowdflower搜索结果相关性 文件和数据描述 train.csv训练数据集包括: id:产品ID查询:使用的搜索词 product_description:完整 ...

  3. __int128的实现

    #include<bitset> #include<algorithm> #include<iostream> #include<string> #in ...

  4. node源码详解(五)

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource5 本博客同步在https://cnodejs.o ...

  5. Git学习总结(12)——多人开发 Git 分支管理详解

    1.前言 在上一篇博客中我们主要讲解了Git 远程仓库,相信大家对远程的Git仓库有一定的了解,嘿嘿.在这一篇博客中我们来在大家讲解一下Git 分支管理,这可以说是Git的又一大特点.下面我们就来学习 ...

  6. 编写App测试用例的关注点

    如何做到测试用例的百分百覆盖一直是测试用例编写过程中的难点,首先在测试时我们经常会遇见一些常见的bug,那么我们可以在编写测试用例时考虑到这些点.    一:关于业务逻辑               ...

  7. Porting from Oracle to MySQL

    A potential customer asked my about porting her application from Oracle Database to MySQL. I always ...

  8. [bzoj3062][Usaco13Feb]Taxi_贪心

    Taxi bzoj-3062 Usaco13Feb 题目大意:有n个奶牛想坐出租车.第i头奶牛在起点a[i]等候,想坐出租车到b[i].Bessie从0出车,车上只能坐一头奶牛.她必须完成所有奶牛的要 ...

  9. 完毕port模型

    完毕port模型过程例如以下: 1.调用CreateIoCompletionPort函数创建完毕port. HANDLE CompletionPort=CreateIoCompletionStatus ...

  10. HDU 4617

    题目多读几次就明白了.主要是求异面直线的距离,然后用距离和两圆半径之和作比较. 空间直线的距离d=|AB*n| / |n| (AB表示异面直线任意2点的连线,n表示法向量,法向量为两条异面直线方向向量 ...