#6 TLE求助!!
查看原帖
#6 TLE求助!!
1357239
LCH22014楼主2024/11/9 20:31
#include<bits/stdc++.h>
using namespace std;
int n,x,y,xx=1,yy=1,cnt=0;
int a[31][31];
bool b[31][31];
vector<int>prime;
inline bool is_p(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i == 0)
		return 0;
	}
	return 1;
}
int main()
{
	cin>>n>>x>>y;
	int cont=0,i=2;
	while(cont<=n*n)
	{
		if(is_p(i)){prime.push_back(i);++cont;}
		++i;
	}
	while(!(b[xx+1][yy] && b[xx-1][yy] && b[xx][yy-1] && b[xx][yy+1]))
	{		
		while(!b[xx][yy] && yy<=n)
		{
			b[xx][yy]=1;
			a[xx][yy]=prime[cnt++];
			++yy;
		}
		--yy;
		++xx;
		while(!b[xx][yy] && xx<=n)
		{
			b[xx][yy]=1;
			a[xx][yy]=prime[cnt++];
			++xx; 
		}
		--xx;
		--yy;		
		while(!b[xx][yy] && yy>=1){
			
			b[xx][yy]=1;
			a[xx][yy]=prime[cnt++];
			--yy;
		} 
		++yy;
		--xx;
		while(!b[xx][yy] && xx>=1)
		{
			b[xx][yy]=1;
			a[xx][yy]=prime[cnt++];
			--xx;
		}
		++xx;
		++yy;
	}
	a[xx][yy]=prime[cnt];
	cout<<a[x][y];
    return 0;
}

bui beng

2024/11/9 20:31
加载中...