Created
May 24, 2016 14:04
-
-
Save macrat/35f443e659d8994660a427078963c2ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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