[UVA][math&bitmask] 11471 - Arrange the Tiles@Morris' Blog|PChome Online 人新台
2013-08-28 07:02:07| 人1,174| 回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,174) | 回(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 or 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
全文
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