Problem:
Problem D: Weather Forecast

You are the God of Wind.

By moving a big cloud around, you can decide the weather: it invariably rains under the cloud,
and the sun shines everywhere else.

But you are a benign God: your goal is to give enough rain to every field in the countryside,
and sun to markets and festivals. Small humans, in their poor vocabulary, only describe this as
“weather forecast”.

You are in charge of a small country, called Paccimc. This country is constituted of 4 × 4 square
areas, denoted by their numbers.

Your cloud is of size 2 × 2, and may not cross the borders of the country.

You are given the schedule of markets and festivals in each area for a period of time.

On the first day of the period, it is raining in the central areas (6-7-10-11), independently of the
schedule.

On each of the following days, you may move your cloud by 1 or 2 squares in one of the four
cardinal directions (North, West, South, and East), or leave it in the same position. Diagonal
moves are not allowed. All moves occur at the beginning of the day.

You should not leave an area without rain for a full week (that is, you are allowed at most 6
consecutive days without rain). You don’t have to care about rain on days outside the period
you were given: i.e. you can assume it rains on the whole country the day before the period,
and the day after it finishes.

Input

The input is a sequence of data sets, followed by a terminating line containing only a zero.

A data set gives the number N of days (no more than 365) in the period on a single line, followed by N lines giving the schedule for markets and festivals. The i-th line gives the schedule for
the i-th day. It is composed of 16 numbers, either 0 or 1, 0 standing for a normal day, and 1 a
market or festival day. The numbers are separated by one or more spaces.

Output

The answer is a 0 or 1 on a single line for each data set, 1 if you can satisfy everybody, 0 if there
is no way to do it.

Sample Input

1
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0
0

Output for the Sample Input

0
1
0
1

Solution in Ruby (judge verdict: Runtime Error)

### constant

H = W = 4
HW = H * W

CH = H - 1
CW = W - 1
CHW = CH * CW

MAX_R = 6

DXYS = [
  [0, 0], [1, 0], [2, 0], [0, -1], [0, -2], [-1, 0], [-2, 0], [0, 1], [0, 2]
]

### subroutines

def check_rec(k, cid, r0, r2, r6, r8)
  return true if k >= $n
  return $cache[k][cid][r0][r2][r6][r8] if $cache[k][cid][r0][r2][r6][r8]

  for pos in $ccvrs[cid]
    return ($cache[k][cid][r0][r2][r6][r8] = false) if $fcs[k][pos]
  end

  rr0 = r0 + 1
  rr2 = r2 + 1
  rr6 = r6 + 1
  rr8 = r8 + 1

  case cid
  when 0
    rr0 = 0
  when 2
    rr2 = 0
  when 6
    rr6 = 0
  when 8
    rr8 = 0
  end

  if rr0 > MAX_R || rr2 > MAX_R || rr6 > MAX_R || rr8 > MAX_R
    return ($cache[k][cid][r0][r2][r6][r8] = false)
  end

  for cid0 in $cnbrs[cid]
    if check_rec(k + 1, cid0, rr0, rr2, rr6, rr8)
      return ($cache[k][cid][r0][r2][r6][r8] = true)
    end
  end

  ($cache[k][cid][r0][r2][r6][r8] = false)
end

### main

$ccvrs = []
$cnbrs = CHW.times.map{[]}

for y in (0...CH)
  for x in (0...CW)
    cid = y * CW + x
    cpos = y * W + x
    $ccvrs[cid] = [cpos, cpos + 1, cpos + W, cpos + W + 1]

    DXYS.each do |dx, dy|
      x0 = x + dx
      y0 = y + dy
      if x0 >= 0 && x0 < CW && y0 >= 0 && y0 < CH
        cid0 = y0 * CW + x0
        $cnbrs[cid] << cid0
      end
    end
    $cnbrs[cid].sort!
  end
end
#p $ccvrs
#p $cnbrs

loop do
  $n = gets.to_i
  break if $n == 0

  $fcs = $n.times.map{gets.split.map{|c| c == '1'}}
  #p $fcs

  $cache = $n.times.map{CHW.times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{false}}}}}}

  puts check_rec(0, 4, 0, 0, 0, 0) ? 1 : 0
end