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 C++ (judge verdict: Time Limit Exceeded)

#include<iostream>
#include<set>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)


int dx[9]={0,1,2,-1,-2,0,0, 0, 0};
int dy[9]={0,0,0, 0, 0,1,2,-1,-2};

int in[365][4][4];
class state{
public:
  int y,x,day;
  char m[4][4];
  int s[4][4];
  state(){
    rep(i,4)rep(j,4)m[i][j]=0;
    y=x=1;day=0;
  }
  bool operator<(const state &a)const;
  bool move(int i);
};

bool state::operator<(const state & a)const{
  if ( day != a.day)return day;
  //rep(i,4)rep(j,4)if (m[i][j]!=a.m[i][j])return m[i][j]<a.m[i][j];
  rep(i,4)rep(j,4)if (s[i][j]!=a.s[i][j])return s[i][j]<a.s[i][j];
  return false;
}

bool state::move(int i){
  int nex=x+dx[i],ney=y+dy[i];
  if ( nex<0||ney<0||nex+1>=4||ney+1>=4)return false;
  x=nex;
  y=ney;
  rep(i,4)rep(j,4)m[i][j]++;
  REP(i,y,y+2){
    REP(j,x,x+2)m[i][j]=0;
  }

  rep(i,4){
    rep(j,4){
      if ( m[i][j]==0)s[i][j]=0;
      else if ( m[i][j]<6)s[i][j]=2;
      else if (m[i][j]==6)s[i][j]=3;
      else if ( m[i][j]==7)s[i][j]=4;
    }
  }

  return true;
}

bool check(state &now){
  rep(i,4){
    rep(j,4){
      if ( now.m[i][j]==0 && in[now.day][i][j]==1)return false;
      if ( now.m[i][j]==7)return false;
    }
  }
  return true;
}

bool solve(int lim,state now,set<state>&S){
  if ( S.find(now) != S.end())return false;
  S.insert(now);
  if ( now.day == lim)return true;

  rep(i,9){
    state next=now;
    next.day++;
    if (next.move(i) == true && check(next)&&solve(lim,next,S))
      return true;
  }

  return false;
}

main(){
  int d;
  while(cin>>d&&d){
    rep(i,d){
      rep(j,4)rep(k,4)cin>>in[i][j][k];
    }
    state ini;
    set<state>S;
    
    
    if ( in[0][1][1]==0&&in[0][1][2]==0&&
	 in[0][2][1]==0&&in[0][2][2]==0&& solve(d,ini,S))puts("1");
    else puts("0");
  }
}