Aboard the Bus to the Workshop, Hello
Time limit1sMemory limit1024 MB
Given a 3-row by N-column bus with staff seats in rows 1 and 3, find the fewest air conditioners to turn on so every cell is cooled, or -1 if impossible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Staff of 월간 향유회 are riding a bus shaped like a rectangle with rows and columns to a workshop. Each cell in rows and has a seat and an air conditioner. Turning on the air conditioner in a cell cools that cell and every cell that shares a side with it. There are staff members in total, and each one sits in a seat on the bus and decides whether to turn on the air conditioner in that cell. All staff sit in different seats.
You manage the bus's air conditioning system and may turn on the air conditioners in seats that no staff member occupies. To keep the bus comfortable, you want every cell, including those in row , to be cooled. Find the minimum number of air conditioners that must be turned on to cool every cell of the bus. Air conditioners turned on by staff members count toward this number too.
Input
The first line contains the number of columns and the number of staff members , separated by a space.
The next lines each contain , , and , separated by spaces. Staff member sits in cell . If , that staff member turns on the air conditioner in that cell. If , it stays off.
All staff sit in different seats, and all input values are integers.
Output
Print the minimum number of air conditioners that must be turned on to cool every cell of the bus. If no way of turning them on cools every cell, print -1 instead.