Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created August 17, 2012 05:23
Show Gist options
  • Save iporsut/3376166 to your computer and use it in GitHub Desktop.
Save iporsut/3376166 to your computer and use it in GitHub Desktop.
ZOJ 2165 : Red and Black
#include <stdio.h>
char tile[20][21];
int count_black(int i, int j, int w, int h) {
int count = 0;
if (tile[i][j] == '.' || tile[i][j] == '@') {
count++;
tile[i][j] = '#';
}
if (((i - 1) >= 0) && (tile[i-1][j] == '.'))
count += count_black(i-1, j, w, h);
if (((i + 1) < h) && (tile[i+1][j] == '.'))
count += count_black(i+1, j, w, h);
if (((j - 1) >= 0) && (tile[i][j-1] == '.'))
count += count_black(i, j-1, w, h);
if (((j + 1) < w) && (tile[i][j+1] == '.'))
count += count_black(i, j+1, w, h);
return count;
}
int main() {
int w, h, i, j, start_i, start_j;
char c;
scanf("%d %d", &w, &h);
while(w != 0 && h != 0) {
for(i = 0; i < h; ++i) {
scanf("%s",tile[i]);
for(j = 0; j < w; ++j) {
if ( tile[i][j] == '@') {
start_i = i;
start_j = j;
}
}
}
printf("%d\n",count_black(start_i, start_j, w, h));
scanf("%d %d", &w, &h);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment