题意:给你一个迷宫,入口处标为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的更多相关文章

  1. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  2. 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 ...

  3. HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn

    Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Jav ...

  4. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  5. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  6. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  7. K - Children of the Candy Corn(待续)

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

  8. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  9. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

随机推荐

  1. unity 3消 游戏

    3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...

  2. memcach 安装

    Windows7 x64在Wampserver上安装memcache 2012-07-13      0个评论       收藏    我要投稿 Windows7 x64在Wampserver上安装m ...

  3. WPF 绑定五(本身就是数据源)

    xaml: <Window x:Class="WpfApplication1.Window5" xmlns="http://schemas.microsoft.co ...

  4. AngularJS(13)-包含

    AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...

  5. keditor_php图片上传

    <script type="text/javascript" src="/statics/js/kindeditor/kindeditor-min.js" ...

  6. javascript refresh page 几种页面刷新的方法

    Javascript刷新页面的几种方法:1    history.go(0) 2    location.reload() 3    location=location 4    location.a ...

  7. epoll分析

      Epoll详解及源码分析 1.什么是epoll epoll是当前在Linux下开发大规模并发网络程序的热门人选,epoll 在Linux2.6内核中正式引入,和select相似,都是I/O多路复用 ...

  8. 20145120黄玄曦 《java程序设计》 寒假学习总结

    1和2.我对未来规划不多,我认为好好学习积累知识能帮助我应对未来的挑战,这是我的学习动力之一,此外,了解新知识满足好奇心也是我的主要的学习动力. 3.我认为专业课学习比公务员考试重要,我认为专业知识是 ...

  9. iOS优化内存方法推荐

    1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...

  10. Java 8 VM GC Tunning Guide Charter 6

    第六章 并行GC The Parallel Collector The parallel collector (also referred to here as the throughput coll ...