24h購物| | PChome| 登入
2013-04-22 19:57:43| 人氣617| 回應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
人氣(617) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][基於概率] 10707 - 2D-Nim
此分類上一篇:[UVA] 10651 - Pebble Solitaire

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文