题目链接:I题
题目大意:给你n个贪吃蛇,要求你去构造一个矩阵,使得编号为k的贪吃蛇长度为k,并且奇数编号贪吃蛇需要经过奇数个转折点,偶数编号贪吃蛇需要经过偶数个转折点,然后所有贪吃蛇刚好占满这个矩阵的所有各自且不能交叉,要求你输出每条贪吃蛇的路径,不是单纯的位置
题目思路:我们先构造出矩阵的大小,发现这样的一个关系:
1:1,1
1
2:1,2
1,2,2
3:2,3
2,2,3
2,2,3
4:2,5
2,2,3,4,4
1,3,3,4,4
5:3,5
1,2,2,3,5
4,4,3,3,5
4,4,5,5,5
6:3,7
1,2,2,3,5,6,6
4,4,3,3,5,6,6
4,4,5,5,5,6,6
7:4,7
2,2,3,4,4,5,7
1,3,3,4,4,5,7
6,6,6,5,5,5,7
6,6,6,7,7,7,7
然后我们就可以发现规律了,长宽跟n有关,偶数的情况直接在奇数的情况下构造一个右边2*n/2的矩阵就可以了,拐点刚好两个,奇数的情况我们需要考虑到它的n-3的情况,补上后三个就好了,然后代码递归去写就好了
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
void fun(int cur){
if(cur == 1){
printf("1 1\n");
return ;
}
if(cur == 2){
printf("1 1\n");
printf("1 2 1 3\n");
return;
}
if(cur == 3){
printf("2 1\n");
printf("1 1 1 2\n");
printf("1 3 2 3 2 2\n");
return;
}
int tx = (cur+1)/2;
int ty = (cur%2)? tx*2-1:tx*2+1;
if(cur % 2 == 0){
fun(cur-1);
for(int i = 1; i <= cur/2; i ++)
printf("%d %d ",i,ty-1);
for(int i = cur/2; i >= 1; i--)
printf("%d %d ",i,ty);
printf("\n");
return ;
}
else{
fun(cur-3);
for(int i = 1; i <= (cur-2)/2; i++)
printf("%d %d ",i,ty-1);
for(int i = 1; i <= (cur-2)/2+1; i++)
printf("%d %d ",tx-1,ty-i);
printf("\n");
for(int i = 1; i <= (cur-1)/2; i++)
printf("%d %d ",tx-1,i);
for(int i = 0; i <= (cur-1)/2-1; i++)
printf("%d %d ",tx,(cur-1)/2-i);
printf("\n");
for(int i = 0; i < cur/2+1; i++)
printf("%d %d ",tx,ty-cur/2+i);
for(int i = 0; i < cur/2; i++)
printf("%d %d ",tx-i-1,ty);
printf("\n");
return ;
}
}
int main(){
int n;
while(~scanf("%d",&n)){
printf("%d %d\n",(n+1)/2,(n%2)?(n+1)/2*2-1:(n+1)/2*2+1);
fun(n);
}
return 0;
}