Skip to content

Instantly share code, notes, and snippets.

@macrat
Last active May 25, 2016 13:11
Show Gist options
  • Save macrat/60c38cd848e17d15b86163482aa9836a to your computer and use it in GitHub Desktop.
Save macrat/60c38cd848e17d15b86163482aa9836a to your computer and use it in GitHub Desktop.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
struct {
int_fast32_t x, y;
} typedef Position;
int main(){
const Position cands[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
uint_fast32_t w, h, x, y, stack_itr, count;
uint_fast8_t c, i;
Position p;
while(scanf("%lu %lu\n", &w, &h) && w){
bool tile[h][w];
Position stack_data[h*w];
stack_itr = 1;
count = 0;
for(y=0; y<h; ++y){
for(x=0; x<w; ++x){
c = fgetc(stdin);
tile[y][x] = c == '.';
if(c == '@'){
stack_data[0].x = x;
stack_data[0].y = y;
}
}
fgetc(stdin);
}
while(stack_itr){
count++;
stack_itr--;
p = stack_data[stack_itr];
for(i=0; i<4; ++i){
x = p.x + cands[i].x;
y = p.y + cands[i].y;
if(x < w && y < h && tile[y][x]){
tile[y][x] = false;
stack_data[stack_itr].x = x;
stack_data[stack_itr].y = y;
stack_itr++;
}
}
}
printf("%lu\n", count);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment