USACO Section 2.1: The Castle

  1 /*

  2 ID: leetcod3

  3 PROG: castle

  4 LANG: C++

  5 */

  6 #include <iostream>

  7 #include <fstream>

  8 #include <string>

  9 #include <map>

 10 #include <vector>

 11 #include <set>

 12 #include <algorithm>

 13 #include <queue>

 14 #include <cmath>

 15 #include <list>

 16 #include <cstring>

 17 #include <cstdlib>

 18 #include <limits>

 19 #include <stack>

 20 

 21 using namespace std;

 22 

 23 ofstream fout ("castle.out");

 24 ifstream fin ("castle.in");

 25 

 26 int num_of_room = 0;

 27 int group_size[2510] = {0};

 28 int room[60][60] = {0};

 29 int group[60][60] = {0};

 30 int M, N;

 31 

 32 int main()

 33 {

 34     fin >> M >> N;

 35     for (int i = 1; i <= N; i++) {

 36         for (int j = 1; j <= M; j++) fin >> room[i][j];

 37     }

 38     for (int i = 1; i <= N; i++) {

 39         for (int j = 1; j <= M; j++) {

 40             if (group[i][j]) continue;

 41             num_of_room++;

 42             queue<pair<int, int> > S;

 43             S.push(make_pair(i, j));

 44             group[i][j] = num_of_room;

 45             while (!S.empty()) {

 46                 pair<int, int> front = S.front();

 47                 //cout << "group " << num_of_room << "has member: " << front.first << " " << front.second << endl;

 48                 int tmp = room[front.first][front.second];

 49                 S.pop();

 50                 int c = tmp & 1;

 51                 if (!c && front.second-1 >= 1 && group[front.first][front.second-1] == 0) {

 52                     group[front.first][front.second-1] = num_of_room;

 53                     S.push(make_pair(front.first, front.second-1));

 54                 }

 55                 tmp >>= 1;

 56                 c = tmp & 1;

 57                 if (!c && front.first -1 >= 1 && group[front.first-1][front.second] == 0) {

 58                     group[front.first-1][front.second] = num_of_room;

 59                     S.push(make_pair(front.first-1, front.second));

 60                 }

 61                 tmp >>= 1;

 62                 c = tmp & 1;

 63                 if (!c && front.second + 1 <= M && group[front.first][front.second+1] == 0) {

 64                     group[front.first][front.second+1] = num_of_room;

 65                     S.push(make_pair(front.first, front.second+1));

 66                 }

 67                 tmp >>= 1;

 68                 c = tmp & 1;

 69                 if (!c && front.first + 1 <= N && group[front.first+1][front.second] == 0) {

 70                     group[front.first+1][front.second] = num_of_room;

 71                     S.push(make_pair(front.first+1, front.second));

 72                 }

 73             }

 74         }

 75     }

 76     /*for (int i = 1; i <= N; i++) {

 77         for (int j = 1; j <= M; j++) {

 78             cout << group[i][j] << " ";

 79         }

 80         cout << endl;

 81     }*/

 82     fout << num_of_room << endl;

 83     int largest_group = 0;

 84     for (int i = 1; i <= N; i++) {

 85         for (int j = 1; j <= M; j++) {

 86             group_size[group[i][j]]++;

 87             largest_group = max(largest_group, group_size[group[i][j]]);

 88         }

 89     }

 90     fout << largest_group << endl;

 91     int largest_union_group = 0;

 92     int x, y;

 93     char dir;

 94     for (int j = 1; j <= M; ++j) {

 95         for (int i = N; i >= 1; --i) {

 96             int tmp = room[i][j];

 97             int c = tmp & 1;

 98             if (c && j-1 >= 1 && group[i][j-1] != group[i][j] && largest_union_group < group_size[group[i][j]] + group_size[group[i][j-1]]) {

 99                 largest_union_group = group_size[group[i][j]] + group_size[group[i][j-1]];

100                 x = i, y = j, dir = 'W';

101             }

102             tmp >>= 1;

103             c = tmp & 1;

104             if (c && i -1 >= 1 && group[i-1][j] != group[i][j] && largest_union_group < group_size[group[i][j]] + group_size[group[i-1][j]]) {

105                 largest_union_group = group_size[group[i][j]] + group_size[group[i-1][j]];

106                 x = i, y = j, dir = 'N';

107             }

108             tmp >>= 1;

109             c = tmp & 1;

110             if (c && j + 1 <= M && group[i][j+1] != group[i][j] && largest_union_group < group_size[group[i][j]] + group_size[group[i][j+1]]) {

111                 largest_union_group = group_size[group[i][j]] + group_size[group[i][j+1]];

112                 x = i, y = j, dir = 'E';

113             }

114             tmp >>= 1;

115             c = tmp & 1;

116             if (c && i + 1 <= N && group[i+1][j] != group[i][j] && largest_union_group < group_size[group[i][j]] + group_size[group[i+1][j]]) {

117                 largest_union_group = group_size[group[i][j]] + group_size[group[i+1][j]];

118                 x = i, y = j, dir = 'S';

119             }

120         }

121     }

122     fout << largest_union_group << endl;

123     fout << x << " " << y << " " << dir << endl;

124     return 0;

125 }

 

你可能感兴趣的:(USACO)