【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. (1≤N≤5,1≤M≤5).

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

2016中国大学生程序设计竞赛 - 网络选拔赛

题目大意:

一张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;
}
时间: 2024-08-06 03:40:05

【HDOJ 5838】Mountain(局部极小值)的相关文章

【HDOJ 5652】 India and China Origins(并查集)

[HDOJ 5652] India and China Origins(并查集) India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 805    Accepted Submission(s): 272 Problem Description A long time ago there are

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

HDOJ 3790 双权值Dijkstra

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 1000000; 8 const int MAXSIZE = 1005; 9 10 int map[MAXSIZE][MAXSIZE]; 11 int price[MAXSIZE][MAXSIZE]; 1

HDOJ 1217 Floyed Template

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 #include<map> 6 using namespace std; 7 8 map<string,int>name; 9 const int INF = 1000000; 10 const int MAXSIZE = 1005; 11 const int