题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6862
Problem Description
It is preferrable to read the pdf statment.
If the world is a hexagon, I will take as many turns as possible before reaching the end.
Cuber QQ has constructed a hexagon grid with radius n. This will be better if explained in a picture. For example, this is a hexagon grid with radius 3:
He challenges you take a perfect tour over the hexagon, that is to visit each cell exactly once. Starting from any point in the grid, you can move to any adjacent cell in each step. There are six different directions you can choose from:
Of course, if you are on the boundary, you cannot move outside of the hexagon.
Let D(x,y) denote the direction from cell x to y, and sequence A denotes your route, in which Ai denotes the i-th cell you visit. For index i (1
Maximize the number of turning while ensuring that each cell is visited exactly once. Print your route. If there are multiple solution, print any.
Input
The first line of the input contains a single integer T (1≤T≤104), denoting the number of test cases.
Each of the next T cases:
The first line contains an integer n (2≤n≤500).
It is guaranteed that the sum of n doesn’t exceed 2⋅104.
Output
For each test case, output one line contains a string with 3(n−1)n characters. The i-th character is D(Ai,Ai+1).
Sample Input
1
2
Sample Output
313456
翻译:
输入一个n表示半径。六个方向,不重复的走完所有的六边形,输出路径的方向。路径保证相邻的方向尽量不相同。
分析:
完整代码:
#include
#include
#include
#include
#include
using namespace std;
int t,n;
void solveji()
{
for(int k=n-2;k>=1;k-=2)
{
int a = 4, b = 2;
printf("3");
for(int t=1;t<=5;t++)
{
for(int i=1;i<=k;i++)
printf("%d%d",a,b);
printf("%d",a);
a++,b++;
if(a > 6) a = 1;
if(b > 6) b = 1;
}
printf("%d",a);
for(int i=1;i<=k-1;i++)
printf("%d%d",b,a);
printf("4");
}
printf("\n");
}
void solveou()
{
for(int k=n-1;k>=1;k-=2)
{
int a = 4, b = 2;
printf("3");
for(int t=1;t<=5;t++)
{
if(t == 5 && k == 1) break;
printf("%d",a);
for(int i=1;i<=k-1;i++)
printf("%d%d",b,a);
a++,b++;
if(a > 6) a = 1;
if(b > 6) b = 1;
}
if(k != 1)
{
printf("%d",a);
for(int i=1;i<=k-2;i++)
printf("%d%d",b,a);
}
if(k == 1)printf("3");
else printf("4");
}
printf("\n");
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n % 2) solveji();
else solveou();
}
return 0;
}