1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std;
int nW, nH; char szData[100][100]; bool bVisit[100][100]; int nNum; int nDice[100]; int nAdd[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
bool IsPosOk(int i, int j) { return i >= 0 i < nH j >= 0 j < nW; }
struct POS { int nI; int nJ; };
bool Bfs(int nI, int nJ) { bool bRet = false; queue<POS> qp; POS pos = {nI, nJ}; int i = nI, j = nJ;
qp.push(pos); while (qp.empty() == false) { POS head = qp.front(); qp.pop();
for (int m = 0; m < 4; ++m) { int nNextI = head.nI + nAdd[m][0]; int nNextJ = head.nJ + nAdd[m][1];
if (IsPosOk(nNextI, nNextJ) bVisit[nNextI][nNextJ] == false) { if (szData[nNextI][nNextJ] == 'X') { bVisit[nNextI][nNextJ] = true; POS pos = {nNextI, nNextJ}; qp.push(pos); } else if (szData[nNextI][nNextJ] == '*') { bRet = true; nI = nNextI; nJ = nNextJ; } } } }
return bRet; }
void dfs(int i, int j, int nNum) { bVisit[i][j] = true; if (szData[i][j] == 'X') { nDice[nNum]++; bool bDfs = Bfs(i, j); if (bDfs == false) { return; } else { dfs(i, j, nNum); } }
for (int m = 0; m < 4; ++m) { int nNextI = i + nAdd[m][0]; int nNextJ = j + nAdd[m][1];
if (IsPosOk(nNextI, nNextJ) bVisit[nNextI][nNextJ] == false szData[nNextI][nNextJ] != '.') { dfs(nNextI, nNextJ, nNum); } } }
int main() { int nCases = 1;
while (scanf("%d%d", &nW, &nH), nW + nH) { for (int i = 0; i < nH; ++i) { scanf("%s", szData[i]); } memset(bVisit, false, sizeof(bVisit)); memset(nDice, 0, sizeof(nDice)); nNum = 0;
for (int i = 0; i < nH; ++i) { for (int j = 0; j < nW; ++j) { if (szData[i][j] == 'X' bVisit[i][j] == false) { dfs(i, j, nNum); nNum++; } } } sort(nDice, nDice + nNum);
printf("Throw %d\n", nCases++); for (int i = 0; i < nNum; ++i) { printf("%d%s", nDice[i], i == nNum - 1 ? "\n" : " "); } printf("\n"); }
return 0; }
|