题意:给个N,表示放N条蛇,蛇的长度分别是1、2、3……N,放到一个恰好(n+1)n/2这么大的矩形里,而且互不相交重合,而且要求奇数号的蛇要奇数次拐弯,偶数编号的蛇要偶数个拐弯点,要输出这个图,并且蛇要按自己的身体顺序输出
思路:有一种规律,按照规律放,同时放的时候记录蛇头,便于最后输出。
规律:放奇数的时候可以把1挪到上面的边上,空出一个躺着的L型。放偶数的时候可以放在侧面,一共放两列,h行
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long LL;
const int INF=0x7fffffff;
const int MAX_N=10000;
struct node{
int x,y;
};
int bottom,lef;
int N,w,h;
int f[2000][2000];
node head[509];
int main(){
while(scanf("%d",&N)!=EOF){
f[1000][1000]=1;
head[1].x=1000;
head[1].y=1000;
w=1;
h=1;
bottom=1000;
lef=1000;
int kind=0;
for(int i=2;i<=N;i++){
if(kind%4==0){
for(int j=lef+w;j<=lef+w+1;j++){
for(int k=bottom;k<=bottom+h;k++){
f[j][k]=i;
}
}
head[i].x=lef+w;
head[i].y=bottom;
w+=2;
}
else if(kind%4==2){
for(int j=lef-2;j<=lef-1;j++){
for(int k=bottom;k<=bottom+h;k++){
f[j][k]=i;
}
}
head[i].x=lef-2;
head[i].y=bottom;
w+=2;
lef-=2;
}
else if(kind%4==1){
f[lef+w-1][bottom+h]=1;
head[1].x=lef+w-1;
head[1].y=bottom+h;
for(int j=lef;j<=lef+w-2;j++){
f[j][bottom+h]=i;
}
f[lef][bottom+h-1]=i;
head[i].x=lef;
head[i].y=bottom+h-1;
h++;
}
else if(kind%4==3){
f[lef][bottom+h]=1;
head[1].x=lef;
head[1].y=bottom+h;
for(int j=lef+1;j<=lef+w-1;j++){
f[j][bottom+h]=i;
}
f[lef+w-1][bottom+h-1]=i;
head[i].x=lef+w-1;
head[i].y=bottom+h-1;
h++;
}
kind++;
}
printf("%d %d\n",h,w);
printf("%d %d\n",bottom+h-head[1].y,head[1].x-lef+1);
for(int i=2;i<=N;i++){
if(i%2==0){
for(int j=1;j<=i/2;j++){
printf("%d %d ",bottom+h-head[i].y-j+1,head[i].x-lef+1);
}
for(int j=i/2;j>=2;j--){
printf("%d %d ",bottom+h-head[i].y-j+1,head[i].x-lef+1+1);
}
printf("%d %d\n",bottom+h-head[i].y,head[i].x-lef+1+1);
}
else{
printf("%d %d ",bottom+h-head[i].y,head[i].x-lef+1);
if(f[head[i].x+1][head[i].y+1]==f[head[i].x][head[i].y]){
for(int j=1;j<=i-2;j++){
printf("%d %d ",bottom+h-head[i].y-1,head[i].x-lef+1+j-1);
}
printf("%d %d\n",bottom+h-head[i].y-1,head[i].x-lef+1+i-1-1);
}
else{
for(int j=1;j<=i-2;j++){
printf("%d %d ",bottom+h-head[i].y-1,head[i].x-lef+1-j+1);
}
printf("%d %d\n",bottom+h-head[i].y-1,head[i].x-lef+1-i+2);
}
}
}
}
return 0;
}