algorithm_2024/P2615/P2615.cpp

51 lines
1.4 KiB
C++
Raw Normal View History

2024-10-01 13:38:46 +00:00
#include <iostream>
#include <istream>
2024-10-01 16:00:28 +00:00
#include <ranges>
2024-10-01 13:38:46 +00:00
using std::cin,std::cout;
2024-10-01 16:00:28 +00:00
static constexpr const auto range = std::ranges::views::iota;
const int MAX_N = 39+5;
int n;
int map[MAX_N][MAX_N];
int x,y,now{1};
2024-10-01 13:38:46 +00:00
int main(){
2024-10-01 14:03:35 +00:00
std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
2024-10-01 13:38:46 +00:00
2024-10-01 16:00:28 +00:00
std::cin>>n;
x=1,y=n/2+1;
map[x][y]=now++;
/*
1. $(K-1)$ $K$ $(K-1)$
2. $(K-1)$ $K$ $(K-1)$
3. $(K-1)$ $K$ $(K-1)$
4. $(K-1)$ $(K-1)$ $K$ $(K-1)$ $K$ $(K-1)$
*/
while(now<=n*n){
if(x==1 && y!=n){
x=n,y++;
}else if(y==n && x!=1){
y=1,x--;
}else if(x==1 && y==n){
x++;
}else if(x!=1 && y!=n){
if(map[x-1][y+1]==0){
x--;
y++;
}else{
x++;
}
}
map[x][y]=now++;
}
for(int i:range(1,n+1)){
for(int j:range(1,n+1)){
std::cout<<map[i][j]<<" ";
}
std::cout<<"\n";
}
2024-10-01 13:38:46 +00:00
}