結果

問題 No.359 門松行列
ユーザー 🐬hec🐬hec
提出日時 2016-04-18 03:26:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 172,432 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 07:19:46
合計ジャッジ時間 2,709 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) (sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg)))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

bool check(int a,int b,int c){
	if(a==b) return false;
	if(b==c) return false;
	if(c==a) return false;

	if(min({a,b,c})==b) return true;
	if(max({a,b,c})==b) return true;

	return false;
}

bool check(int a[3][3]){
	rep(i,3) if(check(a[i][0],a[i][1],a[i][2])==false) return false;
	rep(i,3) if(check(a[0][i],a[1][i],a[2][i])==false) return false;
	if(check(a[0][0],a[1][1],a[2][2])==false) return false;
	if(check(a[0][2],a[1][1],a[2][0])==false) return false;
	return true;
}

int main(void){
	int t;
	cin >> t;
	rep(loop,t){
		int L,a[3][3];
		cin >> L;
		rep(i,3)rep(j,3) cin >> a[i][j];

		int ans=0;
		int r[2]={-1,-1},c[2];
		rep(i,3)rep(j,3){
			if(a[i][j]==0){
				if(r[0]==-1)
					r[0]=i,c[0]=j;
				else
					r[1]=i,c[1]=j;
			}
		} 

		vector<int> boader;
		boader.push_back(0);
		boader.push_back(L);

		boader.push_back(L/2);
		boader.push_back(L/2+1);
		

		rep(i,2){
			rep(j,3){
				if(i==1) 
					boader.push_back(L-a[j][c[i]]);
				else
					boader.push_back(a[j][c[i]]);
			}
			rep(j,3){
				if(i==1) 
					boader.push_back(L-a[r[i]][j]);
				else
					boader.push_back(a[r[i]][j]);
			}

			if(r[i]==c[i]){
				rep(j,3){
					if(i==1) 
						boader.push_back(L-a[j][j]);
					else
						boader.push_back(a[j][j]);
				}
			}
			if(r[i]+c[i]==2){
				rep(j,3){
					if(i==1) 
						boader.push_back(L-a[2-j][j]);
					else
						boader.push_back(a[2-j][j]);
				}
			}
		}

		uniq(boader);
		int n=boader.size();
		
		rep(i,n-1){
			const int zero=(boader[i]+boader[i+1])/2;
			const int one=L-zero;
			if(zero < 0 || one < 0) continue;
			int tmp[3][3];
			rep(j,3)rep(k,3) tmp[j][k]=a[j][k];
			tmp[r[0]][c[0]]=zero,tmp[r[1]][c[1]]=one;
			
			if(check(tmp)) ans+=boader[i+1]-boader[i]-1;
		}

		int tmp[3][3];
		rep(j,3)rep(k,3) tmp[j][k]=a[j][k];

		int zero=L/2,one=L-zero;		
		tmp[r[0]][c[0]]=zero,tmp[r[1]][c[1]]=one;
		if(check(tmp)) ans++;

		zero=L/2+1,one=L-zero;
		tmp[r[0]][c[0]]=zero,tmp[r[1]][c[1]]=one;
		if(check(tmp)) ans++;

		cout << ans << endl;		
	}	
	return 0;
}
0