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. 4 edges colored. A tile is described by its 4 edge colors starting from top and going clockwise.
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 |
#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; }
相关文章推荐
- Paths through the Hourglass - UVa 10564 dp
- UVA11324-- The Largest Clique(SCC+DP)
- UVA 437-The Tower of Babylon(DP)
- UVA 437 The Tower of Babylon(LIS型DP~水)
- UVAlive 6560 - The Urge to Merge(状压dp)
- 【解题报告】uva357_Let Me Count The Ways(让我来数数方案, dp, 完全背包)
- uva 437 The Tower of Babylon(DAG的DP)
- UVA 11324 The Largest Clique(SCC+DP)
- UVa 10564 - Paths through the Hourglass(DP)
- UVA - 1401 Remember the Word(trie+dp)
- POJ 2441 Arrange the Bulls(状态压缩DP入门)
- uva 10285 The Tower of Babylon(dp,记忆化搜索)
- POJ 2441 Arrange the Bulls 状态压缩DP
- UVA - 10917 Walk Through the Forest (最短路+DP)
- UVA 1025 A Spy in the Metro城市里的间谍(dp)
- poj 2441 Arrange the Bulls (状态压缩dp+滚动数组)
- UVA 437 The Tower of Babylon (dp + DAG最长序列)
- Uva-1375 The Best Name for Your Baby(复杂DP)
- uva 437 - The Tower of Babylon(DP)
- UVa 1401 - Remember the Word(Trie + DP)