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. (1≤N≤5,1≤M≤5) .
2 4 .X.. ...X 4 2 X. .. .. X. 1 2 XX
Case #1: 2100 Case #2: 2520 Case #3: 0
题目大意:
一张n*m的地图。
每个点代表一个山/谷,高度为1 ~ n*m,要求两两高度不同。
图上’.’表示山峰,’X’表示沟谷
沟谷必须比四周八个位置都低。
山峰至少比四周八个位置中的一个高。
问给每个点标一个高度,满足上面要求的所有方案数。
bzoj原题也是够了= =不能说赛时他们贴代码,只怪自己刷题不够全
感觉这题做法非常妙。昨晚理解到刚刚才突然恍然大悟。
首先先考虑让’X’满足要求。
因为’X’最多9个
所以可以把沟谷的选择状态二进制压缩一下。
不负责任抛转移方程:
dp[i][j]=dp[i−1][j]C1cntj−i+1+∑k∈jdp[i−1][j−k]
下面来解释一下。
dp[i][j]
表示沟谷的标记状态为j(1为该位置已设置高度 0表示未设置高度)1 ~ i高度已用的合法方案数。
cntj
表示j状态下已标记的沟谷和所有可标记(周围沟谷已标记)的山峰的数量。在dp前可以预处理出来。
因为1 ~ i-1高度标给了j状态下的沟谷和可标记的一些山峰,这样 cntj−i+1 其实就是j状态下可标记且未标记的山峰,他们都可以标为i高度。
上面统计了山峰的标法。那么j状态下的沟谷标i的方案数是多少?
∑k∈jdp[i−1][j−k]
表示的是选择j状态下任意一个沟谷,标为i的方案数。
这里保证方案都合法的证明我想了好一阵。
因为dp数组要求记录的都是合法方案数,那么假设对于一个沟谷
k∈j
(这里k为沟谷的二进制),
dp[i−1][j−k]
表示的是沟谷为j-k状态时,1 ~ i-1高度已用的合法方案数。那么既然k沟谷此状态下未标记,那么与它相关联的山峰肯定也未标记!这在统计cnt数组的时候保证了。那么就可以放心给它标高度i了。
这样通过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;
}