[CF293B]Distinct Paths_搜索_剪枝
Distinct Paths
题目链接:http://codeforces.com/problemset/problem/293/B
数据范围:略。
题解:
带搜索的剪枝....
想不到吧.....
但是剪枝也比较简单,就是能想到的剪枝都加上能过的那种搜索题。
代码:
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
const int mod = 1000000007 ;
int Log[1100], n, m, k, a[21][21], f[21][21], v[21];
int dfs(int x, int y) {
if (y == m + 1) {
x ++ , y = 1;
}
if (x == n + 1) {
return 1;
}
int s = f[x - 1][y] | f[x][y - 1], calc = -1, re = 0;
int S = ~s & ((1 << k) - 1);
if (n + m - x - y + 1 > Log[S]) {
return 0;
}
for (int t = 0; t < k; t ++ ) {
if (S & (1 << t)) {
if (a[x][y] == 0 || a[x][y] == t + 1) {
v[t + 1] ++ ;
f[x][y] = s | (1 << t);
if (v[t + 1] == 1) {
if (calc == -1) {
calc = dfs(x, y + 1);
}
re += calc;
}
else {
re += dfs(x, y + 1);
}
if (re >= mod) {
re -= mod;
}
v[t + 1] -- ;
}
}
}
return re;
}
int main() {
// setIO("search&force");
for (int i = 1; i < 1024; i ++ ) {
Log[i] = Log[i >> 1] + (i & 1);
}
// int T;
// cin >> T ;
// while (T -- ) {
// memset(v, 0, sizeof v);
// memset(f, 0, sizeof f);
cin >> n >> m >> k ;
if (n + m - 1 > k) {
puts("0");
return 0;
}
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= m; j ++ ) {
scanf("%d", &a[i][j]);
v[a[i][j]] ++ ;
}
}
cout << dfs(1, 1) << endl ;
return 0;
}
[CF293B]Distinct Paths_搜索_剪枝的更多相关文章
- CF293B Distinct Paths 搜索
传送门 首先数据范围很假 当\(N + M - 1 > K\)的时候就无解 所以对于所有要计算的情况,\(N + M \leq 11\) 超级小是吧,考虑搜索 对于每一个格子试填一个数 对于任意 ...
- 连连看 HDU - 1175_搜索_剪枝
hdu有毒,考试上 AC 的就是一直 WA- 其实这道题是可以进行初始化来进行优化的,这样的话询问次数是可以达到 10510^5105 的.不过普通的 dfsdfsdfs + 剪枝也是可过的. Cod ...
- CF293B Distinct Paths题解
CF293B Distinct Paths 题意 给定一个\(n\times m\)的矩形色板,有kk种不同的颜料,有些格子已经填上了某种颜色,现在需要将其他格子也填上颜色,使得从左上角到右下角的任意 ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)
题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
- SGU 125 Shtirlits 搜索+可行性剪枝
500ms时限406ms水过…… 直接枚举肯定超时,需要剪枝. 枚举每个格子的元素,检查其左上角和正上方格子是否满足条件,若不满足不必再向下搜索. 在 这里 看到一个更好的方法: 枚举每个格子是哪个相 ...
- USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)
这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...
- P1171 售货员的难题--搜索(剪枝)
题目背景 数据有更改 题目描述 某乡有nn个村庄(1<n \le 201<n≤20),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)s(0<s ...
随机推荐
- LibreOJ #115. 无源汇有上下界可行流
二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...
- dashucoding记录2019.6.8
WordPress网站 网址: https://cn.wordpress.org/ 阿里云市场 https://market.aliyun.com/products/53616009?spm=a2c4 ...
- 配置Jupyter Notebook
配置Jupyter Notebook 1 修改Jupyter Notebook的工作目录 Jupyter默认打开的是用户目录,使用如下步骤自行修改: CMD生成Jupyter配置文件: (python ...
- python 根据时间戳获取秒🐱
print("当前时间: ",time.strftime('%Y.%m.%d %H:%M:%S ',time.localtime(time.time()))) import tim ...
- mysql PS1个性化
method ) $ export MYSQL_PS1="\u@\h [\d]> " method ) mysql> prompt \u@\h [\d]> met ...
- 记录一次腾讯X5内核64位手机初始化失败
之前一直在使用x5内核,只需要一个jar包和so文件就能让webview实现多余原生webview的水平,在32位的手机上能够正常运行,但是到了64位手机上就报如下错误: E/ERROR:: .... ...
- javaWeb项目部署到阿里云服务器
1.云服务器中配置了jdk+eclipse+tomcat 2.将项目导出War到安装的Tomcat的webapps文件夹中: 右击项目->Export->WarFile->Desti ...
- OpenJudge计算概论-买房子
/*================================================================= 买房子 总时间限制: 1000ms 内存限制: 65536kB ...
- Team团队管理执行力
执行力是什么_百度知道https://zhidao.baidu.com/question/144991863.html [图文]如何提高团队执行力 - 百度文库https://wenku.baidu. ...
- 扯扯python的多线程的同步锁 Lock RLock Semaphore Event Condition
我想大家都知道python的gil限制,记得刚玩python那会,知道了有pypy和Cpython这样的解释器,当时听说是很猛,也就意味肯定是突破了gil的限制,最后经过多方面测试才知道,还是那德行… ...