USACO Section 2.1: Prob The Castle

这个题是我下定决心搞定图论题的第一次自主做出来的题。非常爽

  1 /*

  2 ID: yingzho1

  3 LANG: C++

  4 TASK: castle

  5 */

  6 #include <iostream>

  7 #include <fstream>

  8 #include <string>

  9 #include <map>

 10 #include <vector>

 11 #include <set>

 12 #include <algorithm>

 13 #include <stdio.h>

 14 #include <queue>

 15 #include <cstring>

 16 

 17 using namespace std;

 18 

 19 int paths[60][60], com[60][60];

 20 int M, N, component;

 21 int roomsize[3600];

 22 int maxsize = 0;

 23 int dire[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

 24 

 25 ifstream fin("castle.in");

 26 ofstream fout("castle.out");

 27 bool check(int x, int y) {

 28     if (x >= 1 && x <= N && y >= 1 && y <= M) return true;

 29     return false;

 30 }

 31 

 32 void floodfill(int x, int y) {

 33     queue<int> Sx;

 34     queue<int> Sy;

 35     com[x][y] = component;

 36     Sx.push(x);

 37     Sy.push(y);

 38     while (!Sx.empty()) {

 39         int xx = Sx.front();

 40         int yy = Sy.front();

 41         Sx.pop(), Sy.pop();

 42         int dir = paths[xx][yy];

 43         for (int k = 0; k < 4; k++) {

 44             int tmpx = xx + dire[k][0];

 45             int tmpy = yy + dire[k][1];

 46             if (check(tmpx, tmpy) && (~dir & (1 << k)) && com[tmpx][tmpy] == 0) {

 47                 Sx.push(tmpx);

 48                 Sy.push(tmpy);

 49                 com[tmpx][tmpy] = component;

 50             }

 51         }

 52     }

 53 }

 54 

 55 bool issmaller(int i, int j, int k, int indexx, int indexy, int direction) {

 56     if (j == indexy) {

 57         return i == indexx? k < direction : i > indexx;

 58     }

 59     return j < indexy;

 60 }

 61 

 62 

 63 int main()

 64 {

 65     fin >> M >> N;

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

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

 68             fin >> paths[i][j];

 69         }

 70     }

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

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

 73             if (com[i][j] == 0) {

 74                 component++;

 75                 floodfill(i, j);

 76             }

 77         }

 78     }

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

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

 81             roomsize[com[i][j]]++;

 82             maxsize = max(maxsize, roomsize[com[i][j]]);

 83         }

 84     }

 85 

 86     fout << component << endl;

 87     fout << maxsize << endl;

 88 

 89     int sum = 0;

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

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

 92             for (int k = 0; k < 4; k++) {

 93                 if (!check(i+dire[k][0], j+dire[k][1])) continue;

 94                 int curcom = com[i][j];

 95                 int neighcom = com[i+dire[k][0]][j+dire[k][1]];

 96                 if (curcom != neighcom) sum = max(sum, roomsize[curcom] + roomsize[neighcom]);

 97             }

 98         }

 99     }

100 

101     fout << sum << endl;

102 

103     int indexx, indexy;

104     indexx = 0;

105     indexy = M+1;

106     int direction = 5;

107 

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

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

110             for (int k = 0; k < 4; k++) {

111                 int curcom = com[i][j];

112                 int neighcom = com[i+dire[k][0]][j+dire[k][1]];

113                 if (curcom != neighcom && roomsize[curcom]+roomsize[neighcom] == sum && issmaller(i, j, k, indexx, indexy, direction)) {

114                     indexx = i, indexy = j, direction = k;

115                 }

116             }

117         }

118     }

119 

120     fout << indexx << " " << indexy << " ";

121     if (direction == 0) fout << "W" << endl;

122     else if (direction == 1) fout << "N" << endl;

123     else if (direction == 2) fout << "E" << endl;

124     else fout << "S" << endl;

125 

126     return 0;

127 }

 

你可能感兴趣的:(USACO)