A. Nucleic acid detection

A. Nucleic acid detection

Time limit: 100ms
Space limit: 128MB


Title Description
Outbreak of a new coronavirus! A large number of suspected patients in a city are concentrated at each isolation point, and the initial diagnosis is very
difficult. The expert team needs to go to these isolation points to guide on-site to perform nucleic acid testing for the suspected patients. There are n (n + 1) isolation points in the city, which form a square matrix of n rows and n + 1 columns. We use (r, c) to denote the isolation point located in row r from top to bottom and column c from left to right. For example, the coordinate of the upper left corner is (1,1), and
the coordinate of the lower right corner is (n, n + 1).
The traffic structure of this city is quite special, and there is a two-way path between any two diagonally adjacent isolation points.
In addition, there is a one-way subway loop running clockwise at the boundary of the square matrix. The following figure is a
schematic diagram of a city with n = 5 :
Insert picture description here
experts can start from any isolation point, and then you can go to other isolation points along the road or by subway. It takes a unit of time to walk a road and take a subway. The time required for nucleic acid detection at the isolation point is negligible.
Experts are anxious to know the minimum time required to complete nucleic acid testing at all isolation points. Request the minimum time required and a route.
Input description:
Enter a total of one line, including an integer n (2 <= n <= 100), indicating the number of lines in the square matrix of isolation points.
Output description The
first line outputs an integer that represents the time T spent.
Next contains the T + 1 line. The i-th line of this part contains two integers xi, yi, separated by a space, indicating that the coordinate of the i-th isolation point passed by the route you constructed is (xi, yi).
If there are multiple feasible solutions, please Output any feasible solution.
Sample input

2

Sample output

5
1 1 
1 2 
1 3
2 3 
2 2 
2 1

Insert picture description here
This question we use deep search will time out, but deep search tells us that T must be equal to n (n + 1) -1,
so I thought there must be a way to traverse all points without repeating points . We can think of this traversal as being in a circle, and control the traversal through several judgments, as shown below: (note that the red dot needs to be judged).
Insert picture description here

Regrettably, I only made this method one second after the game ended. As for whether it is right or wrong, please correct me in the comment area, but I think there should be no problem

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
int n,ex=1,ey=1,x=2,y=2,step=1;
int main()
{
	//fre();
	scanf("%d",&n);
	int ans=n*(n+1)-1;
	printf("%d\n1 1\n2 2\n",ans);
	while(step!=n*(n+1)-1)
	{
		int tx=x,ty=y;
		if(x==1) //第一行 
		{
			if(y==n+1) x++;
			else if(y==n||ex!=x) y++;  
			else x++,y++;	 
		}
		else if(y==1)    {if(ey!=y&&ex!=x) x--;  else x--,y++;}	            //第一列
		else if(x==n)    {if(ex!=x) y--;  else x--,y++;}                   //第n行
		else if(y==n+1)	 {if(ey!=y||(x==2&&!(n&1)))	x++;  else x++,y--;}  //第n+1列
		else //中间 
		{
			if(ex==x+1&&y!=n)	x--,y++;
			else if(ex==x-1)    x++,y--;
			else x++,y++;
		} 
		printf("%d %d\n",x,y);
		step++,ex=tx,ey=ty;	
	}	
	return 0;
}

Published 130 original articles · won 93 · views 6805

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/105332702