poj 3083 dfs,bfs

 
Children of the Candy Corn
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 }
View Code

 

posted on 2014-08-22 17:52  njczy2010  阅读(326)  评论(13编辑  收藏  举报