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: ...
随机推荐
- ValueError: option names {'--alluredir'} already added 报错
运行测试用例 import pytest from WXP2P_2.test_data2.login_case import logindata_error1,logindata_error2,log ...
- CAD交互绘制圆形批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...
- pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'
http://blog.csdn.net/qq_33202928/article/details/72526710
- python之道04
1.写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...
- 洛谷 P3958 奶酪
谨以此题来纪念我爆炸的NOIp2017 这个题虽然很多人说是并查集,但是搜索也是毫无压力的,考场搜索细节写挂,爆了个不上不下的80分.今天无意看到这道题,终于AC 首先这道题要考虑一下精度问题,虽然出 ...
- Python IDE推荐
八个最佳Python IDE 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Python是一种功能强大.语言简洁的编程语言.本文向大家推荐8个适合pyt ...
- BZOJ 4016 最短路径树问题 最短路径树构造+点分治
题目: BZOJ4016最短路径树问题 分析: 大家都说这是一道强行拼出来的题,属于是两种算法的模板题. 我们用dijkstra算法算出1为源点的最短路数组,然后遍历一下建出最短路树. 之后就是裸的点 ...
- bzoj5286 [Hnoi2018]转盘
题目描述: bz luogu 题解: 看了半个晚上终于明白了. 首先最优决策一定有:在起始点停留一段时间然后一直前进. 解释网上有很多,在这里不赘述了. (由于是环,先把$T$数组倍长.) 首先基于决 ...
- [OpenJudge] 2727 仙岛寻药
2727:仙岛求药 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进 ...
- 【OS_Linux】yum命令安装软件
1.YUM的简介 Yum(全称为 Yellow dog Updater, Modified)是一个rpm包管理器.它能够从指定的服务器上自动下载RPM包并安装,可以自动处理包之间的依赖性关系,并且一次 ...