[UVA][bitmask] 11391 - Blobs in the Board@Morris' Blog|PChome Online 人新台
2013-04-22 19:57:43| 人664| 回0 | 上一篇 | 下一篇

[UVA][bitmask] 11391 - Blobs in the Board

0 收藏 0 0 站台

I I U  O N L I N E   C O N T E S T   2 0 0 8

Problem G: Blobs in the Board

Input: standard input

Output: standard output

 

You are given a board consists of R rows and C columns. There are N blobs in the board. Each cell of the board might or might not contain a blob. A cell can have at most 8 adjacent cells. A blob can jump over any adjacent blobs to the next empty cell. Thus a blob can jump straight to two squares at any of the 8 directions if the destination cell is empty and the cell between the source cell and the destination cell contains a blob. Blobs cannot jump outside of the board. After the jump the blob in the middle cell of the jump will be removed. The goal is to make jumps carefully so that after N-1 jumps only one blob remains. Your task is to count how many ways the goal can be achieved.

 

Input

Input consists of several test cases. The first line of the input file contains a single integer T indicating the number of test cases. Then T test cases follow. Each test case starts with three positive integers R, C and N. Each of the next N lines contain two integers x, y where i-th line contains the row position x and column position y of the i-th blob. No two blobs share the same cell.

 

Output

For each set of input produce one line of output “Case x: n”, where x indicates the test case number (starting from 1) and n the number of ways the goal can be achieved.

 

Constraints

-           R, C ≤ 4

-           1 ≤ x ≤ R

-           1 ≤ y ≤ C

-           1 ≤ i ≤ N

 

Sample Input

Output for Sample Input

3

1 2 1

1 1

3 3 8

2 3

2 1

1 2

3 3

3 2

3 1

1 1

1 3

3 3 3

3 1

2 2

1 2

Case 1: 1

Case 2: 0

Case 3: 2

 


一 R*C 的方格板子,然後有 N 在不同的位置,
然後任是要算出 N 成一的方法。
少的步是一可以藉由近八格中的一格跳到面那格,
此被跨得那格必存在,跳到一有的格子。
然後被跨的那就消失。

因此共跳 N-1 步。
做一下,然後初始化所有只有一的方法是 1。


#include <stdio.h>
#include <string.h>
int dp[4][4][1<<16];
int dx[8] = {0,0,1,1,1,-1,-1,-1};
int dy[8] = {1,-1,0,1,-1,0,1,-1};
int dfs(int mask, int r, int c) {
    if(dp[r][c][mask] != -1)
        return dp[r][c][mask];
    int i, j, k, o1, o2, o3, tx, ty, x, y, mask2;
    int &res = dp[r][c][mask];
    res = 0;
    for(i = 0; i <= r; i++) {
        for(j = 0; j <= c; j++) {
            o3 = i*4+j;
            if((mask>>o3)&1) {
                for(k = 0; k < 8; k++) {
                    tx = i+dx[k], ty = j+dy[k];
                    x = i+2*dx[k], y = j+2*dy[k];
                    if(tx < 0 || tx > r || ty < 0 || ty > c)
                        continue;
                    if(x < 0 || x > r || y < 0 || y > c)
                        continue;
                    o1 = tx*4+ty, o2 = x*4+y;
                    if(!((mask>>o1)&1) || ((mask>>o2)&1))
                        continue;
                    mask2 = mask ^ (1<<o1) ^ (1<<o2) ^ (1<<o3);
                    res += dfs(mask2, r, c);
                }
            }
        }
    }
    return res;
}
int main() {
    memset(dp, -1, sizeof(dp));
    int i, j, k;
    for(i = 0; i < 4; i++) {
        for(j = 0; j < 4; j++) {
            for(k = 0; k < 16; k++)
                dp[i][j][1<<k] = 1;
        }
    }
    int testcase, R, C, N, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &R, &C, &N);
        int x, y, mask = 0;
        while(N--) {
            scanf("%d %d", &x, &y);
            x--, y--;
            mask |= 1<<(x*4+y);
        }
        R--, C--;
        printf("Case %d: %d\n", ++cases, dfs(mask, R, C));
    }
    return 0;
}
/*
3
3 3 3
3 1
2 2
1 2
*/

台: Morris
人(664) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][基於概率] 10707 - 2D-Nim
此分上一篇:[UVA] 10651 - Pebble Solitaire

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86