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;
    }
    
    

    Information

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