求大佬解答为什么不对
查看原帖
求大佬解答为什么不对
1213793
MiTuan_楼主2023/11/22 21:26
#include <cstring>
#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int,int> PII;

const int N=40,M=N*N;

int g[N][N];
PII st[M];
int n;

int main()
{
	cin>>n;
	g[n>>1][0]=1;
	st[1]={n>>1,0};
	for(int i=2;i<n*n;i++)
	{
		if(st[i-1].y==0)
		{
			if(st[i-1].x==n-1) 
			{
				st[i]={st[i-1].x,st[i-1].y+1};
				g[st[i].x][st[i].y]=i;
			}
			else 
			{
				st[i]={st[i-1].x+1,n-1};
				g[st[i].x][st[i].y]=i;
			}
		}
		if(st[i-1].x==n-1&&st[i-1].y!=0)
		{
			st[i]={0,st[i-1].y-1};
			g[st[i].x][st[i].y]=i;
		}
		else
		{
			if(g[st[i-1].x+1][st[i-1].y-1])
			{
				st[i]={st[i-1].x+1,st[i-1].y-1};
				g[st[i].x][st[i].y]=i;
			}
			else
			{
				st[i]={st[i-1].x,st[i-1].y+1};
				g[st[i].x][st[i].y]=i;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
	{
		cout<<g[j][i]<<" ";
	}
	cout<<endl;
	}
	
}
2023/11/22 21:26
加载中...