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 <stdio.h>

int schedule[365];
int number_of_days;
int map[3][3][365];//[x][y][]
int dfs(int x, int y, int day, int* moisture);
void print_map(int x, int y, int day, int* dryness);
/**
void print_binary(int bin){
    unsigned int bit=1<<15;
    for(int i=0;i<16;i++){
        if(bin&bit){
            printf("1");
        }else{
            printf("0");
        }
        bit>>=1;
    }
    printf("\n");
}
**/

int main(void){
    int buf[16];
    int dryness[16];
    while(scanf("%d%*c",&number_of_days),number_of_days){
        //????????????????????????????????§???????????????
        int i,j;
        for(i=0;i<number_of_days;i++){
            scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", buf, buf+1, buf+2, buf+3, buf+4, buf+5, buf+6, buf+7, buf+8, buf+9, buf+10, buf+11, buf+12, buf+13, buf+14, buf+15);
             for(j=15;~j;j--){
                if(buf[j]==1){
                    schedule[i]++;
                }
                if(j)schedule[i]<<=1;
            }           
            /**
            fread(buf, sizeof(char), 32, stdin);
            for(j=15;~j;j--){
                if(buf[j*2]=='1'){
                    schedule[i]++;
                }
                if(j)schedule[i]<<=1;
            }
            **/
        }
        //????????????????????§???????¨?
        int x,y;
        for(i=0;i<number_of_days;i++){
            for(x=0;x<3;x++){
                for(y=0;y<3;y++){
                    if( ((0b110011)<<(x+4*y))&schedule[i]){
                        map[x][y][i]=0;//??????????????????
                    }else{
                        map[x][y][i]=1;//?????????
                    }
                }
            }
        }
        /**
        for(i=0;i<number_of_days;i++){
            printf("day %d\n",i);
            for(y=0;y<4;y++){
                for(x=0;x<4;x++){
                    printf("%d", schedule[i]&(1<<(4*y+x))? 1:0 );
                }
                printf("\n");
            }
            for(y=0;y<3;y++){
                for(x=0;x<3;x++){
                    printf("%d", map[x][y][i] );
                }
                printf("\n");
            }
            printf("\n");
        }
        **/
        //??±???????????¢?´¢
        for(i=0;i<16;i++){
            dryness[i]=0;
        }
        if(map[1][1][0]==0){
            printf("0\n");
            continue;
        }
        if(~dfs(1, 1, 0, dryness)){
            //print_map(1, 1, 0, dryness);
            printf("1\n");
        }else{
            printf("0\n");
        }
    }
    return 0;
}
void print_map(int x, int y, int day, int* dryness){
    char map[16];
    int i;
    for(i=0;i<16;i++){
        map[i]='0'+dryness[i];
    }
    map[x+4*y]='X';
    map[x+4*y+1]='X';
    map[x+4*y+4]='X';
    map[x+4*y+5]='X';
    printf("Day %d :\n",day);
    for(y=0;y<4;y++){
        for(x=0;x<4;x++){
            printf("%c",map[x+4*y]);
        }
        printf("\n");
    }
    printf("\n");
}

int dfs(int x, int y, int day, int* dryness){
    //??¨???????????????
    int drystats[16];
    int i;
    for(i=0;i<16;i++){
        drystats[i]=dryness[i]+1;
    }
    drystats[x+4*y]=0;
    drystats[x+4*y+1]=0;
    drystats[x+4*y+4]=0;
    drystats[x+4*y+5]=0;
    for(i=0;i<16;i++){
        if(drystats[i]>6) return -1;//??±???
    }
    if(day+1==number_of_days){
        return 0;//??????
    }
    
    //???????§?????????????(?????°)
    for(i=-2;i<3;i++){
        if(x+i>=0&&x+i<3){
            if(map[x+i][y][day+1]){
                if(~dfs(x+i,y,day+1,drystats)){
                    //print_map(x+i,y,day+1,drystats);
                    return 0;
                }
            }
        }
        if(y+i>=0&&y+i<3){
            if(map[x][y+i][day+1]){
                if(~dfs(x,y+i,day+1,drystats)){
                    //print_map(x,y+i,day+1,drystats);
                    return 0;
                }
            }
        }
    }
    return -1;
}