Position of F question in the second session of China University of Petroleum 2019-2020 Training Competition for Universities, Middle Schools and Primary Schools [Spiral Matrix + DFS]

Question link http://icpc.upc.edu.cn/problem.php?cid=1961&pid=5

Title description

Since Chenchen has not developed the core algorithm yet, she is always defeated in the game. Chenchen took out his killer's mace to counterattack, and meticulously designed a large-scale problem of finding positions by taking numbers: N N (N is an odd number) floor tiles, each with a number written on it, and these numbers are exactly 1 to N square. She laid the floor tiles spirally on the ground from the middle in order to form an N N square. When N=5, as shown below:

The position of each tile is indicated by a row and column number. The position of the floor tiles on the upper left is the first row and the first column, and the position of the floor tiles on the lower right is the Nth row and Nth column. The floor tile numbered 3 in the picture above is in the second row and the third column.

If a tile is in X row and Y column, the position code is X * N+Y. For example: the address position code with the number 1 in the above figure is: 5 * 3+3 = 18; the address position code with the number 5 is: 5*3+4 = 19.

Chenchen must clearly calculate what is the position code of the number A. For example: N=5, A=6, is: 24

Insert picture description here

Before each question, Chenchen wants to know the correct answer. Please program and calculate the position code.

enter

The first line: 1 odd number N.
The second line: 1 integer A. (0<A<=N*N)

Output

An integer representing the position code of the number A floor tile.
Sample input Copy
5
20

Sample output

28

prompt

Data range: 80% data 0<N<1000; 100% data 0<N<100000.

problem analysis:

First of all, this is a hierarchical matrix, and we have to find the recurrence law in it. You can first deduct the 1 in the center. When the input is 1, you only need to add a special judgment;
consider 1 as the first layer, and each ring is a layer.

When the matrix is ​​in the nth layer:
the value in the upper left corner: (2n-3)² +1
the value in the upper right corner: (2n-3)² +n +2
the value in the lower right corner: (2n-3)² +(2 n)+3
The value in the lower left corner: (2n-3)² +(3
n)+4
Insert picture description here
Picture: qq_35339563
Then we can search from the outermost layer to the inside to
find the number we typed in the layer,
and then the four corners The value of divides each layer of the matrix into four regions
to determine its position.

1 Find out which level the number is looking for
2 Find out which area of ​​the four areas the number is in
3 Determine its rank

Attach the code:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;                   // a 为行, b为列。  t为要寻找的数字,  n为矩阵的目前规格 
ll t,n,a,b; 
void dfs(ll n,ll t ) {
    
    
	ll q,w,e,r,l,circle, maxn;
	maxn=n*n;                          //  该层的最大值
	l=pow((n-2),2);
	q=l+1;                             //  左上角
	w=l+1*n-1;                        //  右上角
	e=l+2*n-2;                        //   右下角
	r=l+3*n-3;                       //   左下角
	if(n==1) {
    
    
		a=1;
		b=1;
		return;
	}
	if(t>=q&&t<=maxn) {
    
                       //  如果要搜索的数字在这一层中,进入寻找。     如果不在进入下面else
		if(t>=q&&t<=w) {
    
    
			a=1;
			b=t-q+1;
		} else if(t>w&&t<=e) {
    
    
			b=n;
			a=t-(w);
		} else if(t>e&&t<=r) {
    
    
			a=n;
			b=n-(t-e)+1;
		} else {
    
    
			b=1;
			a=n-(t-r)+1;
		}
		return ;
	} else {
    
                            // 要找的数不在上一层循环中,  进入下一层, a++,b++;
		dfs(n-2,t);
		a++;
		b++;
		return ;
	}
}
int main() {
    
    
	scanf("%lld%lld",&n,&t);
	dfs(n,t);
	printf("%lld",a*n+b);
	return 0;
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/103218596