Pipe Move 2
InterviewTime limit0.5sMemory limit512 MB
Count the ways to push a 2-cell pipe (horizontal, vertical, or diagonal) across an N by N grid so its end reaches (N, N), keeping all covered cells empty.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Implementation, Simulation, Matrix
- Solved
- No attempts yet
Problem
Yuhyeon has moved into a new house. The house can be represented as an N×N grid, divided into 1×1 square cells. Each cell is denoted by (r, c), where r is the row number and c is the column number, and row and column numbers start at 1. Each cell is either empty or a wall.
Today, to repair the house, he wants to move a single pipe. The pipe has the shape shown below and occupies 2 consecutive cells.

The pipe can be rotated, and there are 3 possible orientations as shown below.

The pipe is very heavy, so Yuhyeon wants to move it by pushing it. Because the walls have new wallpaper on them, the pipe must not scratch the wall. That is, the pipe must always occupy only empty cells.
There are 3 directions in which the pipe can be pushed: →, ↘, and ↓. The pipe can be rotated while being pushed. It can only be rotated by 45 degrees, and the pushing direction must be right, down, or the diagonal direction down-right.
When the pipe lies horizontally there are 2 possible moves, when it lies vertically there are 2, and when it lies diagonally there are 3.
The figures below show all possible moves depending on the pipe's orientation, and the places that must be empty are marked with color.

Horizontal

Vertical

Diagonal
Initially, the pipe occupies (1, 1) and (1, 2), and its orientation is horizontal. Find the number of ways to move one end of the pipe to (N, N).
Input
The first line gives the size of the house N (3 ≤ N ≤ 32). From the second line, N lines give the state of the house. An empty cell is given as 0, and a wall as 1. (1, 1) and (1, 2) are always empty.
Output
Print the number of ways to move one end of the pipe to (N, N) on the first line. If it cannot be moved, print 0.