HihoCoder #1257 (2015-2016 ACM 北京站) Snake Carpet [构造题]

来源:互联网 发布:美容院用的软件 编辑:程序博客网 时间:2024/05/16 08:37

Description 

In school of EECS of Peking University, there is a homework for all freshman -- thecontest of AI snakes. This contest is ended today. Bacchus has got a very good result, so hedecides to make a carpet full of snakes as a souvenir, and lays it over the floor in his room.As his room is square, a square carpet is needed. A H×W carpets is made up of H×Wunits(each unit is 1×1). Snakes can have different length, but all snakes' width is 1 unit. Forsome reason, He hopes that N special snakes are drawn on the carpet: the length of the ithsnake should be i, which can be seen as i connected units(Two units that share an edge areconsidered connected). Except the first snake, the (2k−1)th snake should have positive oddnumber of turning points; except the second snake, the 2kth snake should have an positiveeven number of turning points. i and k both start from 1. Each snake should not intersectwith itself, nor with other snakes. All units of the carpet must be covered by snakes.But the question is whether there is a solution.


题意:

构造出一个H*W的棋盘,放满N条蛇,第i条的蛇的长度为i,宽度为1.同时,除了第一条蛇的奇数长度的蛇,拐点要为正奇数个,除了第二条蛇的偶数长度的蛇,拐点要正偶数个。

范围:(Special Judge)

N<=500 

解法:

构造方案如下:

N为奇数时,H=N/2+1,W=N

N为偶数时,H=N/2,W=N+1

可以分成两部分进行构造,左半部分都是奇数长度的蛇,右半部分都是偶数长度的蛇。

例子:

N=9时

  1  3  5  7  9  |-||-|  2  2  8  8
  3  3  5  7  9  |-||-|  4  4  8  8
  5  5  5  7  9  |-||-|  4  4  8  8
  7  7  7  7  9  |-||-|  6  6  8  8
  9  9  9  9  9  |-||-|  6  6  6  6

N=8 时


  1  3  5  7  |-||-|  2  4  4  6  6
  3  3  5  7  |-||-|  2  4  4  6  6
  5  5  5  7  |-||-|  8  8  8  8  6
  7  7  7  7  |-||-|  8  8  8  8  6

左边是非常容易造的,右半部分需要沿着偶数的那条边,两列两列(或者两行两行)进行构造,然后从左上角沿着M型轨道作为Order即可。

例如N=9的轨迹(从左上角开始):

→ ↓ →停

↓← ↑ ←

→ ↓ → ↑

↓← ↑ ←

→→→ ↑

同理N=8的轨迹:

↓  →↓  → ↓

→ ↑  →↑  ↓

 ↓ ← ↓ ← ↓  

停 ↑ ← ↑ ←


代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input    char c; int sgn; T bit=0.1;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    if(c==' '||c=='\n'){ ret*=sgn; return 1; }    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;    ret*=sgn;    return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)#define lowbit(x) x&-xusing namespace std;typedef long long ll;typedef pair<int,int> pii;int n;struct node{    int x,y;    node(){}    node(int xx,int yy){        x=xx;        y=yy;    }    void swapx(){        swap(x,y);    }}st[1001000];int top;vector<node> c[1111];int d[1111][1111];void draw(int h,int w){    top=0;    int pd=0;    if(h%2==0){        int x=1,y=1;        st[++top]=node(x++,y);        int lr=1;        int ud=0;        while(top<=h*w){            if(x%4==2&&y==w||x%4==0&&y==1){                st[++top]=node(x++,y);                st[++top]=node(x++,y);                lr^=1;                continue;            }            if(lr)st[++top]=node(x,y++);            else st[++top]=node(x,y--);            if(ud)st[++top]=node(x++,y);            else st[++top]=node(x--,y);            ud^=1;        }    }    else{        int x=1,y=1;        st[++top]=node(x,y++);        int lr=0;        int ud=1;        while(top<=h*w){            if(y%4==2&&x==h||y%4==0&&x==1){                st[++top]=node(x,y++);                st[++top]=node(x,y++);                ud^=1;                continue;            }            if(ud)st[++top]=node(x++,y);            else st[++top]=node(x--,y);            if(lr)st[++top]=node(x,y++);            else st[++top]=node(x,y--);            lr^=1;        }    }    int idx=2,sum=0;    rep(i,1,w*h){        c[idx].pb(node(st[i].x,st[i].y+h));        d[st[i].x][st[i].y+h]=idx;        sum++;        if(sum==idx){            idx+=2;            sum=0;        }    }}void draw2(int z){    rep(i,1,z){        int x=i,y=0;        rep(j,1,i){            c[i*2-1].pb(node(x,++y));            d[x][y]=i*2-1;        }        rep(j,1,i-1){            c[i*2-1].pb(node(--x,y));            d[x][y]=i*2-1;        }    }}void debug(int h,int w){    rep(i,1,h){        rep(j,1,w){            printf("%3d",d[i][j]);        }        printf("\n");    }}int main(){    while(scanf("%d",&n)!=EOF){        rep(i,1,n)c[i].clear();        if(n&1){            printf("%d %d\n",(n+1)/2,n);            draw((n+1)/2,n-(n+1)/2);            draw2(n/2+1);            //debug(n/2+1,n);        }        else{            printf("%d %d\n",n/2,n+1);            draw(n/2,n+1-n/2);            draw2(n/2);            //debug(n/2,n+1);        }        rep(i,1,n){            printf("%d %d",c[i][0].x,c[i][0].y);            rep(j,1,c[i].size()-1){                printf(" %d %d",c[i][j].x,c[i][j].y);            }            printf("\n");        }    }    return 0;}





0 0