USACO 2.4 Overfencing

TASK: maze1
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3112 KB]
   Test 2: TEST OK [0.000 secs, 3112 KB]
   Test 3: TEST OK [0.000 secs, 3112 KB]
   Test 4: TEST OK [0.000 secs, 3112 KB]
   Test 5: TEST OK [0.000 secs, 3112 KB]
   Test 6: TEST OK [0.000 secs, 3112 KB]
   Test 7: TEST OK [0.000 secs, 3112 KB]
   Test 8: TEST OK [0.000 secs, 3112 KB]
   Test 9: TEST OK [0.000 secs, 3112 KB]
   Test 10: TEST OK [0.000 secs, 3112 KB]

All tests OK.
1 /*
2 PROG: maze1
3 ID: jiafeim1
4 LANG: C++
5 */
6
7
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 using namespace std;
12
13 #include <queue>
14
15 char map[205][80];
16
17 #define minX ((x)>(y)?(x):(y))
18
19 int out[3][2];
20 long maxN = 0;
21 long minN[205][80];
22 int w,h;
23 struct po
24 {
25 int row;
26 int column;
27 int step;
28 po(int r,int c,int s):row(r),column(c),step(s){}
29 po(){};
30 };
31
32
33 int ward[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
34
35 void work(int row,int column)
36 {
37 queue<po> wq;
38 po point;
39 wq.push(po(row,column,0));
40 bool haveDo[205][80]={false};
41 haveDo[row][column] = true;
42 while(!wq.empty())
43 {
44 point = wq.front();
45 wq.pop();
46 int cur_step = point.step;
47 for(int i = 0;i!=4;++i)
48 {
49 int next_row = point.row + ward[i][0];
50 int next_column = point.column + ward[i][1];
51
52 if(next_row>=0 && next_row <2*h+1 && next_column>=0 && next_column<2*w+1 && map[next_row][next_column] == ' ')
53 {
54 next_row+=ward[i][0];
55 next_column+=ward[i][1];
56
57 if(next_row>=0 && next_row <2*h+1 && next_column>=0 && next_column<2*w+1 && minN[next_row][next_column]>cur_step+1)
58 {
59 minN[next_row][next_column] = cur_step+1;
60 haveDo[next_row][next_column] = true;
61 wq.push(po(next_row,next_column,cur_step+1));
62 }
63 }
64 }
65 }
66 }
67
68 int main()
69 {
70 ifstream fin("maze1.in");
71 ofstream fout("maze1.out");
72
73 string ts;
74 char tc;
75 fin>>w>>h;
76 getline(fin,ts);
77 int temp = 0;
78 for(int i = 0;i!=2*h+1;++i)
79 {
80 getline(fin,ts);
81 for(int j = 0 ;j!=2*w+1;++j)
82 {
83 tc=ts[j];
84 minN[i][j] = 999999999;
85 map[i][j] = tc;
86 if(i==0 && tc == ' ')
87 {
88 out[temp][0] = i-1;
89 out[temp][1] = j;
90 ++temp;
91 }
92 if(j==0 && tc == ' ')
93 {
94 out[temp][0] = i;
95 out[temp][1] = j-1;
96 ++temp;
97 }
98 if(i==2*h && tc == ' ')
99 {
100 out[temp][0] = i+1;
101 out[temp][1] = j;
102 ++temp;
103 }
104 if(j==2*w && tc == ' ')
105 {
106 out[temp][0] = i;
107 out[temp][1] = j+1;
108 ++temp;
109 }
110 }
111 }
112 work(out[0][0],out[0][1]);
113 work(out[1][0],out[1][1]);
114 for(int i = 0;i!=2*h+1;++i)
115 for(int j = 0 ;j!=2*w+1;++j)
116 {
117 if(minN[i][j]!=999999999 && minN[i][j]>maxN)
118 maxN = minN[i][j];
119 }
120 fout<<maxN<<endl;
121
122 fin.close();
123 fout.close();
124 return 0;
125 }
posted @ 2011-05-08 00:48  幻魇  阅读(302)  评论(0编辑  收藏  举报