poj3083 Children of the Candy Corn 深搜+广搜
这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标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 深搜+广搜的更多相关文章
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...
- POJ3083 Children of the Candy Corn(搜索)
题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...
- 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 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- POJ 3083:Children of the Candy Corn
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11015 Acce ...
- 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 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
随机推荐
- PAT_A1072#Gas Station
Source: PAT A1072 Gas Station (30 分) Description: A gas station has to be built at such a location t ...
- 【剑指Offer】28、数组中出现次数超过一半的数字
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如:输入如下所示的一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...
- flex记忆
._rebate { display: -webkit-box; display: -moz-box; display: -webkit-flex; display: -moz-flex; displ ...
- 回文词(Palindromes, UVa401)
输入一个字符串,判断它是否为回文串以及镜像串.输入字符串保证不含数字0. 所谓 回文串,就是反转以后和原串相同,如abba和madam. 所谓镜像串,就是左右镜像之后和原串相同,如2S和3AIAE. ...
- Oracle笔记 多表查询
Oracle笔记 多表查询 本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...
- WordPress 在Ubuntu下安装插件、主题输入FTP及无法创建目录的问题
1.安装新主题.插件需要输入FTP的账户密码 如果不想输入的话可以使用在wp-config.php文件中添加脚本方式. define("FS_METHOD","direc ...
- POJ 2002 Squares【值得摸索的一道二分+点旋转】
id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...
- MX2怎样利用Fiddler进行网络数据抓包
首先须要保证PC与手机在同一局域网内或有独立公网IP, 下面以在同一局域网为例(保证手机能訪问到这台PC机器): 1. PC端配置 1). 安装Fiddler 2). 开启Fiddler下面功能: ...
- MongoDB 索引的使用, 管理 和优化
MongoDB 索引的使用, 管理 和优化 2014-03-25 17:12 6479人阅读 评论(0) 收藏 举报 分类: MongoDB(9) [使用explain和hint] 前面讲高级查询 ...
- Java网页小程序——Java Applet
Java Applet是编译过的Java程序,可以在所有支持Java的浏览器中运行. 1.Applet的使用 import java.applet.Applet; import java.awt.Gr ...