Skip to content

Instantly share code, notes, and snippets.

@macrat
Created May 24, 2016 14:04
Show Gist options
  • Save macrat/35f443e659d8994660a427078963c2ae to your computer and use it in GitHub Desktop.
Save macrat/35f443e659d8994660a427078963c2ae to your computer and use it in GitHub Desktop.
#include <iostream>
#include <set>
class Position {
const int x, y;
public:
Position(int x, int y) : x(x), y(y) { }
int distance(const Position& t) const {
return abs(x - t.x) + abs(y - t.y);
}
bool near(const Position& t) const {
return distance(t) == 1;
}
bool operator <(const Position t) const {
return (y == t.y && x < t.x) || y < t.y;
}
};
class Poses : public std::set<Position> {
public:
int erase(const Poses& p) {
int c = 0;
for(auto x: p){
c += erase(x);
}
return c;
}
int erase(const Position& p) {
return std::set<Position>::erase(p);
}
};
int main(){
while(true){
int w, h;
std::cin >> w >> h;
if(w == 0 && h == 0){
break;
}
Poses step, floor;
for(int y=0; y<h; y++){
for(int x=0; x<w; x++){
char c;
std::cin >> c;
if(c == '.'){
floor.insert(Position(x, y));
}else if(c == '@'){
step.insert(Position(x, y));
}
}
}
do{
for(auto s: step){
for(auto f: floor){
if(s.near(f)){
step.insert(f);
}
}
}
}while(floor.erase(step));
std::cout << step.size() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment