poj 3083 dfs,bfs
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 55
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m;
int c,c1,c2;
char s[N][N];
int cc[N][N];
int dirx[]={,-,,};
int diry[]={-,,,};
int flag; typedef struct
{
int x;
int y;
int now;
int dir;
}PP; PP start,end; void ini()
{
int i,j;
c=c1=c2=;
//memset(cc,0,sizeof(cc));
scanf("%d%d",&m,&n);
for(int i=;i<n;i++){
scanf("%s",s[i]);
}
for(i=;i<n;i++){
for(j=;j<m;j++){
cc[i][j]=;
if(s[i][j]=='S'){
start.x=i;
start.y=j;
start.now=;
}
if(s[i][j]=='E'){
end.x=i;
end.y=j;
}
}
}
} void solve()
{ } int isok(PP o,PP pre)
{
if( o.x>= && o.x<n && o.y>= && o.y<m && s[o.x][o.y]!='#' && cc[o.x][o.y]>pre.now+)
{
o.now=pre.now+;
cc[o.x][o.y]=o.now;
return o.now;
}
return ;
} void bfs()
{
int i;
PP te,next;
queue<PP> q;
start.now=;
q.push(start);
while(q.size()>){
te=q.front();
q.pop();
// printf(" %d %d %d\n",te.x,te.y,te.now);
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(isok(next,te)!=){
next.now=te.now+;
q.push(next);
}
}
}
c=cc[end.x][end.y];
} int ok(PP o)
{
if( o.x>= && o.x<n && o.y>= && o.y<m && s[o.x][o.y]!='#' )
{
return ;
}
return ;
} void dfs1(PP te)
{
//flag=0;
// printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
int i;
PP next;
if(te.x==start.x && te.y==start.y){
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
}
}
} if(te.x==end.x && te.y==end.y){
c1=te.now;
flag=;
return;
} if(flag==) return;
// for(int k=t;k<4;k++){
i=te.dir-;
if(i<) i+=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return; i=te.dir;
// if(i<0) i+=4;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} return;
// } } void dfs2(PP te)
{
//flag=0;
// printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
int i;
PP next;
if(te.x==start.x && te.y==start.y){
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
}
}
} if(te.x==end.x && te.y==end.y){
c2=te.now;
flag=;
return;
} if(flag==) return;
// for(int k=t;k<4;k++){
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return; i=te.dir;
// if(i<0) i+=4;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return;
i=te.dir-;
if(i<) i+=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} return;
// }
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
//while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
flag=;
dfs1(start);
flag=;
dfs2(start);
bfs();
printf("%d %d %d\n",c1,c2,c);
} return ;
}
poj 3083 dfs,bfs的更多相关文章
- 小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】
小鼠迷宫问题 Time Limit: 1500ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中 ...
- 递归,回溯,DFS,BFS的理解和模板【摘】
递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: void f() { if(符合边界条件) { // ...
- 邻接矩阵实现图的存储,DFS,BFS遍历
图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...
- 判断图连通的三种方法——dfs,bfs,并查集
Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...
- 递归,回溯,DFS,BFS的理解和模板
LeetCode 里面很大一部分题目都是属于这个范围,例如Path Sum用的就是递归+DFS,Path Sum2用的是递归+DFS+回溯 这里参考了一些网上写得很不错的文章,总结一下理解与模板 递归 ...
- POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...
- 迷宫问题(DFS,BFS)
/******************************** 啊哈!算法 深度优先搜索算法 迷宫问题 输入: 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 ...
- UVA10410 TreeReconstruction 树重建 (dfs,bfs序的一些性质,以及用栈处理递归 )
题意,给你一颗树的bfs序和dfs序,结点编号小的优先历遍,问你可能的一种树形: 输出每个结点的子结点. 注意到以下事实: (1)dfs序中一个结点的子树结点一定是连续的. (2)bfs,dfs序中的 ...
- dfs,bfs的二分匹配模板(模板题hdu1150)
如果不懂匈牙利算法,请点击:该趣味算法http://blog.csdn.net/dark_scope/article/details/8880547 模板: //DFS版本下的二分匹配算法 http: ...
随机推荐
- django 2.0 + pycharm2017 出现的问题
在创建完成app之后,在models文件里创建两个类:BlogType , Blog, 创建超级用户,注册admin,在登陆admin之后发现,发现 BlogType , Blog,并没有导入到adm ...
- 设置与使用SQL Server的字符集(Collation,即排序规则)
目录 目录 正确认识SQL Server的字符集 选择合适的SQL Server字符集 错误使用SQL Server的字符集 参考资料 正确认识SQL Server的字符集 SQL Server作为一 ...
- robotframework接口测试实例
*** Settings *** Library Collections Library RequestsLibrary *** Test Cases *** test Create Session ...
- CPP-基础:wchar_t
目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...
- python之set (集合)
1. 集合是什么 set {1,2,3} 2. 集合怎么用 去重 集合是无序的 集合就是一个没有值的字典,遵循:唯一,无序,元素要求可哈希(不可变) 集合是可变的 2.1 增 方法一: s.updat ...
- 一款App的开发成本是多少?
答一: 接触过上万名创业者,开发上线过超过30款App,没有比我更适合回答这个问题的了.. 本文对想做好一款App项目的人来说这是一篇价值百万的回答!因为这是我们花了几百万试错成本试出来的经验! &l ...
- HTTP协议详解-基础知识
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.绝大多数的Web开发,都是构建在HTTP协议之上的Web应用. HTTP协议的主要特点可概括如下: 简单: ...
- Ueditor1.4.3上传视频IE下无法播放的问题
一:百度编辑器插入视频后,自动生成一段代码: <video class="edui-upload-video vjs-default-skin video-js" contr ...
- 关于JS的继承总结
最近都在巩固JS的基础知识,今天组要看的是有关继承方面的,每次看都会加深自己的理解呢 1.借助构造函数实现继承 原理:在子类中改变父类this的指向 function Parent1() { this ...
- hihoCode-1043-完全背包
我们定义:best(i,x)代表i件以前的物品已经决定好选择多少件,并且在剩余奖券x的情况下的最优解. 我们可以考虑最后一步,是否再次选择i物品,在不超过持有奖券总额的情况下.上面的第二个式子的k是大 ...