CF293B Distinct Paths题解
CF293B Distinct Paths
题意
给定一个\(n\times m\)的矩形色板,有kk种不同的颜料,有些格子已经填上了某种颜色,现在需要将其他格子也填上颜色,使得从左上角到右下角的任意路径经过的格子都不会出现两种及以上相同的颜色。路径只能沿着相邻的格子,且只能向下或者向右。
计算所有可能的方案,结果对 \(1000000007 (10^9 + 7)\)
输入及输出格式
输入格式
第一行,三个整数$ n, m, k (1 \le n, m \le 1000, 1 \le k \le 10)$;
接下来\(n\)行,每行包含\(m\)个整数,表示颜色。其中\(0\)表示未涂色,非\(0\)表示颜色的编号, 颜色编号为\(1\)到\(k\)。
输出格式
一行,一个整数,表示涂色方案对$ 1000000007 (10^9 + 7)$求模的结果。
样例
此处就不挂了:传送门
思路
看似数据很大:\(n, m, k (1 \le n, m \le 1000, 1 \le k \le 10)\),但是,\(k<n+m-1\) 时,可以直接输出\(0\)。因为无法走完一条路径(一条路径长度为\(n+m-1\),因为是只能向下、向右走)。
那么实际数据范围很小,大概是$n+m-1 \le 10 $左右吧。
这么小的范围很容易就可想到\(dfs\)。这里有两个优化,一个是如果搜到一半,发现剩下的颜色不够用了就直接\(return\)。还有一个就是利用颜色\(A\)与颜色\(B\)的先后次序问题,路径\(AB\)与路径\(BA\)并不是同一种方案,所以搜索时如果搜到是第一次时,就可以直接乘\(now\)就可以省去很多\(dfs\)。
代码很丑,勿喷。
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cstring>
#include<cmath>
#define MOD 1000000007
using namespace std;//恶心的头文件
inline int read(){
char ch=getchar();int res=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
return res*f;
}//读入优化
inline void write(int zx){
if(zx<0) zx=-zx,putchar('-');
if(zx<10) putchar(zx+'0');
else{
write(zx/10);
putchar(zx%10+'0');
}
}//输出优化
int n,m,k,cnt[50],a[50][50],sum,f[30][30],ps,ans,vv;
int dfs(int x,int y){
if(y==m+1){return dfs(x+1,1);}
if(x==n+1) return 1;
int S=0,num=0,mar=0,res=0,las=0;
f[x][y]=f[x-1][y]|f[x][y-1];
for(int i=1;i<=k;i++){
if(!(f[x][y]&(1<<i-1))) ++num;
}
if(num<n+m-x-y+1) return 0;//第一个优化
if(a[x][y]==0){
for(int i=1;i<=k;i++){
if(!(f[x][y]&(1<<i-1))){
if(cnt[i]==0){
if(mar) res+=las,res%=MOD;//第二个优化
else{
mar=1;
cnt[i]++;
f[x][y]|=1<<i-1;
las=dfs(x,y+1);
f[x][y]^=1<<i-1;
cnt[i]--;
res+=las;
res%=MOD;
}
continue ;
}
cnt[i]++;
f[x][y]|=1<<i-1;
res+=dfs(x,y+1);
f[x][y]^=1<<i-1;
cnt[i]--;
res%=MOD;
}
}
}else{
if(!(f[x][y]&(1<<a[x][y]-1))){
f[x][y]|=1<<a[x][y]-1;
res+=dfs(x,y+1);
f[x][y]^=1<<a[x][y]-1;
res%=MOD;
}
}
return res;
}
int main(){
n=read();m=read();k=read();
vv=n+m-1;
if(k<vv){//开始先特判
puts("0");
return 0;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=read();
cnt[a[i][j]]++;
}
}
ans=dfs(1,1);
cout<<ans<<endl;
return 0;
}
CF293B Distinct Paths题解的更多相关文章
- CF293B. Distinct Paths
B. Distinct Paths time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CF293B Distinct Paths 搜索
传送门 首先数据范围很假 当\(N + M - 1 > K\)的时候就无解 所以对于所有要计算的情况,\(N + M \leq 11\) 超级小是吧,考虑搜索 对于每一个格子试填一个数 对于任意 ...
- [codeforces 293]B. Distinct Paths
[codeforces 293]B. Distinct Paths 试题描述 You have a rectangular n × m-cell board. Some cells are alrea ...
- [CF293B]Distinct Paths_搜索_剪枝
Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- World Tour Finals 2019 D - Distinct Boxes 题解
太神了,专门写一篇题解 qwq 简要题意:给你 \(R\) 个红球和 \(B\) 个蓝球,你要把它们放到 \(K\) 个箱子里,要求没有两个箱子完全相同(即两种球个数就相同),求 \(K\) 的最大值 ...
- POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...
- POJ3177:Redundant Paths——题解
http://poj.org/problem?id=3177 明显要求桥的一道题. (因为有桥就说明只能从那一条路走,换句话说就是只有一种方法) 求完桥后按照结论(加几条边成双连通图的结论,不会请ba ...
- SPOJ694/DISUBSTR:Distinct Substrings——题解
https://vjudge.net/problem/SPOJ-DISUBSTR https://www.luogu.org/problemnew/show/SP694 http://www.spoj ...
随机推荐
- tomcat 性能检测
一.jconsole 1.tomcat在windows上,start方式启动 在catalina.bat 文件中的:doRun和:doStart下添加以下代码 (没有换行) set JAVA_OPTS ...
- Perl file checking --- How to get information about a file
There are some short expressions in Perl that allow you to test files, which is handy if you want to ...
- NYOJ 数独 DFS
数独 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一 ...
- Centos7一键编译安装zabbix-4.0.2
##只针对centos7的系统有效,centos6无效,mysql zabbix用户:zabbix,密码:zabbix;建议用全新的centos7服务器 软件版本: (nginx-1.14.2.php ...
- 把数组存入到cookie中
$arr = array(1,2,3); // 把数组序列化之后,存入到cookie中 $arr_str = serialize($arr); // 序列化数组 setcookie('a',$arr_ ...
- Fiddler 使用
一.模拟post请求 User-Agent: FiddlerContent-Type: application/json; charset=utf-8Content-Length: 138Conten ...
- windows git gui右键sublime打开当前文件编辑
git安装目录\Git\libexec\git-core\git-gui.tcl的 proc create_common_diff_popup 下追加: $ctxm add command \ -la ...
- 最小主义:我的Musca桌面环境
我现在有一个非常简单实用的桌面环境了:Musca + conky + trayer. 当然Musca运行时需要dmenu,其实也不是非dmenu不可,据说 dzen 也不错. 我现在用的是dmenu. ...
- 20145209 2016-2017-2 《Java程序设计》第7周学习总结
20145209 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 read()每次读入一个字节. eg:short2个字节,2=0x0201,读入后要0x & ...
- caffe设计网络教程(一)
假设现在我们要设计一个基于VGG的网络,主要考虑的问题是可否修改VGG类似于resnet那样,应该怎么修改?更具体来说,我们需要在VGG网络上考虑eltwise层,现在我们有三种方案,如下: 方案一: ...