[CodeForces299D]Distinct Paths

Description

You have a rectangular n × m-cell board. Some cells are already painted some of k colors. You need to paint each uncolored cell one of the k colors so that any path from the upper left square to the lower right one doesn't contain any two cells of the same color. The path can go only along side-adjacent cells and can only go down or right.

Print the number of possible paintings modulo 1000000007 (10^9 + 7).
Input
---
The first line contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10). The next n lines contain m integers each — the board. The first of them contains m uppermost cells of the board from the left to the right and the second one contains m cells from the second uppermost row and so on. If a number in a line equals 0, then the corresponding cell isn't painted. Otherwise, this number represents the initial color of the board cell — an integer from 1 to k.

Consider all colors numbered from 1 to k in some manner.
Output
---
Print the number of possible paintings modulo 1000000007 (10^9 + 7).
Sample Input
---
2 2 4
0 0
0 0
Sample Output
---
48
题解
---
dfs
自由元答案相同直接返回
颜色不够时直接返回

#include <cstdio>
#include <iostream>

using namespace std;
typedef long long LL;
#define rep(a, b, c) for(register int a = b; a <= c; a++)

template<typename comparable>
void readint(comparable & x)
{
    comparable num = 0, fix = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') fix = -1;
        ch = getchar();
    }
    while( ch >= '0' && ch <= '9')
    {
        num = (num << 3) + (num << 1) + (ch ^ '0');
        ch = getchar();
    }
    x = num * fix;
}

const int mod = 1e9 + 7;
int cnt[1024], bin[15];
int N, M, K;
int vis[15];
int arr[1005][1005];
int sta[1005][1005];

LL dfs(int x, int y)
{
    if(y == M + 1) x = x + 1, y = 1;
    if(x == N + 1) return 1;
    
    int S = 0;
    if(x != 1) S |= sta[x - 1][y];
    if(y != 1) S |= sta[x][y - 1];
    
    int T = (~ S) & (bin[K] - 1);
    LL temp = -1, res = 0;
    
    if(cnt[T] < N - x + M - y + 1) return 0;
    
    for(int i = 0; i < K; i++) if(T & bin[i])
    {
        int c = i + 1;
        if(arr[x][y] != 0 && arr[x][y] != c) continue;
        vis[c]++, sta[x][y] = S | bin[i];
        if(vis[c] == 1)
        {
            if(temp == -1) temp = dfs(x, y + 1);
            (res += temp) %= mod;
        }
        else (res += dfs(x, y + 1)) %= mod;
        vis[c]--;
    }
    
    return res;
}

int main(int argc, char * argv[])
{
    rep(i, 1, 1023) cnt[i] = cnt[i >> 1] + (i & 1);
    bin[0] = 1; rep(i, 1, 10) bin[i] = bin[i - 1] << 1;
    
    readint(N), readint(M), readint(K);
    rep(i, 1, N) rep(j, 1, M)
    {
        readint(arr[i][j]);
        if(arr[i][j]) vis[arr[i][j]]++;
    }
    
    if(N + M - 1 > K) printf("0");
    else cout << dfs(1, 1);
    
    return 0;
}

转载于:https://www.cnblogs.com/ljzalc1022/p/8805646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值