Sdoi & State Compression

Source: Internet
Author: User

is not usually in the cell phone play to eat beans game to play tired of it? Recently Mokia mobile phone launched a new bean game, everyone together to try it.

The rules of the game are very simple, in a NxM matrix grid distribution of D beans, each bean has a different score Vi. The player can choose any one of the squares as the starting grid, each move can walk to the adjacent four squares, until finally back to the beginning of the grid. The final player's score is the sum of all the beans that are surrounded by the path minus the number of steps the player moves. there are obstacles in some lattices in the matrix, and at any moment the player cannot enter a lattice containing obstructions or beans. The player may have a minimum score of 0, that is, do nothing.

Notice the concept of path bracketing, which is the interior of a polygon formed by a bean in a path (possibly a complex polygon with self-intersection). Here are two examples:

In the first example, the beans are surrounded by a rectangle inside the path, so the beans are enclosed. In the second example, although the path passes through 8 squares around the bean, the path-forming polygon does not contain beans, so it does not surround the beans.
Bubu has recently been fascinated by the game, but how to play can not get high scores. Smart you decide to write a program to help him smooth through the customs.


This question d=9, even if you do not know DP How to write also know this is hit duck, 9, how good number, 2^9 is not big.

Hit the duck hit less, I hit the code of the tune I want to vomit blood. Stick to the code of the Weng God, both intuitive and beautiful, even if you never know what is a duck you know what makes a duck ....


#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue > #define MAXN 12#define maxs 512const int dx[] = {0,-1, 0, 1};const int dy[] = {-1, 0, 1, 0};using namespace Std;int n , M, D, VAL[MAXN], PX[MAXN], Py[maxn], f[maxn][maxn][maxs];int ans, NX, NY, NS, NW, Up;bool V[maxn][maxn][maxs];char map[m    axn][maxn];struct node{int x, y, S; Node () {} node (int _x, int _y, int _s) {x = _x; y = _y; s = _s;}}    now;queue<node> q;void Add (int x, int y, int s, int w) {v[x][y][s] = 1; f[x][y][s] = W; Q.push (node (x, Y, s));}    void Solve (int sx, int sy) {memset (f, -0x3f, sizeof (f));    memset (V, 0, sizeof (v));    while (! Q.empty ()) Q.pop ();    Add (SX, sy, 0, 0);        while (! Q.empty ()) {now = Q.front (); Q.pop ();            for (int i = 0; i < 4; + + i) {NX = now.x + dx[i]; ny = now.y + dy[i];  if (NX >= 1 && NX <= n && NY >= 1 && NY <= m && map[nx][ny] = = ' 0 ') {              NS = NOW.S;                NW = F[now.x][now.y][now.s]-1;                    if (ABS (dx[i]) = = 1) {up = min (now.x, NX);                            for (int j = 1; J <= D; + + j) if (px[j] = = up && py[j] > NY) {                            NS ^= (1 << (j-1)); if (NS & (1 << (j-1))) NW + = Val[j];                        else NW-= Val[j];            }} if (! V[nx][ny][ns]) Add (NX, NY, NS, NW); }}} for (int i = 0; i < (1 << D); + + i) ans = max (ans, f[sx][sy][i]);}    int main () {scanf ("%d%d", &n, &m); scanf ("%d", &d);    for (int i = 1; I <= D; + + i) scanf ("%d", &val[i]);        for (int i = 1; I <= n; + + i) {scanf ("%s", Map[i] + 1); for (int j = 1; j <= m; + + j) if (Map[i][j] >= ' 1 ' && map[i][j] <= ' 9 ') {Px[map I [j]-' 0 '] = i;            PY[MAP[I][J]-' 0 '] = j; }    } for (int sx = 1; SX <= N; + sx) for (int sy = 1; sy <= N; + + sy) if (map[sx][sy] = = ' 0 ')    Solve (SX, SY);    printf ("%d\n", ans); return 0;}

Sdoi & State Compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.