Growing Rectangular Spiral( 爱的魔力转圈圈~~~)

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~

 

--------------------------------我只是一条可爱哒分界线-------------------------------

一、问题:

Description

Growing Rectangular Spiral A growing rectangular spiral is a connected sequence of straightline segments starting at the origin. The first 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 first quadrant or determine that there is no such spiral.

Input

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). 

 

Output

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.

 

Sample Input

3

1 1 1

2 3 5

3 8 4

 

Sample Output

1 NO PATH

2 2 3 5

3 6 1 2 3 9 10 11

 

二、题意:

如图所示,从原点出发,按照图中所示进行螺旋转弯,每一次转弯后的长度都必须比转弯前的长度长(至少长1),现给出第一象限的点坐标,问按此方式是否可以达到该点,不能则输出“NO PATHA”(注意都是大写),若能则求出在总路径最短时的转弯次数及每段路径的长度(按递增顺序)。

 

三、思路:

看似复杂,其实无非以下3种情况:

1)x=y;由于题目每段长度都至少比前一段长1个单位,因此不可能到达。

2)x<y;即只需要两次转弯即可,先走x,再走y即可。

3)x>y;除上述两种情况外,就只得转一圈,因为下一段只要比前一段长1即可,无上限,因此如果能达到的话,转一圈完全可以到达,即转弯次数为6。在图中标出每一段的长度,为给后面几段留出空间,使总路径最短,则前三段长度必为1,2,3。此外,需要判断 “y<4”?画图后把后三段的长度表示出来,假设前一段是a+2,中间那段为x+2,则最后一段是a+y,则a+y>=a+4,所以y>=4才能到达,否则也是NON PATH。然后还有非常重要的一点就是,必须由最后一段往前推,不能由前推到最后一段,因为题目要求的是总路径最短,后者得到的不一定是最短。 

 

四、代码:

#include <cstdio>
int main()
{
    int T,k,x,y;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&k,&x,&y);
        printf("%d ",k);
        if(x==y)
            printf("NO PATH\n");
        else if(x<y)
            printf("2 %d %d\n",x,y);
        else if(y<4)
            printf("NO PATH\n");
        else
            printf("6 1 2 3 %d %d %d\n",x-y+5,x+2,x+3);
    }
    return 0;
}

 

--------------------------------我也是有底线的---------------------------------

宇宙第一小仙女\(^o^)/~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值