Submission #661035


Source Code Expand

//TLE

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(i=0;i<n;++i)
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define mp make_pair
#define pb push_back
#define fi first
#define sc second

typedef pair<int,int> p;
typedef pair<p,p> pi;

int main()
{
	int i,j;

	int n;
	cin >>n;
	vector<string> s(n);
	rep(i,n) cin >>s[i];

	if(n>=2)
	{
		for(int T=0; T<1000; ++T)
		{
			int r=rand()%(n-1);
			swap(s[r],s[r+1]);
		}
	}

	int m;
	cin >>m;
	vector<string> r(m);
	rep(i,m) cin >>r[i];

	//候補となる位置
	vector<pi> c;
	rep(i,m)rep(j,m)
	{
		if(r[i][j]=='.')
		{
			//右に伸びるタイプの先頭になる
			if( (j==0&&r[i][j+1]=='.') || (0<j&&j<m-1&&r[i][j-1]!='.'&&r[i][j+1]=='.'))
			{
				c.pb( pi(p(i,j),p(0,0)) );
			}
			//下に伸びるタイプの先頭になる
			if( (i==0&&r[i+1][j]=='.') || (0<i&&i<m-1&&r[i-1][j]!='.'&&r[i+1][j]=='.'))
			{
				c.pb( pi(p(i,j),p(1,0)) );
			}
		}
	}

	int df[2]={0,1}, ds[2]={1,0};

	//始めに各マスの長さを求めておく
	rep(i,n)
	{
		int nf=c[i].fi.fi, ns=c[i].fi.sc;
		int dir=c[i].sc.fi;
		int len=0;
		while(nf<m&&ns<m&&r[nf][ns]!='#')
		{
			++len;
			nf+=df[dir];
			ns+=ds[dir];
		}
		c[i].sc.sc=len;
	}

	//総当り
	vector<int> a(n);
	rep(i,n) a[i]=i;

	vector<string> ans(m);
	do{
		vector<string> t(r);
		bool valid=true;

		//順番に埋めていく
		rep(i,n)
		{
			int nf=c[i].fi.fi, ns=c[i].fi.sc;
			int dir=c[i].sc.fi;
			int len=c[i].sc.sc;

			//枠に合わない
			if(len!=s[a[i]].size())
			{
				valid=false;
				break;
			}
			else
			{
				rep(j,len)
				{
					nf=c[i].fi.fi+df[dir]*j;
					ns=c[i].fi.sc+ds[dir]*j;

					if(t[nf][ns]=='.') t[nf][ns]=s[a[i]][j];
					else
					{//既に文字が入っている場合は一致しているか調べる
						if(t[nf][ns]!=s[a[i]][j])
						{
							valid=false;
							break;
						}
					}
				}
			}

			if(!valid) break;
		}

		if(valid)
		{
			ans=t;
			break;
		}
		else{
			/*
			rep(j,n) printf(" %d",a[j]);
			printf("\n");
			*/
			//この並びは途中で失敗したのでこれ以下の順列を試すのは無意味
			sort(a.begin()+i+1,a.end());
			reverse(a.begin()+i+1,a.end());
		}

	}while(next_permutation(a.begin(),a.end()));

	std::cout << m << std::endl;
	rep(i,m) std::cout << ans[i] << std::endl;
}

Submission Info

Submission Time
Task H - 恐怖!不幸を呼ぶ盾
User imulan
Language C++11 (GCC 4.9.2)
Score 100
Code Size 2497 Byte
Status AC
Exec Time 40 ms
Memory 1020 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 00_sample_00.txt, 00_sample_01.txt, 10_min_00.txt, 10_min_01.txt, 10_min_02.txt, 20_max_00.txt, 20_max_01.txt, 20_max_02.txt, 50_small_00.txt, 50_small_01.txt, 50_small_02.txt, 90_random_00.txt, 90_random_01.txt, 90_random_02.txt, 90_random_03.txt, 90_random_04.txt, 90_random_05.txt, 90_random_06.txt, 90_random_07.txt, 90_random_08.txt, 90_random_09.txt, 99_final_00.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 33 ms 932 KB
00_sample_01.txt AC 28 ms 936 KB
10_min_00.txt AC 32 ms 972 KB
10_min_01.txt AC 29 ms 1020 KB
10_min_02.txt AC 28 ms 1020 KB
20_max_00.txt AC 29 ms 872 KB
20_max_01.txt AC 30 ms 960 KB
20_max_02.txt AC 32 ms 972 KB
50_small_00.txt AC 38 ms 864 KB
50_small_01.txt AC 27 ms 916 KB
50_small_02.txt AC 27 ms 920 KB
90_random_00.txt AC 28 ms 800 KB
90_random_01.txt AC 27 ms 924 KB
90_random_02.txt AC 26 ms 800 KB
90_random_03.txt AC 27 ms 916 KB
90_random_04.txt AC 29 ms 924 KB
90_random_05.txt AC 28 ms 920 KB
90_random_06.txt AC 30 ms 916 KB
90_random_07.txt AC 26 ms 920 KB
90_random_08.txt AC 27 ms 920 KB
90_random_09.txt AC 28 ms 932 KB
99_final_00.txt AC 40 ms 924 KB