USACO 2.1 The Castle

1 /*
2 ID: jiafeim1
3 PROG: castle
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9 #include <string>
10 #include <vector>
11 using namespace std;
12
13 #define maxN(x,y) ((x)>(y)?(x):(y))
14 #include <queue>
15
16 int wall[53][53];
17 int whatSet[53][53];
18
19 int top = 0;
20 int squre[2503];
21 int max_squre = 0;
22 struct point
23 {
24 int x;
25 int y;
26 point(int xi,int yi)
27 {
28 x=xi;
29 y=yi;
30 }
31 };
32 void bfs(int x,int y)
33 {
34 int s = 0;
35 queue<point> wq;
36 whatSet[x][y] = top;
37 wq.push(point(x,y));
38 while(!wq.empty())
39 {
40 ++s;
41 point p = wq.front();
42 wq.pop();
43 int row = p.x;
44 int column = p.y;
45 int wal = wall[row][column];
46 if(!(wal&1) && whatSet[row][column-1] == -1)
47 {
48 wq.push(point(row,column-1));
49 whatSet[row][column-1] = top;
50 }
51 if(!(wal&2) && whatSet[row-1][column] == -1)
52 {
53 wq.push(point(row-1,column));
54 whatSet[row-1][column] = top;
55 }
56 if(!(wal&4) && whatSet[row][column+1] == -1)
57 {
58 wq.push(point(row,column+1));
59 whatSet[row][column+1] = top;
60 }
61 if(!(wal&8) && whatSet[row+1][column] == -1)
62 {
63 wq.push(point(row+1,column));
64 whatSet[row+1][column] = top;
65 }
66 }
67 squre[top] = s;
68 if(s>max_squre) max_squre = s;
69 ++top;
70 }
71
72 char ward;
73 int wall_row;
74 int wall_column;
75 int destory_max = 0;
76
77 int main()
78 {
79 ofstream fout ("castle.out");
80 ifstream fin ("castle.in");
81
82
83 int m,n;
84 fin>>m>>n;
85
86 for(int i = 0 ;i!=n;++i)
87 for(int j = 0;j!=m;++j)
88 {
89 fin>>wall[i][j];
90 whatSet[i][j] = -1;
91 }
92 for(int i = 0;i!=n;++i)
93 for(int j= 0;j!=m;++j)
94 {
95 if(whatSet[i][j] == -1)
96 bfs(i,j);
97 }
98
99 for(int column = 0 ;column !=m;++column)
100 for(int row = n-1;row >=0;--row)
101 {
102 int wal = wall[row][column];
103 if(row>=1&&(wal&2)&&(whatSet[row][column]!=whatSet[row-1][column]))
104 {
105 if(squre[whatSet[row][column]]+squre[whatSet[row-1][column]]>destory_max)
106 {
107 destory_max = squre[whatSet[row][column]]+squre[whatSet[row-1][column]];
108 wall_row = row + 1;
109 wall_column = column + 1;
110 ward = 'N';
111 }
112 }
113 if(column<m-1&&(wal&4)&&(whatSet[row][column]!=whatSet[row][column+1]))
114 {
115 if(squre[whatSet[row][column]]+squre[whatSet[row][column+1]]>destory_max)
116 {
117 destory_max = squre[whatSet[row][column]]+squre[whatSet[row][column+1]];
118 wall_row = row + 1;
119 wall_column = column + 1;
120 ward = 'E';
121 }
122 }
123 }
124 fout<<top<<endl;
125 fout<<max_squre<<endl;
126 fout<<destory_max<<endl;
127 fout<<wall_row<<" "<<wall_column<<" "<<ward<<endl;
128 fin.close();
129 fout.close();
130 return 0;
131 }
posted @ 2011-05-03 11:20  幻魇  阅读(320)  评论(0编辑  收藏  举报