【规律】Growing Rectangular Spiral

Growing Rectangular Spiral

Title Description

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.

Entry

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

Export

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


【answer】

  Ask whether there is a spiral fold line so that went to (x, y) point, every twist and turn is strictly increasing sequence.

  Please output path exists. If not, the output "NO PATH"

【law】

  1, if (x, y) y> x is clearly a two turning points arrive.

  2, if y == x, is such a path does not exist.

  3, if x <y, in fact, the use of x, y relationship to build up the difference to achieve the effect of step 6, see the specific code.

 

 

 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 "\n"
 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\n",x,y);
33         }
34         else{
35             if(y<4) puts("NO PATH");
36             else{
37                 printf("6 1 2 3 %d %d %d\n",x+3-y+2, x+2, x+3);
38             }
39         }
40   
41     }
42   
43 }
View Code

 

Guess you like

Origin www.cnblogs.com/Osea/p/11387196.html