cf 85 E. Petya and Spiders

http://codeforces.com/contest/112/problem/E

輪廓線dp。每個格子中的蜘蛛選一個去向,最終,使每個蜘蛛都有一個去向,同時保證有蜘蛛的格子最少。需要用4進制模擬

此題還可以用DLX+二分來解,這個解法相對於輪廓線dp就很無腦了,不用考慮細節。以後再補上

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <set>
using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
int n, m;
const int MALL = 1 << 14;

int dp[2][MALL];
///00 up, left; 01 stall; 10 right; 11 down;
int now, next;
int ALL;
int ans;
void update(int r, int nextr, int val)
{
    nextr &= ALL;
    dp[next][nextr] = max(dp[next][nextr], dp[now][r] + val);
}
int getI(int x, int i)
{
    return (x >> (2 * i))&3;
}
int main()
{
    while (cin >> n >> m)
    {
        if (n < m) swap(n, m);
        ALL = (1 << (m * 2)) - 1;

        memset(dp, -1, sizeof(dp));
        dp[0][0] = 0;
        now = 0; next = 1;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                for (int r = 0; r <= ALL; r++)
                {
                    if (dp[now][r] != -1)
                    if ((j && getI(r, 0) == 2) || (i && getI(r, m - 1) == 3))
                    {
                        update(r, (r << 2) + 1, 0);
                        continue;
                    }
                    update(r, (r << 2) + 1, 0);
                    if (j && getI(r, 0) == 1 ) update(r, r << 2, 1);///left 00

                    if (i && getI(r, m - 1) == 1 ) update(r, r << 2, 1);///up 00

                    if (j < m - 1) update(r, (r << 2) + 2, 1);///right 10

                    if (i < n - 1) update(r, (r << 2) + 3, 1);///down 11
                }
                memset(dp[now], -1, sizeof(dp[now]));
                next = now; now ^= 1;
            }
        }
        ans = 0;
        for (int i = 0; i <= ALL; i++)
            ans = max(ans, dp[now][i]);
        cout << ans << endl;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章