24h購物| | PChome| 登入
2013-08-28 07:02:07| 人氣1,125| 回應2 | 上一篇 | 下一篇

[UVA][math&bitmask] 11471 - Arrange the Tiles

推薦 0 收藏 0 轉貼0 訂閱站台

 
Problem A
Arrange the Tiles
Time Limit : 4 seconds

 

 

There is a board of dimension 4 x 3. Each cell of the board is a container that can hold a tile. The board is shown in the following diagram.

   

As expected, you have got 12 tiles with you and you are required to place them in the containers so that every container has one tile in it. However, the tiles that you have are not ordinary tiles. Each tile is a square piece with all its 4 edges colored. A tile is described by its 4 edge colors starting from top and going clockwise.

R G B Y R G R Y Y Y Y Y

The image shows 3 tiles. The description of the tiles are given below the image. The colors that will be used to denote the tiles will be from the set {yellow, green, blue and red} and will be represented by {Y, G, B and R} for the sake of brevity.

There are 12 factorials (12!) ways of placing the tiles on the board. A placement is considered fragile if the touching sides of any two adjacent tiles is made up of different colors. You are required to find out the total number of placements which are not fragile.

Note: You can not rotate the tiles. That is, the initial orientation must be preserved.

 
  Input    
  The first line of input is an integer T( T < 20 ) that denotes the number of test cases. Each case starts on a new line and consists of one or more lines containing 12 strings. Each string represents a tile and is made up of 4 characters.  
     
  Output  
  For each case, output the case number followed by the total number of non-fragile placements.  
     
  Sample Input Sample Output    
 

2
BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB YYYY
GGGG GGGG GGGG GGGG GGGG GGGG GGGG GGGG GGGG GGGG GGGG GGGG

Case 1: 0
Case 2: 479001600

   
 
Problem Setter: Sohel Hafiz
Special Thanks: Shamim Hafiz, Md. Arifuzzaman Arif
Next Generation Contest 5
     
題目描述:

排列 12 塊的方塊成 4x3 如圖那樣,每塊最多使用一次,因此是 12! 種,而每塊四邊有不同的顏色,

放上去時,相鄰的邊要共同色,問總共有多少種排法。

題目解法:

拆成一半 2x3,考慮將這一半的所有可能找出來,並且記錄這個 2x3 最上邊的 3 個顏色和下邊的 3 個顏色,因此會有 C(12, 6) = 924 種,顏色則會有 4^3 = 64。

考慮將使用過的排列,以 bitmask 記錄,以方便我們進行兩個 2x3 合併。

合併的時候很簡單,下邊顏色要等於上邊顏色,而且使用的塊會互補,方法就是相乘起來。


#include <stdio.h>
#include <string.h>
char tile[12][10];
int dpup[64][4096], dpdown[64][4096];
// up side&down side, 4^3 = 64, each state 2^12 = 4096
int chways[6], used[12] = {};//chose 6 item
int panel[2][3];
int trans(char c) {
    switch(c) {
        case 'R':return 0;
        case 'G':return 1;
        case 'B':return 2;
        case 'Y':return 3;
    }
    puts("err");
    return -1;
}
void dfs(int idx) {
    int i;
    int x, y;
    if(idx == 6) {
        int up = 0, down = 0, state = 0;
        int d;
        for(i = 0; i < 6; i++)
            state |= 1<<chways[i];
        for(i = 0; i < 3; i++)
            up = up*4 + trans(tile[panel[0][i]][0]);
        for(i = 0; i < 3; i++) {
            down = down*4 + trans(tile[panel[1][i]][2]);
        }
        dpup[up][state]++;
        dpdown[down][state]++;
        return;
    }
    for(i = 0; i < 12; i++) {
        if(used[i] == 0) {
            x = idx/3, y = idx%3;
            if(x-1 >= 0) {// up
                if(tile[panel[x-1][y]][2] != tile[i][0]) {
                    continue;
                }
            }
            if(y-1 >= 0) {
                if(tile[panel[x][y-1]][1] != tile[i][3]) {
                    continue;
                }
            }
            chways[idx] = i;
            panel[x][y] = i;
            used[i] = 1;
            dfs(idx+1);
            used[i] = 0;
        }
    }
}
int main() {
    int testcase, cases = 0;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        for(i = 0; i < 12; i++)
            scanf("%s", tile[i]);
        memset(dpup, 0, sizeof(dpup));
        memset(dpdown, 0, sizeof(dpdown));
        dfs(0);
        long long ret = 0;
        for(i = 0; i < 64; i++) {
            for(j = 0; j < 4096; j++) {
                ret += dpdown[i][j]*dpup[i][4095-j];
            }
        }
        printf("Case %d: %lld\n", ++cases, ret);
    }
    return 0;
}

台長: Morris
人氣(1,125) | 回應(2)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][sssp] 658 - It's not a Bug, it's a Feature!
此分類上一篇:[UVA] 11065 - A Gentlemen's Agreement

https://thegioigachlatsan.com
This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info
2020-02-17 16:12:11
https://thegioigachlatsan.com
This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info
2020-02-17 16:15:09
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文