2 solutions

  • 0
    @ 2025-10-18 18:50:25
    using namespace std;
    int n,x,y,dir=0,xx,yy,t,ge;
    int main()
    {
    	cin>>n>>x>>y;
    	if(x==1)
    	{
    		cout<<y;
    		return 0;
    	}
    	xx=1,yy=n,t=n,ge=n-1;
    	while(t<n*n)
    	{
    		for(int i=1;i<=2;i++)
    		{
    			if(dir==0)
    			{
    				for(int j=ge;j>=1;j--)
    				{
    					xx++;
    					t++;
    					if(xx==x&&yy==y)
    					{
    						cout<<t;
    						return 0;
    					}
    				}
    			}
    			if(dir==1)
    			{
    				for(int j=ge;j>=1;j--)
    				{
    					yy--;
    					t++;
    					if(xx==x&&yy==y)
    					{
    						cout<<t;
    						return 0;
    					}
    				}
    			}
    			if(dir==2)
    			{
    				for(int j=ge;j>=1;j--)
    				{
    					xx--;
    					t++;
    					if(xx==x&&yy==y)
    					{
    						cout<<t;
    						return 0;
    					}
    				}
    			}
    			if(dir==3)
    			{
    				for(int j=ge;j>=1;j--)
    				{
    					yy++;
    					t++;
    					if(xx==x&&yy==y)
    					{
    						cout<<t;
    						return 0;
    					}
    				}
    			}
    			dir=(dir+1)%4;
    		}
    		ge--;
    	}
    	return 0;
    }
    
    
    • -1
      @ 2024-6-12 10:45:31

      模拟(n2n^2 50pts)

      • 使用类似于蛇形填数的方式,依次填下每个数,然后输出要求的位置的数值
      #include<bits/stdc++.h>
      using namespace std;
      const int N=1010;
      int dx[]={1,0,-1,0};//行下左上右
      int dy[]={0,-1,0,1};//列下左上右
      int a[N][N];
      int main()
      {
          int n,ex,ey;
          cin>>n>>ex>>ey;
          int d=3; //最开始的方向往右
          int x=1,y=1; //(1,1)开始
          for(int i=1;i<=n*n;i++)
          {
              a[x][y]=i;
              int tx=x+dx[d],ty=y+dy[d];
              //(tx,ty)是由(x,y)通过d方向可以走到的点 
              if(tx>n||tx<1||ty>n||ty<1||a[tx][ty]!=0) 
              //越界,或者(tx,ty)已经有数 
              {
                  d++; //换个方向 
                  if(d==4) d=0; //方向走完以后从下开始 
                  tx=x+dx[d],ty=y+dy[d]; 
                  //由新的方向可以走到的位置 
              }
              x=tx,y=ty;
          }   
          cout<<a[ex][ey];
          return 0;
      }
      

      找规律+模拟(100pts)

      • 先找到所在的圈数,然后模拟填数
      #include<bits/stdc++.h>
      using namespace std;
      int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; //右下左上 
      int main()
      {
      	int n,X,Y;
      	cin>>n>>X>>Y;
      	int k=min(min(X,Y),min(n-X+1,n-Y+1));
      	int res=0;
      	for(int i=1,j=n-1;i<k;i++,j-=2) //计算外围的点 
      	{
      		res+=4*j;
      	}
      	
      	res++; //下个位置需要填写的数 
      
      	if(X==k&&Y==k) cout<<res; //找到答案 
      	else
      	{
      		int d=0,x=k,y=k;
      		for(int i=0;i<4;i++)
      		{
      			for(int j=1;j<=n-2*(k-1)-1;j++) //n-2*(k-1)-1个数转个方向 
      			{
      				res++; //下个位置 
      				int a=x+dx[d],b=y+dy[d]; //往后走 
      				if(a==X&&b==Y) //找到答案 
      				{
      					cout<<res<<endl;
      				}
      				x=a,y=b;
      			}	
      			d++; //下个方向 
      		}	
      	} 
      	return 0; 
      }
      
      • 1

      Information

      ID
      463
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      9
      Tags
      # Submissions
      18
      Accepted
      3
      Uploaded By