poj 3083 dfs,bfs

Children of the Candy Corn

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 3083

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

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

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks (‘#‘), empty space by periods (‘.‘), the start by an ‘S‘ and the exit by an ‘E‘.

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

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S‘ and ‘E‘) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9
 10 #define N 55
 11 #define M 15
 12 #define mod 6
 13 #define mod2 100000000
 14 #define ll long long
 15 #define maxi(a,b) (a)>(b)? (a) : (b)
 16 #define mini(a,b) (a)<(b)? (a) : (b)
 17
 18 using namespace std;
 19
 20 int T;
 21 int n,m;
 22 int c,c1,c2;
 23 char s[N][N];
 24 int cc[N][N];
 25 int dirx[]={0,-1,0,1};
 26 int diry[]={-1,0,1,0};
 27 int flag;
 28
 29 typedef struct
 30 {
 31     int x;
 32     int y;
 33     int now;
 34     int dir;
 35 }PP;
 36
 37 PP start,end;
 38
 39 void ini()
 40 {
 41     int i,j;
 42     c=c1=c2=0;
 43     //memset(cc,0,sizeof(cc));
 44     scanf("%d%d",&m,&n);
 45     for(int i=0;i<n;i++){
 46         scanf("%s",s[i]);
 47     }
 48     for(i=0;i<n;i++){
 49         for(j=0;j<m;j++){
 50             cc[i][j]=10000000;
 51             if(s[i][j]==‘S‘){
 52                 start.x=i;
 53                 start.y=j;
 54                 start.now=1;
 55             }
 56             if(s[i][j]==‘E‘){
 57                 end.x=i;
 58                 end.y=j;
 59             }
 60         }
 61     }
 62 }
 63
 64 void solve()
 65 {
 66
 67 }
 68
 69 int isok(PP o,PP pre)
 70 {
 71     if( o.x>=0 && o.x<n && o.y>=0 && o.y<m && s[o.x][o.y]!=‘#‘ && cc[o.x][o.y]>pre.now+1)
 72     {
 73         o.now=pre.now+1;
 74         cc[o.x][o.y]=o.now;
 75         return o.now;
 76     }
 77     return 0;
 78 }
 79
 80 void bfs()
 81 {
 82     int i;
 83     PP te,next;
 84     queue<PP> q;
 85     start.now=1;
 86     q.push(start);
 87     while(q.size()>0){
 88         te=q.front();
 89         q.pop();
 90      //   printf(" %d %d %d\n",te.x,te.y,te.now);
 91         for(i=0;i<4;i++){
 92             next.x=te.x+dirx[i];
 93             next.y=te.y+diry[i];
 94             if(isok(next,te)!=0){
 95                 next.now=te.now+1;
 96                 q.push(next);
 97             }
 98         }
 99     }
100     c=cc[end.x][end.y];
101 }
102
103 int ok(PP o)
104 {
105     if( o.x>=0 && o.x<n && o.y>=0 && o.y<m && s[o.x][o.y]!=‘#‘ )
106     {
107         return 1;
108     }
109     return 0;
110 }
111
112 void dfs1(PP te)
113 {
114     //flag=0;
115    // printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
116     int i;
117     PP next;
118     if(te.x==start.x && te.y==start.y){
119         for(i=0;i<4;i++){
120             next.x=te.x+dirx[i];
121             next.y=te.y+diry[i];
122             if(ok(next)!=0){
123                 next.now=te.now+1;
124                 next.dir=i;
125                 dfs1(next);
126             }
127         }
128     }
129
130     if(te.x==end.x && te.y==end.y){
131         c1=te.now;
132         flag=1;
133         return;
134     }
135
136     if(flag==1) return;
137    // for(int k=t;k<4;k++){
138         i=te.dir-1;
139         if(i<0) i+=4;
140         next.x=te.x+dirx[i];
141         next.y=te.y+diry[i];
142         if(ok(next)!=0){
143             next.now=te.now+1;
144             next.dir=i;
145             dfs1(next);
146         }
147
148         if(flag==1) return;
149
150         i=te.dir;
151        // if(i<0) i+=4;
152         next.x=te.x+dirx[i];
153         next.y=te.y+diry[i];
154         if(ok(next)!=0){
155             next.now=te.now+1;
156             next.dir=i;
157             dfs1(next);
158         }
159
160         if(flag==1) return;
161         i=te.dir+1;
162         if(i>=4) i-=4;
163         next.x=te.x+dirx[i];
164         next.y=te.y+diry[i];
165         if(ok(next)!=0){
166             next.now=te.now+1;
167             next.dir=i;
168             dfs1(next);
169         }
170
171         if(flag==1) return;
172         i=te.dir+2;
173         if(i>=4) i-=4;
174         next.x=te.x+dirx[i];
175         next.y=te.y+diry[i];
176         if(ok(next)!=0){
177             next.now=te.now+1;
178             next.dir=i;
179             dfs1(next);
180         }
181
182         return;
183    // }
184
185 }
186
187 void dfs2(PP te)
188 {
189     //flag=0;
190    // printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
191     int i;
192     PP next;
193     if(te.x==start.x && te.y==start.y){
194         for(i=0;i<4;i++){
195             next.x=te.x+dirx[i];
196             next.y=te.y+diry[i];
197             if(ok(next)!=0){
198                 next.now=te.now+1;
199                 next.dir=i;
200                 dfs2(next);
201             }
202         }
203     }
204
205     if(te.x==end.x && te.y==end.y){
206         c2=te.now;
207         flag=1;
208         return;
209     }
210
211     if(flag==1) return;
212    // for(int k=t;k<4;k++){
213         i=te.dir+1;
214         if(i>=4) i-=4;
215         next.x=te.x+dirx[i];
216         next.y=te.y+diry[i];
217         if(ok(next)!=0){
218             next.now=te.now+1;
219             next.dir=i;
220             dfs2(next);
221         }
222
223         if(flag==1) return;
224
225         i=te.dir;
226        // if(i<0) i+=4;
227         next.x=te.x+dirx[i];
228         next.y=te.y+diry[i];
229         if(ok(next)!=0){
230             next.now=te.now+1;
231             next.dir=i;
232             dfs2(next);
233         }
234
235         if(flag==1) return;
236         i=te.dir-1;
237         if(i<0) i+=4;
238         next.x=te.x+dirx[i];
239         next.y=te.y+diry[i];
240         if(ok(next)!=0){
241             next.now=te.now+1;
242             next.dir=i;
243             dfs2(next);
244         }
245
246         if(flag==1) return;
247         i=te.dir+2;
248         if(i>=4) i-=4;
249         next.x=te.x+dirx[i];
250         next.y=te.y+diry[i];
251         if(ok(next)!=0){
252             next.now=te.now+1;
253             next.dir=i;
254             dfs2(next);
255         }
256
257         return;
258    // }
259 }
260
261
262 int main()
263 {
264     //freopen("data.in","r",stdin);
265     scanf("%d",&T);
266     for(int cnt=1;cnt<=T;cnt++)
267     //while(T--)
268     //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
269     {
270         ini();
271         flag=0;
272         dfs1(start);
273         flag=0;
274         dfs2(start);
275         bfs();
276         printf("%d %d %d\n",c1,c2,c);
277     }
278
279     return 0;
280 }

时间: 2024-08-09 02:03:01

poj 3083 dfs,bfs的相关文章

DFS,BFS算法

粗略的讲一下这两种算法,为老年痴呆做好准备(ノへ ̄.) DFS: 如上图,你将搜索整张图,而DFS的搜索方法就是,先一味的往前走!走到某个尽头后发现无路可走,后退.咦?后退一步有一个分岔口,这里有多个支路,选择一条没走过的继续走,碰到死胡同,后退,又到了这个分岔口,再去选择没走过的路,直到无路可走,然后返回上一个分岔口.具体过程如下:                                                                       (图中黑色数字均为遍历

递归,回溯,DFS,BFS的理解和模板【摘】

递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: 1 void f() { 2 if(符合边界条件) { 3 /////// 4 return; 5 } 6 7 //某种形式的调用 8 f(); 9 } 回溯:递归的一种,或者说是通过递归这种代码结构来实现回溯这个目的.回溯法可以被认为是一个有过剪枝的DFS过程.解答树角度:带回溯的dfs遍历一棵解答树回溯的一般结构: 1 void dfs(int

POJ 1426 Find The Multiple(DFS,BFS)

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100

poj 3083 dfs+bfs

背景:竟然G++,wa了一发,同样的代码,改为C++就过...后来看了discuss里面人说bfs最后虽然不会用到有return 的情况,也要加上,这个warning警告了的,没想到加上就ac了... 思路:bfs求最短路,然后就是对一直向左和一直向右进行dfs,方法是:记录上一次来的方向,然后根据上一次来的方向确定当前方向怎样才是向左,怎样才是向右,向左的话是顺时针转动,向右的话是逆时针转动. 我的代码: #include <set> #include <stack> #incl

hdu 4707 Pet(dfs,bfs)

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1548    Accepted Submission(s): 733 Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He sear

图论(一):DFS,BFS,邻接链表,并查集

本文总结了图的深度优先搜索,图的广度优先搜索,邻接链表和邻接矩阵的实现,并查集的实现. 0),预备知识 基础词汇:有向图,无向图,带权有向图,带权无向图,有向图中<Vi, Vj>:即Vi--->Vj,弧尾--->弧头,无向图中相邻记为(Vi, Vj),顶点有穷集合V+边的有穷集合E. 图的两种实现方式:1,邻接矩阵:edge[n][n]表示有n个结点,数组内容为权值大小或者是否存在边(∞表示无边,权值或1表示有边,0表示结点到结点本身): 2,邻接链表:针对稀疏矩阵较适宜,为图的每

迷宫问题(DFS,BFS)

1 /******************************** 2 啊哈!算法 3 深度优先搜索算法 4 迷宫问题 5 输入: 6 5 4 7 0 0 1 0 8 0 0 0 0 9 0 0 1 0 10 0 1 0 0 11 0 0 0 1 12 1 1 4 3 13 14 输出:7 15 *********************************/ 16 #include<iostream> 17 #include<ctime> 18 19 using name

&lt;算法基础&gt;图的三种遍历方法————DFS,BFS,Topological sort

1.BFS(Breadth First Search) 具体实现的时候用栈来实现更简单.从start point开始,一圈圈向外. 对于例图的访问顺序是——s,a,c,d,e,b,g,f 2.DFS(Depth First Search) DFS(s){ 首先访问定点s: if(s尚有未被访问的邻居){任取其一u,递归执行DFS(u);}else{return;} } 对于例图的访问顺序是——s,a,e,f,g,b,c,d 3.Topological Sort 仅仅用于有向无环图. 从AOV网中

PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one's supplier in a pr