結果

問題 No.223 1マス指定の魔方陣
ユーザー 👑 kmjpkmjp
提出日時 2015-06-05 23:15:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 3,445 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 152,072 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 19:26:11
合計ジャッジ時間 3,477 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 3 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,X,Y,Z;
int pat[16][16];
int pat2[16][16];

int patt[10][4][4]={
	{{1,1,0,0},{0,0,1,1},{1,1,0,0},{0,0,1,1}},
	{{1,0,1,0},{0,1,0,1},{0,1,0,1},{1,0,1,0}},
	{{1,1,0,0},{0,0,1,1},{0,0,1,1},{1,1,0,0}},
	{{1,1,0,0},{1,1,0,0},{1,0,1,0},{0,0,1,1}},
	{{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,0}},
	{{1,0,1,0},{1,0,1,0},{0,1,0,1},{0,1,0,1}},
	{{1,0,0,1},{0,1,1,0},{1,0,0,1},{0,1,1,0}},
	{{1,0,0,1},{1,0,0,1},{0,1,1,0},{0,1,1,0}},
	{{1,1,0,0},{1,0,1,0},{0,1,0,1},{0,0,1,1}},
	{{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,0}},
};

int get1(int pat,int y,int x,int t) {
	if(t) return patt[pat][y][x];
	else return patt[pat][3-y][3-x];
}
int get2(int a,int y,int x) {
	if(a>=10) return patt[a-10][y][x];
	return patt[a][3-y][3-x];
}
int get3(int a,int b,int c,int d,int y,int x) {
	return get2(a,y,x)*8+get2(b,y,x)*4+get2(c,y,x)*2+get2(d,y,x);
}

void dodo(int yy,int xx,int zz) {
	int a,b,c,d;
	int y,x;
	FOR(a,20) FOR(b,20) FOR(c,20) FOR(d,20) {
		if(zz!=get3(a,b,c,d,yy,xx)) continue;
		set<int> S;
		FOR(y,4) FOR(x,4) pat[y][x]=get3(a,b,c,d,y,x), S.insert(pat[y][x]);
		if(S.size()!=16) continue;
		int ng=0;
		FOR(y,4) {
			int t=0;
			FOR(x,4) t+=pat[y][x];
			if(t!=30) ng++;
		}
		FOR(x,4) {
			int t=0;
			FOR(y,4) t+=pat[y][x];
			if(t!=30) ng++;
		}
		if(pat[0][0]+pat[1][1]+pat[2][2]+pat[3][3]!=30) ng++;
		if(pat[0][3]+pat[1][2]+pat[2][1]+pat[3][0]!=30) ng++;
		if(ng==0) return;
	}
	assert(0);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>X>>Y>>Z;
	X--;
	Y--;
	Z--;
	if(N==4) {
		dodo(Y,X,Z);
	}
	else if(N==8) {
		dodo(Y%4,X%4,Z%16);
		FOR(y,4) FOR(x,4) {
			pat[y+4][x+4]=pat[y][x];
			pat[y+4][x]=pat[y][x];
			pat[y][x+4]=pat[y][x];
		}
		int p[4][4]={
			{0,3,1,2},
			{3,0,2,1},
			{1,2,0,3},
			{2,1,3,0},
		};
		memmove(pat2,pat,sizeof(pat));
		FOR(i,4) {
			memmove(pat,pat2,sizeof(pat));
			FOR(y,8) FOR(x,8) {
				if(y<4) pat[y][x] += 16*p[i][x/2];
				else pat[y][x] += 16*p[i^1][x/2];
			}
			if(pat[Y][X]==Z) break;
		}
		
	}
	else if(N==16) {
		dodo(Y%4,X%4,Z%16);
		FOR(y,4) FOR(x,4) {
			pat[y+4][x+4]=pat[y][x];
			pat[y+4][x]=pat[y][x];
			pat[y][x+4]=pat[y][x];
		}
		int p[4][4]={
			{0,3,1,2},
			{3,0,2,1},
			{1,2,0,3},
			{2,1,3,0},
		};
		memmove(pat2,pat,sizeof(pat));
		FOR(i,4) {
			memmove(pat,pat2,sizeof(pat));
			FOR(y,8) FOR(x,8) {
				if(y<4) pat[y][x] += 16*p[i][x/2];
				else pat[y][x] += 16*p[i^1][x/2];
			}
			if(pat[Y%8][X%8]==Z%64) break;
		}
		FOR(y,8) FOR(x,8) {
			pat[y+8][x+8]=pat[y][x];
			pat[y+8][x]=pat[y][x];
			pat[y][x+8]=pat[y][x];
		}
		memmove(pat2,pat,sizeof(pat));
		FOR(i,4) {
			memmove(pat,pat2,sizeof(pat));
			FOR(y,16) FOR(x,16) {
				if(y<8) pat[y][x] += 64*p[i][x/4];
				else pat[y][x] += 64*p[i^1][x/4];
			}
			if(pat[Y][X]==Z) break;
		}
		
	}
	FOR(y,N) {
		FOR(x,N) _P("%d%c",pat[y][x]+1,(x==N-1)?'\n':' ');
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0