Ural 1519. Formula 1 优美的插头DP
今天早上学了插头DP的思想和最基础的应用,中午就开始敲了,岐哥说第一次写不要看别人代码,利用自己的理解一点点得写出来,这样才锻炼代码能力!于是下午慢慢地构思轮廓,一点点地敲出主体代码,其实是很磨蹭的,由于要考虑好多东西,而且昨晚2点睡的有点困,最后终于磨蹭出来了,第一次的代码搓没关系,自己写的才重要。然后果然不出我所料,调试到了晚上才A了(一个郁闷的错误)。。。A的感觉真的是爽呀,虽然搞了差不多一天。当然自己写了自己想的代码后也要把代码优化,不然队友看不懂自己代码就囧了。。。
插头DP,建议大家想学的好好看看陈丹琦的国家集训队论文,这是个优美的DP。
http://www.docin.com/p-46797997.html
#include <stdio.h>
#include <string.h> #define LL __int64 const int mod = 10007; // 哈希表
struct HASH{
int head[mod+10], E, next[80000];
LL val[80000], cnt[80000]; void init() {
memset(head, -1, sizeof(head));
E = 0;
} int findhash(LL x) {
return (x%mod + mod)%mod;
} void add(LL x, LL sum) {
int u = findhash(x);
for(int i = head[u];i != -1;i = next[i]) if(val[i] == x) {
cnt[i] += sum;
return ;
}
val[E] = x;
cnt[E] = sum;
next[E] = head[u];
head[u] = E++;
} }biao1, biao2; int c[22], n, m, d[22];
// 编码
void get(LL x) {
for(int i = m+1;i >= 1; i--) {
c[i] = x&7;
x /= 8;
}
}
// 解码
LL getval() {
LL ret = 0;
for(int i = 1;i <= m+1; i++) {
ret |= d[i];
ret *= 8;
}
ret /= 8;
return ret ;
}
// 转化成最小表示法
void change() {
int vis[22];
memset(vis, 0, sizeof(vis));
int num = 1;
for(int i = 1;i <= m+1;i ++) {
if(!d[i]) continue;
if(!vis[d[i]]) {
vis[d[i]] = num;
d[i] = num++;
}
else {
d[i] = vis[d[i]];
}
}
} void fuzhi() {
for(int i = 1;i <= m+1;i ++) d[i] = c[i];
} char s[22][22]; int main() {
int i, j, k, l;
while(scanf("%d%d", &n, &m) != -1) {
for(i = 1;i <= n; i++)
scanf("%s", s[i]+1);
int tot = 0;
for(i = 1;i <= n; i++)
for(j = 1;j <= m; j++)
if(s[i][j] == '.') tot++;
if(tot%2==1 || tot < 4) {
puts("0");
continue;
}
int tox = -1, toy = -1;
for(i = 1;i <= n; i++)
for(j = 1;j <= m; j++) if(s[i][j] == '.') {
tox = i;
toy = j;
}
biao1.init();
biao1.add(0, 1);
LL ans = 0;
for(i = 1;i <= n; i++){
for(j = 0;j <= m; j++){
biao2.init();
for(l = 0;l < biao1.E; l++) {
get(biao1.val[l]);
if(j == m) {
for(int ii = 2;ii <= m+1; ii++) d[ii] = c[ii-1];
d[1] = 0;
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
continue;
}
if(c[j+1] && !c[j+2]) { // 有左插头无上插头
if(s[i][j+1] != '.') continue;
if(j+2 <= m) {
fuzhi();
d[j+1] = 0;d[j+2] = c[j+1];
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
if(i < n) {
fuzhi();
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
}
else if(!c[j+1] && c[j+2]) { // 有上插头无左插头
if(s[i][j+1] != '.') continue;
if(i < n) {
fuzhi();
d[j+1] = c[j+2]; d[j+2] = 0;
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
if(j+2 <= m) {
fuzhi();
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
}
else if(!c[j+1] && !c[j+2]) { // 左和上都无插头
if(s[i][j+1] != '.') {
fuzhi();
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
continue;
}
if(j+2 <= m && i < n) {
fuzhi();
d[j+1] = d[j+2] = 13;
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
}
else { // 左和上都有插头 , 要判断左和上插头是否连通
if(c[j+2] == c[j+1]) {
int tot = 0;
for(int ii = 1;ii <= m+1; ii++) if(c[ii])
tot++;
if(tot == 2 && i == tox && j+1 == toy) ans += biao1.cnt[l];
}
else {
if(s[i][j+1] != '.') continue;
fuzhi();
for(int ii = 1;ii <= m+1; ii++) if(ii != j+1 && ii != j+2 && d[ii] == d[j+1]) {
d[ii] = d[j+2];
break;
}
d[j+1] = d[j+2] = 0;
change();
LL now = getval();
biao2.add(now, biao1.cnt[l]);
}
}
}
biao1 = biao2;
}
}
printf("%I64d\n", ans);
}
return 0;
}
Ural 1519. Formula 1 优美的插头DP的更多相关文章
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 942 Solved: 356[Submit][Sta ...
- ural 1519 Formula 1(插头dp)
1519. Formula 1 @ Timus Online Judge 干了一天啊!!!插头DP入门. 代码如下: #include <cstdio> #include <cstr ...
- URAL 1519 Formula 1(插头DP,入门题)
Description Background Regardless of the fact, that Vologda could not get rights to hold the Winter ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- bzoj1814 Ural 1519 Formula 1(插头DP)
对插头DP的理解还不是很透彻. 先说一下肤浅的理解吧. 插头DP使用范围:指数级复杂度,且适用于解决网格图连通性问题,如哈密顿回路等问题.插头一般指每相邻2个网格的接口. 题目难度:一般不可做. 使用 ...
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
随机推荐
- 嵌入式web server——Goahead移植要点
前言 在嵌入式设备中,在没有液晶显示的情况下,可以使用web来访问设备,查看设备的运行状态以及进行参数设置,类似于路由器设置.网上有很多关于各种web server的优劣的评论,在此不讨论,只是介绍其 ...
- 简单的背包问题(入门)HDU2602 HDU2546 HDU1864
动态规划,我一直都不熟悉,因为体量不够,所以今天开始努力地学习学习. 当然背包从01开始,先选择了一个简单的经典的背包HDU2602. Many years ago , in Teddy's home ...
- php 解决大流量网站访问量问题
当一个网站发展为知名网站的时候(如新浪,腾讯,网易,雅虎),网站的访问量通常都会非常大,如果使用虚拟主机的话,网站就会因为访问量过大而引起 服务器性能问题,这是很多人的烦恼,有人使用取消RSS等错误的 ...
- python隐含的特性
本文源自(http://stackoverflow.com/questions/101268/hidden-features-of-python)希望介绍Python非常有用,而比较忽视的Python ...
- JQuery.imgAreaSelect 参数说明
imgAreaSelect 参数说明: 参数 描述 aspectRatio 设定选取区域的显示比率,如:”4:3“ autoHide 如果设置为true,当选择区域选择结束时消失,默认值为:false ...
- PHP获取当前页面完整url地址,包括参数的函数
//php获取当前访问的完整url地址 function get_current_url(){ $current_url='http://'; if(isset($_SERVER['H ...
- service httpd restart失败解决方法(小记)
1.首先查看错误日志 /var/log/message看看具体问题.如果一时难以判定直接使用下面一招 2.直接中断http服务,killall -9 httpd, 然后重启http,service ...
- C# mvc 验证码2
public class ValidateCode { /// <summary> /// 產生圖形驗證碼. /// </su ...
- 当xcode里点运行出现treating unicode character as whites
可能是由于粘贴网页上的代码的时候两行之间的回车引起的,两行之间重新输入回车就行......删掉重新写一遍就ok了 引入网页上的回车时 可能 网页对其格式做了处理,所以Xcode 不认识了
- Gogs:可能是比Gitlab更好的选择
Gitlab是一个很棒的Git托管服务,几乎像GitHub一样强大. 但是,有没有能和Gitlab/Github媲美但操作更简单的项目呢?我认为 Gogs 是很好的选择. 简介 现在,GitHub已经 ...