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: Accepted)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<stack>
#include<algorithm>
#include<queue>
#include<string>
#define OK 1
#define INF 2147483647
#define LINF 9223372036854775807LL
#define DINF 100000000
#define LL long long
using namespace std;
typedef struct
{
	int cnt[4][4];
}HNode;
vector<HNode>H[366][4][4];
int n,map[365][4][4];
int biao[4][4][4][2];
const int di[] = { 0, 0, 0, -1, -2, 0, 0, 1, 2 };
const int dj[] = { 0, -1, -2, 0, 0, 1, 2, 0, 0 };
bool dfs(int cur, int ci, int cj, int cnt[4][4]);
bool judge(int cur, int ci, int cj, int cnt[4][4]);

int main()
{
	int i,j,k,cnt[4][4];
	while (scanf("%d", &n) > 0)
	{
		if (n == 0) break;
		for (k = 0; k < n; k++)
		{
			for (i = 0; i < 4; i++)
			{
				for (j = 0; j < 4; j++) H[k][i][j].clear();
			}
		}
		for (k = 0; k < n; k++)
		{
			for (i = 0; i < 4; i++)
			{
				for (j = 0; j < 4; j++) scanf("%d", &map[k][i][j]);
			}
		}
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 4; j++) cnt[i][j] = 1;
		}
		cnt[1][1] = cnt[1][2] = cnt[2][1] = cnt[2][2] = 0;
		judge(0, 1, 1, cnt);
		if (dfs(0, 1, 1, cnt) == true) printf("1\n");
		else printf("0\n");
	}
	return 0;
}

bool judge(int cur, int ci, int cj, int cnt[4][4])
{
	int k,j,i, len;
	bool flag = false;
	HNode e;
	for (k = 0, len = H[cur][ci][cj].size(); k < len; k++)
	{
		e = H[cur][ci][cj][k];
		flag = true;
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 4; j++)
			{
				if (e.cnt[i][j] != cnt[i][j]) flag = false;
			}
		}
		if (flag == true) break;
	}
	if (flag == false)
	{
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 4; j++) e.cnt[i][j] = cnt[i][j];
		}
		H[cur][ci][cj].push_back(e);
		return false;
	}
	else return true;
}

bool dfs(int cur,int ci,int cj,int cnt[4][4])
{
	int k,next_ci,next_cj,i,j,nextCnt[4][4];
	if (map[cur][ci][cj] == 1 || map[cur][ci][cj + 1] == 1 || map[cur][ci + 1][cj] == 1 || map[cur][ci + 1][cj + 1] == 1) return false;
	if (cur == n - 1)  return true;
	for (k = 0; k < 9; k++)
	{
		next_ci = ci + di[k];
		next_cj = cj + dj[k];
		if (next_ci < 0 || next_ci + 1 >= 4 || next_cj < 0 || next_cj + 1 >= 4) continue;
		memcpy(nextCnt, cnt, sizeof(nextCnt));
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 4; j++) nextCnt[i][j]++;
		}
		nextCnt[next_ci][next_cj] = nextCnt[next_ci][next_cj + 1] = 0;
		nextCnt[next_ci + 1][next_cj] = nextCnt[next_ci + 1][next_cj + 1] = 0;
		bool flag = true;
		for (i = 0; i < 4; i++)  //判断下雨天数
		{
			for (j = 0; j < 4; j++)
			{
				if (nextCnt[i][j] == 7) flag = false;
			}
		}  
		if (flag == false) continue;
		if (judge(cur + 1, next_ci, next_cj, nextCnt) == true) continue;
		if (dfs(cur + 1, next_ci, next_cj, nextCnt) == true) return true;
 	}
	return false;
}