zoukankan      html  css  js  c++  java
  • [codeforces 293]B. Distinct Paths

    [codeforces 293]B. Distinct Paths

    试题描述

    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 (109 + 7).

    输入

    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.

    输出

    Print the number of possible paintings modulo 1000000007 (109 + 7).

    输入示例

    5 6 10
    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

    输出示例

    3628800

    数据规模及约定

    见“输入

    题解

    容易发现当 n + m - 1 > k 时,答案永远是 0,所以真正需要计算的数据范围是 n, m <= 10 且 n + m <= 11,那么这个地图就很小了,最多有 25 个块。

    然后我想状压 dp,发现根本没法转移。。。上网搜了一下题解发现是大爆搜 + 剪枝。。。

    贴个传送门,上面两种剪枝讲得很清楚。(戳这里

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
        if(Head == Tail) {
            int l = fread(buffer, 1, BufferSize, stdin);
            Tail = (Head = buffer) + l;
        }
        return *Head++;
    }
    int read() {
        int x = 0, f = 1; char c = getchar();
        while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
        while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
        return x * f;
    }
    
    #define maxn 15
    #define maxs 1100
    #define MOD 1000000007
    int n, m, k, A[maxn][maxn];
    
    int s[maxn][maxn], ans, col[maxn][maxn], use[maxn];
    int dfs(int x, int y) {
    //	printf("%d %d
    ", x, y);
    //	for(int i = 1; i <= n; i++) {
    //		for(int j = 1; j <= m; j++) printf("%d ", col[i][j]);
    //		putchar('
    ');
    //	}
    //	putchar('
    ');
    	if(x > n) return 1;
    	s[x][y] = s[x-1][y] | s[x][y-1];
    	int cnt = 0;
    	for(int i = 0; i < k; i++) if(s[x][y] >> i & 1) cnt++;
    	if(k - cnt < (n - x) + (m - y) + 1) return 0;
    	if(A[x][y] && (s[x][y] >> A[x][y] - 1 & 1)) return 0;
    	int ans = 0, sum = -1;
    	if(!A[x][y]) {
    		for(int i = 1; i <= k; i++) if(!(s[x][y] >> i - 1 & 1)) {
    			if(!use[i] && sum >= 0) {
    				ans += sum;
    				if(ans >= MOD) ans -= MOD;
    				continue;
    			}
    			s[x][y] ^= (1 << i - 1); use[i]++; col[x][y] = i;
    			int tmp = 0;
    			if(y < m) tmp = dfs(x, y + 1); else tmp = dfs(x + 1, 1);
    			s[x][y] ^= (1 << i - 1); use[i]--; col[x][y] = 0;
    			ans += tmp;
    			if(ans >= MOD) ans -= MOD;
    			if(!use[i]) sum = tmp;
    		}
    	}
    	else {
    		s[x][y] ^= (1 << A[x][y] - 1); use[A[x][y]]++; col[x][y] = A[x][y];
    		if(y < m) ans += dfs(x, y + 1); else ans += dfs(x + 1, 1);
    		s[x][y] ^= (1 << A[x][y] - 1); use[A[x][y]]--; col[x][y] = 0;
    		if(ans >= MOD) ans -= MOD;
    	}
    	return ans;
    }
    
    int main() {
    	n = read(); m = read(); k = read();
    	for(int i = 1; i <= n; i++)
    		for(int j = 1; j <= m; j++) A[i][j] = read(), use[A[i][j]] = 1;
    	
    	printf("%d
    ", dfs(1, 1));
    	
    	return 0;
    }
    
  • 相关阅读:
    idea 红线 并提示idea cant resolve symbol
    shell 脚本不能执行多条?何解
    怎么在linux下创建一个可运行脚本?
    java linux sdk1.8
    Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:co
    什么是结构化数据和非结构化数据?什么是数据清洗?
    Installation failed with message Failed to finalize session: INSTALL_FAILED_TEST_ONLY:installPackageLI.
    Tensorflow-目标检测之yolov3训练自己的模型
    Tensorflow-目标检测之yolov3训练自己的模型
    《C#高效编程》读书笔记11-理解短小方法的优势
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5813195.html
Copyright © 2011-2022 走看看