結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-06 02:51:23 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 120 ms / 5,000 ms |
| コード長 | 5,148 bytes |
| コンパイル時間 | 2,465 ms |
| コンパイル使用メモリ | 83,388 KB |
| 実行使用メモリ | 53,068 KB |
| 最終ジャッジ日時 | 2024-07-05 02:53:53 |
| 合計ジャッジ時間 | 5,420 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class Main implements Runnable{
public static void main(String[] args) {
new Thread(null,new Main(), "" ,Runtime.getRuntime().maxMemory()).start();
}
boolean check(int[][] a) {
int n=a.length;
boolean ret=true;
long sum=Arrays.stream(a[0]).sum();
for(int i=0;i<a.length;++i)ret&=Arrays.stream(a[i]).sum()==sum;
{
int s=0;
for(int i=0;i<a.length;++i) {
s+=a[i][i];
}
ret=s==sum;
s=0;
for(int i=0;i<a.length;++i) {
s+=a[n-1-i][i];
}
ret=s==sum;
}
//縦はさぼる。
return ret;
}
void apply(int y,int x,int base,int[][] add,int[][] subject) {
for(int i=0;i<2;++i) {
for(int j=0;j<2;++j) {
subject[y+i][x+j]=base+add[i][j];
}
}
}
int[][] magic_square_LUX(int N){
assert(N%4==2);
int[][] a=magic_square_odd(N/2);
int[][] ret=new int[N][N];
for(int i=0;i<a.length;++i)
for(int j=0;j<a[i].length;++j)
a[i][j]=(a[i][j]-1)*4;
int[][] L= {{4,1},{2,3}};
int[][] U= {{1,4},{2,3}};
int[][] X= {{1,4},{3,2}};
for(int i=0;i<N/2;++i) {
for(int j=0;j<N/2;++j) {
if(i==N/4&&j==N/4) {// U
apply(i*2,j*2,a[i][j],U,ret);
}else if(i==N/4+1&&j==N/4) {// L
apply(i*2,j*2,a[i][j],L,ret);
}else if(i<=N/4) {// L
apply(i*2,j*2,a[i][j],L,ret);
}else if(i==N/4+1) {// U
apply(i*2,j*2,a[i][j],U,ret);
}else {// X
apply(i*2,j*2,a[i][j],X,ret);
}
}
}
return ret;
}
int[][] magic_square_four(int N){
assert(N%4==0);
int[][] ret=new int[N][N];
for(int i=0;i<N;++i) {
for(int j=0;j<N;++j) {
if(((i+1)/2+(j+1)/2)%2==0) {
ret[i][j]=i*N+j+1;
}else {
ret[i][j]=N*N-(i*N+j);
}
}
}
return ret;
}
int[][] magic_square_odd(int N){
assert(N%2==1);
int[][] m=new int[2*N-1][2*N-1];
{
int v=1;
int center=(2*N-1)/2;
for(int i=0;i<N;++i) {
int x=(2*N-1)/2-i;
int y=i;
for(int j=0;j<N;++j) {
m[y++][x++]=v++;
}
}
for(int i=0;i<2*N-1;++i) {
for(int j=0;j<2*N-1;++j) {
if(m[i][j]==0)continue;
if(center-i>N/2) {
m[i+N][j]=m[i][j];
}
if(i-center>N/2) {
m[i-N][j]=m[i][j];
}
if(center-j>N/2) {
m[i][j+N]=m[i][j];
}
if(j-center>N/2) {
m[i][j-N]=m[i][j];
}
}
}
}
int[][] ret=new int[N][N];
for(int i=0;i<N;++i) {
ret[i]=Arrays.copyOfRange(m[i+N/2], N/2, N/2+N);
}
return ret;
}
int[][] magic_square(int N){
if(N%2==1)return magic_square_odd(N);
else if(N%4==0)return magic_square_four(N);
else return magic_square_LUX(N);
}
public void run() {
FastScanner sc=new FastScanner();
int N=sc.nextInt();
int[][] m=magic_square(N);
for(int i=0;i<m.length;++i) {
for(int j=0;j<m[i].length;++j) {
System.out.print(m[i][j]+(j==m[i].length-1?"\n":" "));
}
}
}
void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() { return Double.parseDouble(next());}
}