• [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.

    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

    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;
    }
    
  • 相关阅读:
    求正方形的面积和周长
    面向对象练习——校园管理系统
    python--面向对象(最全讲解)
    git删除远程仓库的文件或目录
    git设置忽略文件和目录
    计算器
    织梦标签整理
    比较恶心的360老年版浏览器 文件导出下载
    织梦文章列表样式多元化实现
    php函数整理
  • 原文地址:https://www.cnblogs.com/ljzalc1022/p/8805646.html
Copyright © 2020-2023  润新知