Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created February 27, 2014 09:20
Show Gist options
  • Select an option

  • Save KT-Yeh/9246894 to your computer and use it in GitHub Desktop.

Select an option

Save KT-Yeh/9246894 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int X, Y;
char grid[55][55];
struct hole_type {
char ch;
int num;
}hole[2550];
struct point_type {
int i;
int j;
};
hole_type BFS(char c, int i, int j);
bool cmp(hole_type a, hole_type b);
int main()
{
// freopen("input.txt","rt",stdin);
int Case = 1;
while (scanf("%d%d", &X, &Y) && (X || Y)) {
for (int i = 0; i < X; ++i)
scanf("%s", grid[i]);
int numOfhole = 0;
for (int i = 0; i < X; ++i)
for (int j = 0; j < Y; ++j)
if (grid[i][j] != '.')
hole[numOfhole++] = BFS(grid[i][j], i, j);
sort(hole, hole + numOfhole, cmp);
printf("Problem %d:\n", Case++);
for (int i = 0; i < numOfhole; ++i)
printf("%c %d\n", hole[i].ch, hole[i].num);
}
}
const int direction[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
hole_type BFS(char c, int i, int j)
{
queue<point_type> Q;
Q.push({i,j});
grid[i][j] = '.';
int num = 1;
point_type cur, nxt;
while (!Q.empty()) {
cur = Q.front();
Q.pop();
for (int k = 0; k < 4; ++k) {
nxt.i = cur.i + direction[k][0];
nxt.j = cur.j + direction[k][1];
if (nxt.i < 0 || nxt.i >= X || nxt.j < 0 || nxt.j >= Y) continue;
if (grid[nxt.i][nxt.j] == c) {
grid[nxt.i][nxt.j] = '.';
Q.push(nxt);
++num;
}
}
}
return {c, num};
}
bool cmp(hole_type a, hole_type b)
{
return (a.num == b.num) ? (a.ch < b.ch) : (a.num > b.num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment