uva 11471 Arrange the Tiles (DP)
来源:互联网 发布:迪优美特网络机顶盒 编辑:程序博客网 时间:2024/06/05 00:23
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 YThe 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 Output2
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 2: 479001600
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <map>#include <queue>using namespace std;#define ll long longconst 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;}
- uva 11471 Arrange the Tiles (DP)
- uva 11481 Arrange the Numbers
- UVa:11481 Arrange the Numbers
- UVA - 11481 Arrange the Numbers
- UVA 11125 - Arrange Some Marbles (dp)
- uva 11481 - Arrange the Numbers(计数问题)
- UVA 11481 Arrange the Numbers (容斥)
- POJ2441 Arrange the Bulls 状态压缩DP
- poj 2441 Arrange the Bulls(状压DP)
- poj2441 Arrange the Bulls 状压DP
- Arrange the Bulls(poj2441)状态压缩dp
- poj 2441 Arrange the Bulls (状压dp)
- UVA 11481 - Arrange the Numbers(组合数学)
- UVa 11481 Arrange the Number(容斥原理)
- UVa 11481 Arrange the Numbers (组合数学+容斥原理)
- poj 2441Arrange the Bulls解题报告-状态压缩dp
- POJ--2441--Arrange the Bulls--状压DP-滚动数组优化
- POJ 2441 Arrange the Bulls / 状态压缩DP
- js实现页面右键自定义功能
- [黑马程序员]第二篇:常用对象的使用
- Filezilla server设置指南及中文乱码、登录欢迎语问题解决
- Java Socket 编程学习(1)
- "黑马程序员"Android 开发中的XmlPullParser解析器
- uva 11471 Arrange the Tiles (DP)
- 因为我认为没有
- Qualcomm笔记
- 文件上传简介
- Linux安全访问控制模型
- TexturePacker整合图片方法
- 在子线程上调主线程
- ASP.NET 处理回发密码框清空的问题
- Android GPS定位测试(附效果图)