列表

详情


NC51038. 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 x 4 square areas, denoted by their numbers. 

Your cloud is of size 2 x 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. 

输入描述

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.

输出描述

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.

示例1

输入:

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

输出:

0
1
0
1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 49ms, 内存消耗: 15352K, 提交时间: 2020-07-21 17:33:28

//Author:XuHt
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 370;
int n, a[N];
bool v[N][10][8][8][8][8];
int dx[10] = {0,0,1,2,-1,-2,0,0,0,0};
int dy[10] = {0,0,0,0,0,0,1,2,-1,-2};
struct P {
    int d[5];
} st;
 
int get(int x, int y) {
    return 1 << (16 - ((x - 1) * 4 + y));
}
 
bool pd(int day, int x, int y, P now) {
    for (int i = 1; i < 5; i++) if (now.d[i] == 7) return 0;
    int flag = get(x, y) | get(x, y + 1) | get(x + 1, y) | get(x + 1, y + 1);
    if (flag & a[day]) return 0;
    if (v[day][(x-1)*3+y][now.d[1]][now.d[2]][now.d[3]][now.d[4]]) return 0;
    return v[day][(x-1)*3+y][now.d[1]][now.d[2]][now.d[3]][now.d[4]] = 1;
}
 
bool dfs(int day, int x, int y, P now) {
    if (day == n + 1) return 1;
    if (!pd(day, x, y, now)) return 0;
    for (int i = 1; i < 10; i++) {
        P nxt = now;
        for (int j = 1; j < 5; j++) ++nxt.d[j];
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 1 || nx > 3 || ny < 1 || ny > 3) continue;
        if (nx == 1) {
            if (ny == 1) nxt.d[1] = 0;
            else if (ny == 3) nxt.d[2] = 0;
        } else if (nx == 3) {
            if (ny == 1) nxt.d[3] = 0;
            else if (ny == 3) nxt.d[4] = 0;
        }
        if (dfs(day + 1, nx, ny, nxt)) return 1;
    }
    return 0;
}
 
int main() {
    while (cin >> n && n) {
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= 16; j++) {
                int w;
                cin >> w;
                (a[i] <<= 1) |= w;
            }
        memset(v, 0, sizeof(v));
        for (int i = 1; i < 5; i++) st.d[i] = 1;
        puts(dfs(1, 2, 2, st) ? "1" : "0");
    }
    return 0;
}

C++ 解法, 执行用时: 144ms, 内存消耗: 31296K, 提交时间: 2022-02-06 13:17:00

#include<bits/stdc++.h>
using namespace std;
int n,state[366][4][4],st[366][3][3][7][7][7][7];
struct Node{int day,x,y,s0,s1,s2,s3;};
int bfs(){
    if(state[1][1][1]||state[1][1][2]||state[1][2][1]||state[1][2][2])return 0;
    memset(st,0,sizeof st);
    queue<Node>q;
    q.push(Node{1,1,1,1,1,1,1});
    st[1][1][1][1][1][1][1]=1;
    int dx[9]={1,2,0,0,-1,-2,0,0,0},dy[9]={0,0,1,2,0,0,-1,-2,0};
    while(q.size()){
        Node t=q.front();
        q.pop();
        if(t.day==n)return 1;
        for(int i=0;i<9;i++){
            int x=t.x+dx[i],y=t.y+dy[i];
            if(x<0||x>2||y<0||y>2)continue;
        	//auto s=state[t.day+1];
            if(state[t.day+1][x][y]||state[t.day+1][x+1][y]||state[t.day+1][x+1][y+1]||state[t.day+1][x][y+1])continue;
            int s0=t.s0,s1=t.s1,s2=t.s2,s3=t.s3;
            if(!x&&!y)s0=0;
            else if(++s0==7)continue;
            if(!x&&y==2)s1=0;
            else if(++s1==7)continue;
            if(x==2&&!y)s2=0;
            else if(++s2==7)continue;
            if(x==2&&y==2)s3=0;
            else if(++s3==7)continue;
            if(st[t.day+1][x][y][s0][s1][s2][s3])continue;
            st[t.day+1][x][y][s0][s1][s2][s3]=1;
            q.push({t.day+1,x,y,s0,s1,s2,s3});
        }
    }
    return 0;
}
int main(){
    while(cin>>n,n){for(int i=1;i<=n;i++)for(int j=0;j<4;j++)for(int k=0;k<4;k++)cin>>state[i][j][k];cout<<bfs()<<"\n";}
    return 0;
}

上一题