Submission #1331144


Source Code Expand

#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
#include <ctime>
#pragma endregion //#include
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)
/////////
#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

//////////////////
vector< vector< vector<int> > > block;
vector< bool > used;
vector< vector<int> > men;

//num面決定している。
LL dfs( vector<int> v,int num ){
	if( num >= 6 ){//全て配置されている。
		return 1;
	}
	vector<int> orgV = v;//保存しておく
	vector<int> ter(5,-1);//探す面
	for(int i=0;i<4;++i){
		ter[i] = v[ men[num][i] ];
	}
	vector< vector<int> >::iterator itr,end;
	LL ans,res;
	ans = 0;
	//set<vector<int> > bSet;//回転対象分も含める
	for(int d=0;d<4;++d){//回転分探す
		itr = lower_bound( block[d].begin(),block[d].end(), ter );
		end = block[d].end();
		bool flag = true;
		for( ;flag && itr != end;++itr ){
			//色のチェック
			for(int c=0;c<4;++c){
				if( ter[c] == -1 )continue;//なんでもいい
				if( ter[c] != (*itr)[c] ){
					flag = false;
				}
			}
			if( flag ){//色はマッチしてる
				int now = (*itr)[4];//ブロック番号
				if( used[now] )continue;//使用済み
				//色を配置する。
				vector<int> next = orgV;//もらった配置
				for(int i=0;i<4;++i){//選択したブロックの色を配置
					next[ men[num][i] ] = (*itr)[i];
				}
				used[now] = true;//使用済みへ
				res = dfs( next, num+1 );
				used[now] = false;
				ans += res;
			}
		}
	}
	return ans;
}

void solve(){
	int N;
	cin >> N;
	vector<int> color(1000,0);
	vector< vector<int> > org(N,vector<int>(4));
	for(int i=0;i<N;++i){
		for(int j=0;j<4;++j){
			cin >> org[i][j];
			color[org[i][j]]++;
		}
	}
	int sum = 0;
	for(int i=0;i<1000;++i){
		if( color[i] < 3 ){
			color[i] = 0;
		}
	}
	for(int i=0;i<N;++i){
		bool flag = true;
		for(int j=0;j<4;++j){
			if( color[ org[i][j] ] == 0 ){
				flag = false;
			}
		}
		if( flag ){
			++sum;
		}
	}
	block = vector< vector< vector<int> > >(4,
		vector< vector<int> >(sum,
			vector<int>(5)
			)
		);
	int index = 0;
	for(int i=0;i<N;++i){
		bool flag = true;
		for(int j=0;j<4;++j){
			if( color[ org[i][j] ] == 0 ){
				flag = false;
				break;
			}
		}
		if( flag ){//使えるブロック
			for(int d=0;d<4;++d){//回転
				for(int j=0;j<4;++j){
					block[d][index][j] = org[i][(d+j)%4];
				}
				block[d][index][4] = index;//ブロック番号
			}
			++index;
		}
	}
	for(int d=0;d<4;++d){
		sort(block[d].begin(),block[d].end());
	}
	used = vector<bool>(sum,false);
	LL ans = 0;
	men = vector< vector<int> >(6,vector<int>(4));
	men[0][0]=0;men[0][1]=1;men[0][2]=2;men[0][3]=3;
	men[1][0]=1;men[1][1]=0;men[1][2]=5;men[1][3]=4;
	men[2][0]=2;men[2][1]=1;men[2][2]=4;men[2][3]=7;
	men[3][0]=3;men[3][1]=2;men[3][2]=7;men[3][3]=6;
	men[4][0]=0;men[4][1]=3;men[4][2]=6;men[4][3]=5;
	men[5][0]=4;men[5][1]=5;men[5][2]=6;men[5][3]=7;
	/*
	men[i]のi=0から順に埋めていく
	決定した数字が先頭から埋まるように時計回りに選択。
	*/
	for(int i=0;i<=sum-6;++i){
		vector<int> v(8,-1);//未決定-1
		/*
		  5--4
		 /  /|
		0--1 7 ←6が裏
		|  |/
		3--2
		*/
		for(int j=0;j<4;++j){
			v[j] = block[0][i][j];
		}
		int now = block[0][i][4];//ブロック番号
		used[now] = true;//使用済みに
		LL res = dfs( v, 1 );
		ans += res;
		//used[i] = true;
	}
	cout << ans << endl;
}

#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	
	
	solve();
}
#pragma endregion //main()

Submission Info

Submission Time
Task E - Building Cubes with AtCoDeer
User akarin55
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4465 Byte
Status TLE
Exec Time 4203 ms
Memory 384 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 3
AC × 14
TLE × 6
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt
Case Name Status Exec Time Memory
0_000.txt AC 1 ms 256 KB
0_001.txt AC 1 ms 256 KB
0_002.txt AC 27 ms 256 KB
1_003.txt TLE 4203 ms 384 KB
1_004.txt TLE 4203 ms 256 KB
1_005.txt TLE 4203 ms 384 KB
1_006.txt TLE 4203 ms 384 KB
1_007.txt TLE 4203 ms 384 KB
1_008.txt TLE 4203 ms 384 KB
1_009.txt AC 1681 ms 384 KB
1_010.txt AC 21 ms 256 KB
1_011.txt AC 7 ms 384 KB
1_012.txt AC 1 ms 256 KB
1_013.txt AC 2 ms 384 KB
1_014.txt AC 1 ms 256 KB
1_015.txt AC 1 ms 256 KB
1_016.txt AC 1 ms 256 KB
1_017.txt AC 1 ms 256 KB
1_018.txt AC 1 ms 256 KB
1_019.txt AC 2 ms 384 KB