LightOJ - 1333 - Grid Coloring

链接:

https://vjudge.net/problem/LightOJ-1333

题意:

You have to color an M x N two dimensional grid. You will be provided K different colors for this. You will also be provided a list of B blocked cells of this grid. You cannot color these blocked cells.

A cell can be described as (x, y), which points to the yth cell from the left of the xth row from the top.

While coloring the grid, you have to follow these rules -

  1.  You have to color each cell which is not blocked.
  2.  You cannot color a blocked cell.
  3.  You can choose exactly one color from K given colors to color a cell.
  4.  No two vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) shouldn't contain the same color.

You have to calculate the number of ways you can color this grid obeying all the rules provided.

思路:

把不能填的位置记录排序,然后枚举每个不能填的位置,同时维护上一个能填的位置,计算之间的种类,乘起来

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9;
const int MAXN = 1e6+10;

struct Node
{
    int x, y;
    bool operator < (const Node& rhs) const
    {
        if (this->y != rhs.y)
            return this->y < rhs.y;
        return this->x < rhs.x;
    }
}node[510];
int n, m, k, b;

LL PowMod(LL a, LL b, LL p)
{
    LL res = 1;
    while(b)
    {
        if (b&1)
            res = res*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return res;
}

LL GetVal(LL len)
{
    if (len <= 0)
        return 1;
    if (len == 1)
        return k;
    return k*PowMod(k-1, len-1, MOD)%MOD;
}

int main()
{
    // freopen("test.in", "r", stdin);
    int t, cas = 0;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &n, &m, &k, &b);
        printf("Case %d:", ++cas);
        for (int i = 1;i <= b;i++)
            scanf("%d%d", &node[i].x, &node[i].y);
        sort(node+1, node+1+b);
        int x = 1, y = 1;
        LL ans = 1;
        if (b == 0)
        {
            ans = k*PowMod(k-1, n-1, MOD)%MOD;
            ans = PowMod(ans, m, MOD);
        }
        for (int i = 1;i <= b;i++)
        {
            if (node[i].y == y)
            {
                LL tmp = GetVal(node[i].x-x);
                ans = ans*tmp%MOD;
                x = node[i].x+1;
                if (x > n)
                    x = 1, y++;
            }
            else
            {
                LL tmp = GetVal(n-x+1);
                ans = ans*tmp%MOD;
                tmp = GetVal(n);
                ans = ans*PowMod(tmp, node[i].y-y-1, MOD)%MOD;
                x = 1, y = node[i].y;
                i--;
            }
            // cout << ans << ' ' << x << ' ' << y << endl;
        }
        if (y <= m && b != 0)
        {
            LL tmp = GetVal(n-x+1);
            ans = ans*tmp%MOD;
            tmp = GetVal(n);
            ans = ans*PowMod(tmp, m-y, MOD)%MOD;
        }
        printf(" %lld\n", ans);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/12019529.html