POJ 3083
---恢复内容开始---
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的更多相关文章
- poj 3083 dfs,bfs
传送门 Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- 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 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- 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 (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ 3083 Children of the Candy Corn 解题报告
最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...
随机推荐
- ansible 的组件inventory
P44 Ansible 的默认的inventory的是一个静态的ini格式的文件/etc/ansible/hosts. 我们还可以通过ansible_hosts环境变脸指定或者运行ansible和an ...
- 图解Android - Android GUI 系统 (1) - 概论
Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...
- 【bzoj1191】 HNOI2006—超级英雄Hero
http://www.lydsy.com/JudgeOnline/problem.php?id=1191 (题目链接) 题意 有m个问题,n个锦囊妙计,每个锦囊妙计可以解决一个问题,每个问题有两个锦囊 ...
- JSON后端页面解析
json-lib 请求: http://localhost:8080/MyWeb/pay?cmd=getUrl¶m={"OrderId":"sddd111 ...
- boost解析json
#include <QtCore/QCoreApplication> #include <boost/property_tree/ptree.hpp> #include < ...
- hdu 2049 不容易系列之(4)——考新郎
在本博AC代码中,求CNM用的是Anm/amm没用阶乘的形式,两者皆可 #include <stdio.h> int main(void) { long long a,b,larr[21] ...
- pthread_exit
当主线程调用pthread_exit时,其余线程不退出,继续执行 当主线程调用exit/或return时,其余线程退出,整个进程都退出了. #include <pthread.h> #in ...
- Java finally语句到底是在return之前还是之后执行?
网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...
- mysql 查询技巧
查出来的结果每一行显示一条,中间以*号分隔. select * from tableName limit 10 \G mysql 随机取数据 SELECT * FROM table_name ORDE ...
- tar 的一个记录
今天看到群里有人在问.用tar命令备份/etc/passwd 文件提示删除/的提示 这里我要说明下,tar的打包压缩 命令: tar -zcv -f /backup/passwd.tar.gz /et ...