您的位置:首页 > 其它

uva 11471 Arrange the Tiles (DP)

2014-01-14 14:42 691 查看
[align=center]Problem A [/align]
[align=center]Arrange the Tiles[/align]
[align=center]Time Limit : 4 seconds[/align]
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 YR G R YY 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 InputSample 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

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
#define ll long long

const int maxn = 1<<12;
struct Dp{
int vis;
ll sum;
Dp(int V = 0, int S = 0){
vis = V, sum = S;
}
}dp[5][5][5][5][maxn];
char tile[13][5];
int v;
map<char , int> mp;

void initial(){
v = 1;
mp['R'] = 1;
mp['G'] = 2;
mp['B'] = 3;
mp['Y'] = 4;
}

void readcase(){
for(int i = 0;i < 12;i++){
scanf("%s", tile[i]);
//cin >> tile[i] ;
}
}

ll DP(int k , int c[4] , int sta , queue<int> q){
if(k >= 13) return 1;
if(dp[c[0]][c[1]][c[2]][c[3]][sta].vis == v) return dp[c[0]][c[1]][c[2]][c[3]][sta].sum;
ll ans = 0;
int qsize = q.size();
while(qsize--){
int tem = q.front();
q.pop();
int tc[4] = {c[0] , c[1] , c[2] , c[3]};
if(k%3 == 1){
if(c[0] == mp[tile[tem][0]] || c[0] == 0){
tc[0] = mp[tile[tem][2]];
tc[3] = mp[tile[tem][1]];
ans += DP(k+1 , tc , sta+(1<<tem) , q);
}
}
if(k%3 == 2){
if((c[1] == mp[tile[tem][0]] || c[1] == 0) && c[3] == mp[tile[tem][3]]){
tc[1] = mp[tile[tem][2]];
tc[3] = mp[tile[tem][1]];
ans += DP(k+1 , tc , sta+(1<<tem) , q);
}
}
if(k%3 == 0){
if((c[2] == mp[tile[tem][0]] || c[2] == 0) && c[3] == mp[tile[tem][3]]){
tc[2] = mp[tile[tem][2]];
tc[3] = mp[tile[tem][1]];
ans += DP(k+1 , tc , sta+(1<<tem) , q);
}
}
q.push(tem);
}
dp[c[0]][c[1]][c[2]][c[3]][sta].vis++;
return dp[c[0]][c[1]][c[2]][c[3]][sta].sum = ans;
}

int main(){
//freopen("in" , "r" , stdin);
initial();
queue<int> q;
for(int j = 0;j < 12;j++){
q.push(j);
}
int t;
scanf("%d" , &t);
for(int i = 1;i <=t ;i++){
readcase();
int color[4] = {0};
printf("Case %d: %lld\n" , i , DP(1 ,color,0,q));
v++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: