結果

問題 No.401 数字の渦巻き
ユーザー evawenis
提出日時 2020-06-25 10:50:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 1,912 ms
コンパイル使用メモリ 200,108 KB
最終ジャッジ日時 2025-01-11 10:09:20
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	int n; cin>>n;
	if(n<3){
		cout<<"001"s;
		if(n==2)cout<<" 002\n004 003"s;
		cout<<"\n"s;
		return 0;
	}
	vector<vector<int>>v(n,vector<int>(n,0));
	int lim=n*n,y=0,x=0,cnt=1;
	while(cnt<=lim){
		while(x<n&&v.at(y).at(x)==0&&cnt<=lim)v.at(y).at(x)=cnt,++cnt,++x;
		if(cnt<=lim)++y,--x;
		while(y<n&&v.at(y).at(x)==0&&cnt<=lim)v.at(y).at(x)=cnt,++cnt,++y;
		if(cnt<=lim)--y,--x;
		while(x>=0&&v.at(y).at(x)==0&&cnt<=lim)v.at(y).at(x)=cnt,++cnt,--x;
		if(cnt<=lim)--y,++x;
		while(y>=0&&v.at(y).at(x)==0&&cnt<=lim)v.at(y).at(x)=cnt,++cnt,--y;
		if(cnt<=lim)++y,++x;
	}
	for(int i=0;i<n;++i){
		cout<<setfill('0')<<setw(3)<<v.at(i).at(0);
		for(int j=1;j<n;++j){
			cout<<" "s<<setfill('0')<<setw(3)<<v.at(i).at(j);
		}
		cout<<"\n"s;
	}
}
0