【HDOJ 5838】Mountain(局部极小值)

【HDOJ 5838】Mountain(局部极小值)

Mountain

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
Zhu found a map which is a N * M rectangular grid.Each cell has a height and there are no two cells which have the same height. But this map is too old to get the clear information,so Zhu only knows cells which are valleys.

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.
 

Input
The input consists of multiple test cases.

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. (1N5,1M5) .
 

Output
For each test case, output a single line “Case #x: y”, where x is the case number, starting from 1. And y is the answer after module 772002.
 

Sample Input
  
  
2 4 .X.. ...X 4 2 X. .. .. X. 1 2 XX
 

Sample Output
  
  
Case #1: 2100 Case #2: 2520 Case #3: 0
 

Author
UESTC
 

Source


题目大意:
一张n*m的地图。
每个点代表一个山/谷,高度为1 ~ n*m,要求两两高度不同。

图上’.’表示山峰,’X’表示沟谷
沟谷必须比四周八个位置都低。
山峰至少比四周八个位置中的一个高。
问给每个点标一个高度,满足上面要求的所有方案数。

bzoj原题也是够了= =不能说赛时他们贴代码,只怪自己刷题不够全
感觉这题做法非常妙。昨晚理解到刚刚才突然恍然大悟。

首先先考虑让’X’满足要求。
因为’X’最多9个
所以可以把沟谷的选择状态二进制压缩一下。
不负责任抛转移方程:
dp[i][j]=dp[i1][j]C1cntji+1+kjdp[i1][jk]

下面来解释一下。
dp[i][j] 表示沟谷的标记状态为j(1为该位置已设置高度 0表示未设置高度)1 ~ i高度已用的合法方案数。
cntj 表示j状态下已标记的沟谷和所有可标记(周围沟谷已标记)的山峰的数量。在dp前可以预处理出来。

因为1 ~ i-1高度标给了j状态下的沟谷和可标记的一些山峰,这样 cntji+1 其实就是j状态下可标记且未标记的山峰,他们都可以标为i高度。

上面统计了山峰的标法。那么j状态下的沟谷标i的方案数是多少?
kjdp[i1][jk] 表示的是选择j状态下任意一个沟谷,标为i的方案数。
这里保证方案都合法的证明我想了好一阵。
因为dp数组要求记录的都是合法方案数,那么假设对于一个沟谷 kj (这里k为沟谷的二进制), dp[i1][jk] 表示的是沟谷为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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值