Watering Plan Check 3
Time limit1sMemory limit128 MB
Given a grid land divided into fields and a proposed sprinkler plan drawn with letters and underscores, verify the plan and count holes drilled in fences.
- Level
Medium6 of 10
- Topics
- Implementation, Simulation, Graph, DFS
- Solved
- No attempts yet
Problem
Sara farms a large rectangular piece of land. The land is a grid of rows and columns. A horizontal fence runs across it after every fifth row and a vertical fence runs down it after every fifth column, so the fences cut the land into areas of cells called fields.
To keep birds off the crops, some fields have a scarecrow. A scarecrow takes up a single cell, and a field holds at most one.
During a drought Sara waters the crops with sprinklers. A sprinkler has three nozzles: one main nozzle and two side nozzles. Each side nozzle sits in a cell next to the main nozzle (up, down, left or right), so one sprinkler takes up exactly three cells and waters all three. The three cells form a straight line or an L.
A watering plan places sprinklers so that exactly one sprinkler waters every cell without a scarecrow. A cell with a scarecrow holds no nozzle, and no nozzle sits outside the land.
The three cells of one sprinkler need not lie in the same field. When they reach into a neighbouring field, Sara drills a hole in the fence between the two cells.
A plan is written as a picture in the same shape as the land. Every empty cell becomes a lowercase letter, and every fence character between two cells watered by the same sprinkler becomes an underscore _. The letters follow these rules.
- The three cells watered by one sprinkler carry the same letter, even when they lie in different fields.
- Two adjacent cells in the same field watered by different sprinklers carry different letters.
- Two adjacent cells in neighbouring fields watered by different sprinklers with a hole between them carry different letters.
- Two adjacent cells in different fields may carry the same letter as long as the rules above hold.
These rules make the sprinklers readable from the plan alone. Two adjacent cells belong to the same sprinkler when they carry the same letter and either lie in the same field or have a hole between them. One group of cells linked this way is one sprinkler.
You are given the land and a plan. Decide whether the plan is valid and, if it is, count the holes drilled in the fences. A plan is valid when all three conditions below hold.
- Every scarecrow cell holds
#and every empty cell holds one lowercase letter. Every fence position holds its original fence character or an underscore, and every+where fences meet stays+. - Every group read off the plan covers exactly three cells.
- The two cells on either side of an underscore carry the same lowercase letter.
Input
The first line contains and ().
The next lines contain characters each and describe the land. One cell is one character. A dot . is an empty cell and # (ASCII 35) is a scarecrow. A vertical fence is | (ASCII 124), a horizontal fence is - (minus), and + marks a place where fences meet. The fence has no thickness, but it is drawn with characters.
The next lines give the plan in the same shape. Each of those lines consists of English letters and the characters #, |, -, + and _. The plan is not guaranteed to be valid.
Output
Print the number of holes drilled in the fences if the plan is valid. Print -1 otherwise.