POJ3083Children of the Candy Corn
题意:给你一个迷宫,入口处标为S,出口处标为E,可以走的地方为“.”,不可以走的地方为#,求左转优先时从出口到入口的路程,再求右转优先时,出口到入口的路程,最后求从出口到入口的最短路程。
思路:求前两个的时候用DFS递归求解能走的路就OK,求最短的时候当然要用广度优先搜索啦。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
char ch[][];
int sum ;//总步数
int h,w,i,j,k ;
int enx,eny,stax,stay;
int fx[] = {,,,-};//列的变化,从0开始顺时针变化
int fy[] = {,,-,};//行的变化,从0开始顺时针变化
int lef[] = {,,,};//左转优先时,标记上下左右4个方向分别为0213;
int rig[] = {,,,};//右转优先时,右边为1;
struct node
{
int xz,yz;
int cnt;
} sh[];//数组模拟队列
void DFS(int dire,int x,int y,int d)//这个点的初始方向,这个点现在坐标,以及是左先还是右先
{
if(x == enx&&y == eny)//如果遍历到了'E'所在坐标,就结束掉遍历,返回
{
printf("%d",sum);
return ;
}
int xx,yy;
sum++;
if(d == )//左转优先
{
for(i = ; i <= ; i++)//4个方向遍历一遍
{
j = (dire+lef[i])%;//求出当前位置的方向数字
xx = x+fx[j];//传过来的参数x为原来的坐标j
yy = y+fy[j];
if(xx>=&&xx< h&&yy>=&&yy< w&&ch[yy][xx] != '#')//继续遍历的条件是不为#且没越出边界,要注意xx是到h为边界的与主函数中对应
{
DFS(j,xx,yy,);//继续递归下去
return ;
}
}
}
else
{
for(i = ; i <= ; i++)
{
j = (dire+rig[i])%;
xx = x+fx[j];
yy = y+fy[j];
if(xx>= && xx < h&&yy>= && yy < w&&ch[yy][xx] != '#')
{
DFS(j,xx,yy,);
return ;
}
}
}
}
void BFS(int x,int y)//广度优先搜索是队列,深度优先搜索是栈,深度是递归
{
int zh[][] = {};//标记数组,初始化为0,表示节点未被访问过
int le = ,ri = ;
zh[y][x] = ;
//将当前点坐标压入队列,后边进行处理
sh[].xz = x;
sh[].yz = y ;
sh[].cnt = ;
while(le < ri)
{
x = sh[le].xz;//从队列中取出进行操作
y = sh[le].yz ;
sum = sh[le++].cnt;
for(k = ; k <= ; k++)
{
//点的坐标
i = y+fy[k];
j = x+fx[k];
if(i>= && i<w && j<h && j>= && !zh[i][j] &&ch[i][j]!='#')//在原有条件下,还要保证节点没有被访问过
{
if(j == enx&&i == eny)//如果找到'E'就输出长度
{
cout<<sum+<<endl;
return ;
}
zh[i][j] = ;//表示这个节点已经被访问过了,标记为1
//将这个点加入队列用来以后进行访问这个点四周的点
sh[ri].yz = i;
sh[ri].xz = j;
sh[ri].cnt = sum+;//这个点步数加1
ri++;//while循环结束的条件就是该点不符合条件,ri不再累加
}
}
}
}
int main()
{
int n,dire;
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&h,&w);//h是列,w是行
for(i = ; i <= w- ; i++)
{
scanf("%s",ch[i]);
for(j = ; j <= h- ; j++)
{
if(ch[i][j] == 'S')
{
stax = j;
stay = i;
}
if(ch[i][j] == 'E')
{
enx = j;
eny = i ;
}
}
}
if(stax == ) dire = ;//左
if(stax == h-) dire = ;//右
if(stay == ) dire = ;//上
if(stay == w-) dire = ;//下
sum = ;//初始化
DFS(dire,stax,stay,);//左转优先
printf(" ");
sum = ;//初始化
DFS(dire,stax,stay,);//右转优先
printf(" ");
BFS(stax,stay);//求最短的用bfs
}
return ;
}
POJ3083Children of the Candy Corn的更多相关文章
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- 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 ...
- HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Jav ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- POJ 3083:Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
随机推荐
- python 中range与xrange的区别
先来看看range与xrange的用法介绍 help(range)Help on built-in function range in module __builtin__: range(...) r ...
- js 统计字符串中出现次数最多的字符?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《shell条件测试语句,字符串测试apache是否开启》
还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgre ...
- html关于强制显示 隐藏浏览器的滚动条
浏览器的滚动条在一些特殊的展示中,是不需要的,所以必须把它隐藏掉,文章主要介绍一些隐藏或者显示IE的水平或者垂直滚动条的实现代码,需要了解的朋友可以参考下: 相关css代码如下: //强制显示滚动条: ...
- hadoop架构
HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块. 1.HADOOP的文件是以HDFS格式存储的 HDFS ...
- jquery如何通过name名称获取当前name的value值
本文为大家介绍下jquery通过name名称获取当前name的value值的具体实现,感兴趣的朋友可以参考下. 复制代码代码如下: $("*[name='name']").val( ...
- ASP.NET中的常用快捷键
想查找ASP.NET中的属性快捷键,忘记了,搜了一下,找到了ASP.NET中的常用快捷键. 大神文章:http://www.cnblogs.com/xiacao/archive/2012/06/12/ ...
- 腾讯WEB前端开发三轮面试经历及面试题
[一面]~=110分钟 2013/04/24 11:20 星期三 进门静坐30分钟做题. 填空题+大题+问答题 >>填空题何时接触电脑 何时接触前端运算符 字符串处理 延 ...
- nginx error_page 404 用 php header 无法跳转
nginx error_page 404 用 php header 无法跳转 之前用Apache的时候,只需要设置 ErrorDocument 404 /404.php 就可以在 404.php 中根 ...
- TCP相关知识
1. TCP与TCP/IP协议族 TCP是TCP/IP协议族中运输层的一个协议.TCP/IP,即传输控制协议/网间协议,是一个工业标准的协议集,包含了运输层.网络层和链路层的协议,其结构如下图所示:其 ...