【HDOJ 5838】Mountain(局部极小值)
来源:互联网 发布:全球云计算排名2017 编辑:程序博客网 时间:2024/06/04 18:15
Mountain
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)A cell is called valley only if its height is less than the heights of all its adjacent cells.If two cells share a side or a corner they are called adjacent.And one cell will have eight adjacent cells at most.
Now give you N strings,and each string will contain M characters.Each character will be ‘.’ or uppercase ‘X’.The j-th character of the i-th string is ‘X’ if the j-th cell of the i-th row in the mountain map is a valley, and ‘.’ otherwise.Zhu wants you to calculate the number of distinct mountain maps that match these strings.
To make this problem easier,Zhu defines that the heights are integers between 1 and N*M.Please output the result modulo 772002.
Each test case begins with a line containing two non-negative integers N and M. Then N lines follow, each contains a string which contains M characters.
2 4.X.....X4 2X.....X.1 2XX
Case #1: 2100Case #2: 2520Case #3: 0
题目大意:
一张n*m的地图。
每个点代表一个山/谷,高度为1 ~ n*m,要求两两高度不同。
图上’.’表示山峰,’X’表示沟谷
沟谷必须比四周八个位置都低。
山峰至少比四周八个位置中的一个高。
问给每个点标一个高度,满足上面要求的所有方案数。
bzoj原题也是够了= =不能说赛时他们贴代码,只怪自己刷题不够全
感觉这题做法非常妙。昨晚理解到刚刚才突然恍然大悟。
首先先考虑让’X’满足要求。
因为’X’最多9个
所以可以把沟谷的选择状态二进制压缩一下。
不负责任抛转移方程:
下面来解释一下。
因为1 ~ i-1高度标给了j状态下的沟谷和可标记的一些山峰,这样
上面统计了山峰的标法。那么j状态下的沟谷标i的方案数是多少?
这里保证方案都合法的证明我想了好一阵。
因为dp数组要求记录的都是合法方案数,那么假设对于一个沟谷
这样通过dp就可以统计出X位置为沟谷的合法方案数。
但这里的合法只保证了沟谷合法,但可能一些山峰会变成沟谷,搜一下这些情况,然后容斥排除一下,最终答案就是合法的方案数。
代码如下:
#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const int mod = 772002;const double eps = 1e-8;int dir[2][9] = {{ 0, 0, 1,-1, 1,-1, 1,-1, 0}, { 1,-1, 0, 0, 1,-1,-1, 1, 0}};char mp[10][10];bool vis[10][10];Pr pr[9];int cnt[1<<9],dp[66][1<<9];int tp,n,m;//判断是否越界bool in(int x,int y){ return 0 <= x && x < n && 0 <= y && y < m;}int cal(){ tp = 0; //找出沟谷 for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(mp[i][j] == 'X') { pr[tp].first = i; pr[tp++].second = j; } //沟谷状压 int tot = 1<<tp; int k; memset(cnt,0,sizeof(cnt)); //统计每个状态下已标记沟谷+可标记山峰数量 for(int s = 0; s < tot; ++s) { memset(vis,0,sizeof(vis)); for(int i = 0; i < tp; ++i) if(!(s&(1<<i))) vis[pr[i].first][pr[i].second] = 1; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) { for(k = 0; k < 9; ++k) if(in(i+dir[0][k],j+dir[1][k]) && vis[i+dir[0][k]][j+dir[1][k]]) break; if(k == 9) cnt[s]++; } } dp[0][0] = 1; for(int i = 1; i <= n*m; ++i) { for(int s = 0; s < tot; ++s) { dp[i][s] = (dp[i-1][s]*max(cnt[s]-i+1,0))%mod; for(int j = 0; j < tp; ++j) { if(s&(1<<j)) { dp[i][s] = (dp[i][s]+dp[i-1][s-(1<<j)])%mod; } } } } return dp[n*m][tot-1];}int ans;//判断初始地图是否合法(沟谷不相邻)bool can(){ for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) { if(mp[i][j] == '.') continue; for(int k = 0; k < 8; ++k) { if(!in(i+dir[0][k],j+dir[1][k])) continue; if(mp[i+dir[0][k]][j+dir[1][k]] == 'X') return false; } } return true;}//容斥void dfs(int x,int y,int cnt){ if(x == n) { ans = ((ans+cal()*(cnt&1? -1: 1))%mod+mod)%mod; return; } if(y == m) { dfs(x+1,0,cnt); return; } dfs(x,y+1,cnt); //如果是山峰,并且可能被当做沟谷,需排除。 if(mp[x][y] == '.') { for(int i = 0; i < 8; ++i) if(in(x+dir[0][i],y+dir[1][i]) && mp[x+dir[0][i]][y+dir[1][i]] == 'X') return; mp[x][y] = 'X'; dfs(x,y+1,cnt+1); mp[x][y] = '.'; }}int main(){ //fread(""); //fwrite(""); int z = 1; while(~scanf("%d%d",&n,&m)) { printf("Case #%d: ",z++); for(int i = 0; i < n; ++i) scanf("%s",mp[i]); if(!can()) { puts("0"); continue; } ans = 0; dfs(0,0,0); printf("%d\n",ans); } return 0;}
- 【HDOJ 5838】Mountain(局部极小值)
- HDU 5838 Mountain && BZOJ 2669 局部极小值
- 【CQOI2012】局部极小值
- bzoj 2669 局部极小值
- bzoj2669: [cqoi2012]局部极小值
- BZOJ2669: [cqoi2012]局部极小值
- 局部极小
- 【bzoj2669】【cqoi2012】【局部极小值】【状压dp】
- bzoj 2669: [cqoi2012]局部极小值
- BZOJ 2669 局部极小值 CQOI2012
- BZOJ 2669- [cqoi2012]局部极小值
- BZOJ 2669: [cqoi2012]局部极小值
- bzoj 2669: [cqoi2012]局部极小值
- 什么是神经网络或者微粒群算法所说的陷入 局部极小值?局部极小值是什么?
- bzoj 2669: [cqoi2012]局部极小值(dp+容斥原理)
- 【bzoj 2669】[cqoi2012]局部极小值(状压dp+容斥原理)
- [BZOJ2669][cqoi2012]局部极小值(状压DP+容斥原理)
- [DP 容斥] BZOJ 2669 [cqoi2012]局部极小值
- Android样式的开发:drawable汇总篇
- Android webview 页面自动缩放为设备宽度
- 1181 质数中的质数
- Bone Collector(0-1背包模板)
- C++学习笔记--泛型算法
- 【HDOJ 5838】Mountain(局部极小值)
- mysql5.7 慢查询配置 查看sql语句执行时间
- 堆排序
- Xcode因为证书问题经常报的那些错
- Node之Event模块
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
- 自己总结 ContentProvider数据共享
- mysql 定时备份数据
- Git学习路线图