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 ...
随机推荐
- Qt webkit插件相关知识
1.在Qt中使用 WebKit 浏览器核心 使用 QtWebKit 需要在工程文件(*.pro)中加入: 1. QT +=webkit 2. QT += n ...
- Qt无边框,可移动窗口
QPoint dragPosition; void MainWindow::mousePressEvent(QMouseEvent *event) { if(event->button()==Q ...
- SQL-Server 创建数据库,创建表格
use master --使用master权限 create database E_Market--创建新数据库 on primary--指定主数据文件,有且只有一个 ( name='E_Market ...
- Java-clone浅/深复制
Object中的clone方法为复制当前对象 protected native Object clone() throws CloneNotSupportedException; 想要使用这个方法需要 ...
- java基础-关键字-native
一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由 ...
- 【poj2186】 Popular Cows
http://poj.org/problem?id=2186 (题目链接) 题意 给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个 Solution 其实这道题的题解已经在“题意 ...
- 新建maven项目
1.新建maven project 注意:勾上create a new simple project 2.填写相关信息, Grounp id为大项目名字,Artifact id为小项目的名字.注意:P ...
- HackerRank and MiniMax
传送门 Sherlock and MiniMax Authored by darkshadows on May 07 2014 Problem Statement Watson gives Sherl ...
- dto
dto dto- datatransfer object(数据传输对象):dto在设计之初的主要考量是以粗粒度的数据结构减少网络通信并简化调用接口. http://www.cnblogs.com/wu ...
- c++模板库(简介)
目 录 STL 简介 ......................................................................................... ...