Skip to content

Instantly share code, notes, and snippets.

@duyet
Last active August 29, 2015 14:13
Show Gist options
  • Save duyet/865d1e896c3a572ff759 to your computer and use it in GitHub Desktop.
Save duyet/865d1e896c3a572ff759 to your computer and use it in GitHub Desktop.
#include<stdio.h>
int width, height, pool, case_id = 0;
int field[103][103];
void rec(int x,int y) {
if (field[x][y]!=0) return;
field[x][y]=pool;
if (x<=height) rec(x+1,y);
if (y<=width) rec(x,y+1);
if (x>0) rec(x-1,y);
if (y>0) rec(x,y-1);
if (x<=height && y<=width) rec(x+1,y+1);
if (x<=height && y>0) rec(x+1,y-1);
if (x>0 && y<=width) rec(x-1,y+1);
if (x>0 && y>0) rec(x-1,y-1);
}
int main() {
while (~scanf("%d%d", &height, &width)) {
char s[103];
int i,j;
pool = 0;
case_id++;
for(i=0;i<width+2;i++)
field[0][i] = field[height+1][i]=-1;
for(j=0;j<height+2;j++)
field[j][0] = -1;
for(i=1;i<=height;i++) {
scanf("%s", &s[1]);
for(j=1; s[j]!='\0'; j++) {
if (s[j]=='-')
field[i][j]=0;
else
field[i][j]=-1;
}
field[i][width+1]=-1;
}
for (i=1; i<=height; i++) {
for (j=1; j<=width; j++) {
if (field[i][j]==0) {
pool++;
rec(i,j);
}
}
}
printf("Case %d: %d\n", case_id, pool);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment