Kunai
Time limit3sMemory limit256 MB
Ninjas on a huge grid throw kunai in four directions; kunai vanish when two arrive at the same point at the same instant, so count the squares any surviving kunai passes through.
- Level
Hard8 of 10
- Topics
- Geometry, Hash map, Sorting, Implementation
- Solved
- No attempts yet
Problem
A kunai (a throwing blade) is a finely made, dagger-like weapon used by ninjas, who attack by throwing it at their enemies.
There is a grid of rows and columns of unit squares, holding ninjas. Every ninja stands at the center of a square, and each square holds at most one ninja. Each ninja carries one kunai and faces one of four directions: right, up, left, or down. At time , every ninja throws its kunai in the direction it is facing.
Every kunai flies in a straight line at a speed of one square per unit of time. If more than one kunai arrives at the same point at the same instant, those kunai collide and disappear. A kunai is small enough that its size can be ignored. The ninjas move so fast that they are never hit by a kunai. Unless it collides with another kunai, a kunai keeps moving in the same direction at the same speed and never slows down. Note that a collision may happen partway between two squares, not only at a square's center.
After enough time has passed, count how many of the squares have been passed through by at least one kunai.
Input
The first line contains two integers and , separated by a single space, giving the size of the grid (, ).
The second line contains one integer , the number of ninjas ().
Each of the next lines contains three integers , , describing ninja , who stands in the -th column from the left and the -th row from the top (, ). No two ninjas occupy the same square. The direction ninja faces is given by :
- : ninja faces right.
- : ninja faces up.
- : ninja faces left.
- : ninja faces down.
Output
Print the number of squares of the grid that have been passed through by a kunai after enough time has passed.
Hint
Take the sample input. Let "kunai " be the kunai thrown by ninja . At time , kunai and kunai meet between their squares and disappear. At time , kunai and kunai collide and disappear. After time there are no further collisions. In the end, exactly squares have been passed through by a kunai, so the answer is .