题意:给你一个迷宫,入口处标为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. 【Newtonsoft.Json】Window Phone Json解析开发包

    WP从服务器.API交换数据一般都是用JSON格式字符串. 下面介绍用Newtonsoft.Json来处理JSON. 准备 1.到 http://json.codeplex.com/ 下载Newton ...

  2. 《samba服务搭建》RHEL6

    Samba服务不仅可以实现linux和win之间的文件共享,也可以实现linux和linux之间的共享,samba的用户只限服务端本地用户使用. 本文的环境是selinux开启的情况下配置 Samba ...

  3. C++实现设计模式之 — 简单工厂模式

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4251756.html 所谓简单工厂模式,是一种实例化对象的方式,只要输入需要实例化对象的名字 ...

  4. Hbase 0.95.2介绍及下载地址

    HBase是一个分布式的.面向列的开源数据库,该技术来源于Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件系统(File System) ...

  5. js 鼠标事件的抓取代码

    js 鼠标事件的抓取代码,分享给大家. 1.通过ele.setCapture();设置鼠标事件的抓取. 2,应用可以通过单.双击文字来获取时间. <html> <head> & ...

  6. Java注意的地方

    oo: 单一原则(SRP) 开放封闭原则(OCP) 里氏替换原则(LSP) 依赖倒转原则(DIP) 接口分离原则(ISP) equals: 若两个对象equals为true,则他们的hashcode值 ...

  7. 比较不错的JS 曲线图

    fashion chart   falsh文件支持,无需考虑兼容 Highcharts(纯JS,很漂亮 效果很好) Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容 ...

  8. How to change comment

    AX2009 // USR Changed on 2013-07-10 at 12:57:46 by 7519 - Begin // USR Changed on 2013-07-10 at 12:5 ...

  9. 重绘panel控件,实现panel的阴影效果

    最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下: 重绘代码如下: using System; using System.Co ...

  10. openerp学习笔记 调用工作流

    获取工作流服务:wf_service = netsvc.LocalService("workflow")删除对象对应记录的工作流:wf_service.trg_delete(uid ...