• 【规律】Growing Rectangular Spiral


    Growing Rectangular Spiral

    题目描述

    A growing rectangular spiral is a connected sequence of straightline segments starting at the origin. The fi rst segment goes right (positive x direction). The next segment goes up (positive y direction). The next segment goes left (negative x direction). The next segment goes down (negative y direction) and the sequence of directions repeats. Each segment has integer length and each segment is at least one unit longer than the previous segment. In the spiral on the right, the segment lengths are 1, 2, 4, 6, 7, 9,11, 12, 15, 20.
    Write a program to determine the shortest growing rectangular spiral (in total length) that ends at a given integer point (x, y) in the fi rst quadrant or determine that there is no such spiral.

    输入

    The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
    Each data set consists of a single line of input consisting of three space separated decimal integers.
    The first integer is the data set number. The next two integers are the x and y coordinates of the desired end point (1 ≤ x ≤ 10000, 1 ≤ y ≤ 10000).

    输出

    For each data set there is a single line of output. If there is no spiral solution, the line consists of the data set number, a single space and ‘NO PATH’ (without the quotes). If there is a solution, the line consists of the data set number, a single space, the number of segments in the solution, a single space,followed by the lengths of the segments in order, separated by single spaces. The input data will be chosen so that no path requires more than 22 segments.

    样例输入

    3
    1 1 1
    2 3 5
    3 8 4
    

    样例输出

    1 NO PATH
    2 2 3 5
    3 6 1 2 3 9 10 11


    【题解】

      问是否存在一条螺旋折线使得跑到(x,y)点,每一次转折都是严格递增的顺序。

      请输出存在的路径。如果没有则输出"NO PATH"

    【规律】

      1、如果是(x,y)y>x明显是有一条两次转折到达的点。

      2、如果  y==x,是不存在这样的路径。

      3、如果是  x<y,其实是利用x,y的差值关系来构建出来6步达到的效果,具体看代码。

     1 #pragma GCC optimize("Ofast,no-stack-protector")
     2 #pragma GCC optimize("O3")
     3 #pragma GCC optimize(2)
     4 #include <bits/stdc++.h>
     5 #define inf 0x3f3f3f3f
     6 #define linf 0x3f3f3f3f3f3f3f3fll
     7 #define pi acos(-1.0)
     8 #define nl "
    "
     9 #define pii pair<ll,ll>
    10 #define ms(a,b) memset(a,b,sizeof(a))
    11 #define FAST_IO ios::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL)
    12 using namespace std;
    13 typedef long long ll;
    14 const int mod = 998244353;
    15 ll qpow(ll x, ll y){ll s=1;while(y){if(y&1)s=s*x%mod;x=x*x%mod;y>>=1;}return s;}
    16 //ll qpow(ll a, ll b){ll s=1;while(b>0){if(b%2==1)s=s*a;a=a*a;b=b>>1;}return s;}
    17 inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
    18   
    19 const int N = 1e5+5;
    20   
    21   
    22 int main()
    23 {
    24     int _, cas, x, y;
    25     for(scanf("%d",&_);_--;)
    26     {
    27         scanf("%d",&cas);
    28         scanf("%d%d",&x,&y);
    29         printf("%d ",cas);
    30         if(x==y) puts("NO PATH");
    31         else if(x<y){
    32             printf("2 %d %d
    ",x,y);
    33         }
    34         else{
    35             if(y<4) puts("NO PATH");
    36             else{
    37                 printf("6 1 2 3 %d %d %d
    ",x+3-y+2, x+2, x+3);
    38             }
    39         }
    40   
    41     }
    42   
    43 }
    View Code
  • 相关阅读:
    灰度图转换
    OGRE分析之文件系统 (1)
    屏幕截图
    [GP]template必须定义于头文件中
    OGRE分析之设计模式
    ON_COMMAND_RANGE和ON_UPDATE_COMMAND_UI_RANGE
    使用SkinMagic Toolkit美化界面(II)
    Single Sign On for Windows and Linux
    "C compiler cannot create executables"
    How to Create a First C Program on Linux
  • 原文地址:https://www.cnblogs.com/Osea/p/11387196.html
Copyright © 2020-2023  润新知