---恢复内容开始---

http://poj.org/problem?id=3083

题目大意就是给你要你从S走到E,且只有.代表的地方才是可以走的,有三种方式的走法。

一、是向左优先转,从S到E的步数。

二、是向右优先转,从S到E的步数。

三、S到E的最短路径,小于等于前面二者。

思路:这题比较难的就是怎么确定方向,对于向左走的人。它的右边对于我们来说是向上的,解决这个办法可以用数字来模拟方向

  0  
1 当前位置 3
  2  

当你的是从3走到当前位置时,对于你来说,你的左边就是2,右边就是0,

而当你的优先偏转方向是#也就是不能走时,你应该按原方向走,意思就是对于左优先你走的顺序应该2 1 0 3

右优先就是0 1 2 3 而不是0 3 2 1

我的代码也是借鉴了别人的,比较繁琐,但是思路比较清晰,个人觉得比较好理解

 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue> using namespace std; queue<int >first; //定义两个队列,用来分别存位置当前位置,当然也可以用pair类型,那样定义一个就可以了
queue<int >second;
char str[][];
int lstep,rstep,bstep[][];
bool mark[][]; //用来标记走过的,在前两个搜索时不需要用,因为有可能会走原路,用在第三个求最短路径
int lbfs(int i,int j,int d) //向左优先转
{
lstep++;
if(str[i][j]=='E') return ;
switch(d)
{
case :
{
if(str[i][j-]=='.'||str[i][j-]=='E') //记得要加==‘E’,不然它是永远也找不到E的
lbfs(i,j-,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
lbfs(i-,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
lbfs(i,j+,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
lbfs(i+,j,);
break;
}
case :
{
if(str[i+][j]=='.'||str[i+][j]=='E')
lbfs(i+,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
lbfs(i,j-,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
lbfs(i-,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
lbfs(i,j+,);
break;
}
case :
{
if(str[i][j+]=='.'||str[i][j+]=='E')
lbfs(i,j+,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
lbfs(i+,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
lbfs(i,j-,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
lbfs(i-,j,);
break;
}
case :
{
if(str[i-][j]=='.'||str[i-][j]=='E')
lbfs(i-,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
lbfs(i,j+,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
lbfs(i+,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
lbfs(i,j-,);
break;
}
}
return ;
}
int rbfs(int i,int j,int d)
{
rstep++;
if(str[i][j]=='E') return ;
switch(d)
{
case :
{
if(str[i][j+]=='.'||str[i][j+]=='E')
rbfs(i,j+,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
rbfs(i-,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
rbfs(i,j-,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
rbfs(i+,j,);
break;
}
case :
{
if(str[i-][j]=='.'||str[i-][j]=='E')
rbfs(i-,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
rbfs(i,j-,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
rbfs(i+,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
rbfs(i,j+,);
break;
}
case :
{
if(str[i][j-]=='.'||str[i][j-]=='E')
rbfs(i,j-,);
else if(str[i+][j]=='.'||str[i+][j]=='E')
rbfs(i+,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
rbfs(i,j+,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
rbfs(i-,j,);
break;
}
case :
{
if(str[i+][j]=='.'||str[i+][j]=='E')
rbfs(i+,j,);
else if(str[i][j+]=='.'||str[i][j+]=='E')
rbfs(i,j+,);
else if(str[i-][j]=='.'||str[i-][j]=='E')
rbfs(i-,j,);
else if(str[i][j-]=='.'||str[i][j-]=='E')
rbfs(i,j-,);
break;
}
}
return ;
}
int dfs(int i,int j)
{
memset(mark,false,sizeof(mark)); //切记对这些数值都要进行清零,还有对队列要记得清空,不然很容易出问题
memset(bstep,,sizeof(bstep));
bstep[i][j]=;
while(!first.empty())
{
first.pop();
second.pop();
}
int he,ba;
first.push(i);
second.push(j);
mark[i][j]=true;
while(!first.empty())
{
he=first.front();
first.pop();
ba=second.front();
second.pop();
if(str[he][ba]=='E')
{
printf(" %d\n",bstep[he][ba]);
break;
}
if((str[he+][ba]=='.'||str[he+][ba]=='E')&&!mark[he+][ba]) //对于没走过的路又可以走的路进行标记,这样可以确定这个路是最短的。
{
first.push(he+);
second.push(ba);
mark[he+][ba]=true;
bstep[he+][ba]=bstep[he][ba]+;
}
if((str[he][ba+]=='.'||str[he][ba+]=='E')&&!mark[he][ba+])
{
first.push(he);
second.push(ba+);
mark[he][ba+]=true;
bstep[he][ba+]=bstep[he][ba]+;
}
if((str[he][ba-]=='.'||str[he][ba-]=='E')&&!mark[he][ba-])
{
first.push(he);
second.push(ba-);
mark[he][ba-]=true;
bstep[he][ba-]=bstep[he][ba]+;
}
if((str[he-][ba]=='.'||str[he-][ba]=='E')&&!mark[he-][ba])
{
first.push(he-);
second.push(ba);
mark[he-][ba]=true;
bstep[he-][ba]=bstep[he][ba]+;
}
}
return ;
}
int main()
{
int n,m,t,i,j,k,start;
scanf("%d",&t);
while(t)
{
t--;
memset(str,,sizeof(str));
lstep=;
rstep=;
scanf("%d%d",&m,&n);
for(i=;i<=n;i++)
scanf("%s",str[i]);
for(i=,k=;i<=n;i++)
{
for(j=;j<m;j++)
if(str[i][j]=='S')
{
k=;
break;
}
if(k==) break;
}
bstep[i][j]=;
if(i==n) start=;
else if(j==m-) start=;
else if(i==) start=;
else start=;
switch(start)
{
case :{ lbfs(i-,j,);break;} case :{ lbfs(i,j-,);break;} case :{ lbfs(i+,j,);break;} case :{ lbfs(i,j+,);break;}
}
switch(start)
{
case :{ rbfs(i-,j,);break;} case :{ rbfs(i,j-,);break;} case :{ rbfs(i+,j,);break;} case :{ rbfs(i,j+,);break;}
}
printf("%d %d",lstep,rstep);
dfs(i,j);
}
return ;
}

POJ 3083的更多相关文章

  1. poj 3083 dfs,bfs

    传送门    Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

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

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

  3. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  4. POJ:3083 Children of the Candy Corn(bfs+dfs)

    http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...

  5. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  6. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  7. poj 3083 Children of the Candy Corn

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

  8. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  9. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

随机推荐

  1. Java设计模式-模板方法模式(Template Method)

    解释一下模板方法模式,就是指:一个抽象类中,有一个主方法,再定义1...n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用,先看个关系 ...

  2. 人工蜂群算法-python实现

    ABSIndividual.py import numpy as np import ObjFunction class ABSIndividual: ''' individual of artifi ...

  3. HDU 2896 病毒侵袭

    Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福 ...

  4. 关于mysql乱码的问题

    ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8 COLLATE UTF8_GENERAL_CI; 第一步,用mysql的自带修复工具在bin文 ...

  5. 查看apk包名package和入口activity名称的方法

    ctrl+r 打开CMD窗口 进入sdk-aapt目录 执行命令:aapt dump badging xx.apk 内容太多?不好看,没关系,全部拷出来,ctrl+f,so easy! package ...

  6. Html Div 拖拽

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. heap和stack有什么区别

    1.heap是堆,stack是栈. 2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,heap常用new关键字来分配. 3.stack空间有限,heap的空间是很大的自由 ...

  8. shell 删除文件下的* (copy).jpg备份文件

    shell编程中,  在for, while循环中为什么不用(), {} 不是没有; 而是因为(), {}做了其他用途: (): 执行命令组, 注意这个命令组是新开一个子shell中执行, 因此,括号 ...

  9. R语言操作数据库

    以下内容出自http://www.douban.com/note/172387172/ CRAN上有很多R的数据库支持包,使R能够对数据库进行读写操作.这些包有:RODBC.DBI.RMySQL.RO ...

  10. 大数据下Limit使用(MySQL)

    对于一直用Oracle的我,今天可是非常诧异,MySQL中同一个函数在不同数量级上的性能居然差距如此之大. 先看表ibmng(id,title,info)  唯一  id key 索引title 先看 ...