zoukankan      html  css  js  c++  java
  • [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;
    }
    
  • 相关阅读:
    《区块链100问》第51集:区块链资产有匿名性
    《区块链100问》第52集:区块链资产能去中心化记账
    《区块链100问》第53集:区块链资产不可复制
    day 5 模块导入、常用模块os shutil sys commands subprocess hashlib json pickle zipfile traceback random datetime pathlib
    apache中配置php支持模块模式、cgi模式和fastcgi模式的实验
    day3--集合、文件操作、字符编码与转换、函数(递归,lambda,filter,map)、字典排序
    练习题目 :if for while else range、xrange、zip
    解决mySQL占用内存超大问题
    my.cnf重要配置参数说明
    chattr与lsattr命令详解
  • 原文地址:https://www.cnblogs.com/ljzalc1022/p/8805646.html
Copyright © 2011-2022 走看看