HDU 3377 插头dp
题目大意:
从左上角走到右下角,每个点之多经过一次,取到所有路径上经过点的权值,求最大的权值之和,这里走到右下角就算停止了
这里有个思路是转化成熟悉的回路问题
在上方和右方最外围定义一圈权值为0 , 那么我们相当于从定义以后的左上角开始经过所有外围点形成的回路,那么去掉最外围的0,剩下的就是(1,1)-》(n,n)的权值和了
但是这里最外围的点的插头具有特殊性,每次到最外围的点,你左和上有且仅有一个插头,而且出去的方向必然是下一个最外围的点
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std;
#define ll long long
const int HASH = ;
const int STATE = ;
const int MAXD = ;
int n , m;
int code[MAXD] , mp[MAXD][MAXD];
bool flag[MAXD][MAXD];//标记位,标记当前点能否作为最终点
ll ans = ;
int w[MAXD][MAXD];
struct HASHMAP{
int head[HASH] , next[STATE] , state[STATE] , size;
ll f[STATE]; void init(){
size = ;
memset(head , - , sizeof(head));
} void push_in(int st , ll sum){
int h = st%HASH;
for(int i = head[h] ; ~i ; i=next[i]){
if(st == state[i]){
f[i]=max(f[i] , sum); //这题目为求最大值
return ;
}
}
f[size]=sum;
state[size] = st;
next[size] = head[h];
head[h] = size++;
}
}hashmap[]; int num = ;//记录共有的插头数量
void decode(int *code , int m , int st)
{
num = ;
for(int i=m ; i>= ; i--){
code[i] = st&;
st>>=;
if(code[i]) num++;
}
} int encode(int *code , int m)
{
int st=;
for(int i= ; i<=m ; i++){
st<<=;
st |= code[i];
}
return st;
} void shift(int *code , int m) //换行,可理解为将最右侧轮廓线换到了下一行的最左侧
{
for(int i=m ; i>= ; i--) code[i] = code[i-];
code[] = ;
} void dpblank(int i , int j , int cur)
{
int k , left , up;
for(k= ; k<hashmap[cur].size ; k++){
decode(code , m , hashmap[cur].state[k]);
left = code[j-];
up = code[j];
if(!left && !up){
if(mp[i][j]==) continue;
if(mp[i][j]==){
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
}
if(mp[i][j+] && mp[i+][j]){
code[j-] = , code[j] = ;
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
}
else if(!left && up){
if(mp[i][j]== && (up!=)) continue;
else if(mp[i][j]==){
if(mp[i][j+]==) hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
else{
code[j-] = up , code[j] = ;
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
continue;
}
if(mp[i][j+]) hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
if(mp[i+][j]){
code[j-] = up , code[j] = ;
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
}
else if(left && !up){
if(mp[i][j]== && (left!=)) continue;
else if(mp[i][j]==){
if(mp[i][j+]==){
code[j-] = , code[j] = left;
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}else{
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
continue;
}
if(mp[i+][j]){
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
if(mp[i][j+]){
code[j-] = , code[j] = left;
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
}
else if(mp[i][j]==) continue;
else if(left== && up == ){
int cnt = ;
for(int v=j+ ; v<=m ; v++){
if(code[v]==)cnt++;
if(code[v]==)cnt--;
if(!cnt){
code[v]=;
break;
}
}
code[j-] = code[j] = ;
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
else if(left == && up == ){
int cnt=;
for(int v=j- ; v>= ; v--){
if(code[v]==)cnt++;
if(code[v]==)cnt--;
if(!cnt){
code[v]=;
break;
}
}
code[j-] = code[j] = ;
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
else if(left== && up==){
if(i==n && j==m) ans=max(ans,hashmap[cur].f[k]+w[i][j]);
}
else{
code[j-]=code[j]=;
if(j == m) shift(code , m);
hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]+w[i][j]);
}
}
} void init()
{
for(int i= ; i<=m+ ; i++) mp[][i]= , w[][i] = ;
for(int i= ; i<=n+ ; i++){
for(int j= ; j<=m ; j++)
scanf("%d" , &w[i][j]) , mp[i][j]=;
w[i][m+] = , mp[i][m+] = ;
mp[i][m+] = mp[n+][i]=;
} n++ , m++;
mp[][] = mp[n][m] = ;
/*for(int i=1 ; i<=n ; i++){
for(int j=1 ;j <=m ; j++){
cout<<mp[i][j]<<" ";
}
cout<<endl;
}*/
} ll solve()
{
ans = -1e9;
int cur = ;
hashmap[cur].init();
hashmap[cur].push_in( , );
for(int i= ; i<=n ; i++){
for(int j= ; j<=m ; j++){
hashmap[cur^].init();
dpblank(i , j , cur);
cur ^= ;
/* cout<<"test: "<<i<<" "<<j<<endl;
for(int k=0 ; k<hashmap[cur].size ; k++)
cout<<hashmap[cur].f[k]<<endl;*/ }
}
// for(int i=0 ; i<hashmap[cur].size ; i++)
return ans;
} int main()
{
// freopen("in.txt" , "r" , stdin);
int cas = ;
while(~scanf("%d%d" , &n , &m))
{
init();
printf("Case %d: ",++cas);
if(n== && m==){
printf("%d\n" , w[][]);
continue;
}
printf("%I64d\n" , solve());
}
return ;
}
HDU 3377 插头dp的更多相关文章
- hdu 1693 插头dp入门
hdu1693 Eat the Trees 题意 在\(n*m\)的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. 解法 这是一道插头dp的入门题,只需要 ...
- HDU 1693 插头dp入门详解
放题目链接 https://vjudge.net/problem/22021/origin 给出一个n*m的01矩阵,1可走0不可通过,要求走过的路可以形成一个环且可以有多个环出现,问有多少不同的 ...
- HDU 3377 Plan (插头DP,变形)
题意:有一个n*m的矩阵,每个格子中有一个值(可能负值),要从左上角走到右下角,求路径的最大花费. 思路: 除了起点和终点外,其他的点可以走,也可以不走. (2)我用的是括号表示法,所以起始状态为') ...
- HDU 4113 Construct the Great Wall(插头dp)
好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- HDU 4949 Light(插头dp、位运算)
比赛的时候没看题,赛后看题觉得比赛看到应该可以敲的,敲了之后发现还真就会卡题.. 因为写完之后,无限TLE... 直到后来用位运算代替了我插头dp常用的decode.encode.shift三个函数以 ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
随机推荐
- html5移动端制作知识点总结
一.测试工具:1.Chrome 2.Opera Mobile二.分辨率:一般现代手机最小320px,最大640px.三.全屏流体设计: 1.腾讯新闻:http://xw.qq.com/ 2.途牛旅游: ...
- linux 命令 第一波
man 命令名字 查看命令详细解释 q退出cd 切换目录cd .. 回到上级目录su 切换用户pwd 当前目录mkdir cmy 创建cmy文件夹[目录]rm cmy 删除cmy文件夹[如果cmy里面 ...
- Android 热补丁动态修复框架小结
一.概述 最新github上开源了很多热补丁动态修复框架,大致有: https://github.com/dodola/HotFix https://github.com/jasonross/Nuwa ...
- Oracle一个用户查询另一个用户的表数据
1.两个用户是在不同的库,需要建立dblink 2.属于同一个库的不同用户 1)方法一:使用"用户名."的方式访问 例如:要从USER1账号访问USER2中的表TABLE2 A. ...
- spring+redis实现缓存
spring + redis 实现数据的缓存 1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个j ...
- [转载]最牛B的编码套路
原文地址:http://www.codeceo.com/article/nb-coding-style.html 这篇文章很不错,推荐给大家看. 最近,我大量阅读了Steve Yegge的文章.其中有 ...
- shelve模块
#coding:utf-8 __author__ = 'similarface' #email:similarface@outlook.com ''' shelve模块: 映射容器 存储对象,被存储的 ...
- Selenium处理时间控件
Web网页的时间控件往往嵌入到一个iframe里,抓取页面元素时,总是失败? 不要慌,WebDriver已经有相应的处理方法了. driver.switchTo().frame(driver.find ...
- centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误
centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误 之前把selinux关闭了,这次想打开selinux,于是修改了 /e ...
- MariaDB 加密特性及使用方法
版权声明:本文由吴洪辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/193 来源:腾云阁 https://www.qclo ...