uva 11471 Arrange the Tiles (DP)

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 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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值