2015ACM-ICPC 北京赛区 Problem I. Snake Carpet

来源:互联网 发布:大学生兼职数据调查 编辑:程序博客网 时间:2024/05/16 07:58
Description 
  In school of EECS of Peking University, there is a homework for all freshman -- the contest of AI snakes. This contest is ended today. Bacchus has got a very good result, so he decides 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×W units(each unit is 1×1). Snakes can have different length, but all snakes' width is 1 unit. For some reason, He hopes that N special snakes are drawn on the carpet: the length of the ith snake should be i, which can be seen as i connected units(Two units that share an edge are considered connected). Except the first snake, the (2k−1)th snake should have positive odd number of turning points; except the second snake, the 2kth snake should have an positive even number of turning points. i and k both start from 1. Each snake should not intersect with itself, nor with other snakes. All units of the carpet must be covered by snakes.  But the question is whether there is a solution. 
Input 
   Multiple test cases. There will be up to 25 cases.  
  For each test case: one line contains one integer N, indicating the number of snakes. (1≤N≤500) 
Output 
  For each test case: 
  If the solution does not exist, output one line "0 0", otherwise output N+1 lines: The first line contains two integers H and W, indicating the height and the width of the carpet. You should guarantee that H×W=1+2+⋯+N. For the next N lines, the ith line contain 2i integers, indicating the coordinates of the ith snake in order. The coordinate of top-left corner unit is (1,1) and the coordinate of bottom-right corner unit is (H,W) 
Sample Input 
3  4  5   
Sample Output 
2 3  1 2  
1 3 2 3  
1 1 2 1 2 2  2 5  1 4  
1 5 2 5  
1 1 2 1 2 2  
1 2 1 3 2 3 2 4  3 5  3 4  
1 4 1 5  
2 4 2 5 3 5  
2 2 2 3 3 3 3 2  3 1 2 1 1 1 1 2 1 3 
Hint 

This problem is special judged, and the solutions for the sample input are: 



题目大意:用1 2 3 4覆盖出一个矩形面积。对于i,要求放置i个。若i为偶数,则应有偶数个转弯;若i为奇数,则应有奇数个转弯。

让你构造出一种覆盖方法

若m为奇数。则构造下图逐层递归。其中n=m-1;到m=5的时候直接构造题目样例

n n

n n

n n

n m

n m m m m m m m m

若m为偶数

则将m放在最左边放两排即可

 
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct save{int x,y;}a[501][501];//int map[501][501];inline void solve(int h,int w,int n,int x,int y){if(n<=5){int xx=x,yy=y;a[5][1].x=xx+2;a[5][1].y=yy;a[5][2].x=xx+1;a[5][2].y=yy;a[5][3].x=xx;a[5][3].y=yy;a[5][4].x=xx;a[5][4].y=yy+1;a[5][5].x=xx;a[5][5].y=yy+2;a[4][1].x=xx+2;a[4][1].y=yy+1;a[4][2].x=xx+1;a[4][2].y=yy+1;a[4][3].x=xx+1;a[4][3].y=yy+2;a[4][4].x=xx+2;a[4][4].y=yy+2;a[3][1].x=xx;a[3][1].y=yy+3;a[3][2].x=xx;a[3][2].y=yy+4;a[3][3].x=xx+1;a[3][3].y=yy+4;a[2][1].x=xx+1;a[2][1].y=yy+3;a[2][2].x=xx+2;a[2][2].y=yy+3;a[1][1].x=xx+2;a[1][1].y=yy+4;return ;}int xx=x+h-1,yy=y;int p=0;while(xx>0){p++;a[n-1][p].x=xx;a[n-1][p].y=yy;xx--;}xx++;yy++;p++;a[n-1][p].x=xx;a[n-1][p].y=yy;while(p<n-1){p++;xx++;a[n-1][p].x=xx;a[n-1][p].y=yy;}xx++;a[n][1].x=xx;a[n][1].y=yy;xx++;int i;for(i=yy;i<=yy+w-1;i++){a[n][i-yy+2].x=xx;a[n][i-yy+2].y=i;}solve(h-1,w-2,n-2,x,y+2);}int main(){//freopen("I.in","r",stdin);//freopen("I.out","w",stdout);int n;while(scanf("%d",&n)!=EOF){int i,j;if(n<=4){if(n==1){printf("1 1\n");printf("1 1\n");}if(n==2){printf("1 3\n");printf("1 1\n1 2 1 3\n");}if(n==3){printf("2 3\n");printf("1 1\n2 1 2 2\n1 2 1 3 2 3\n");}if(n==4){printf("2 5\n");printf("1 4\n1 5 2 5\n1 1 2 1 2 2\n1 2 1 3 2 3 2 4\n");}}else if(n%2==1){solve(n/2+1,n,n,1,1);printf("%d %d\n",n/2+1,n);for(i=1;i<=n;i++){for(j=1;j<=i-1;j++)printf("%d %d ",a[i][j].x,a[i][j].y);printf("%d %d\n",a[i][j].x,a[i][j].y);}}else{solve(n/2,n-1,n-1,1,3);printf("%d %d\n",n/2,n+1);int p=0;for(i=n/2;i>=1;i--){p++;a[n][p].x=i;a[n][p].y=1;}for(i=1;i<=n/2;i++){p++;a[n][p].x=i;a[n][p].y=2;}for(i=1;i<=n;i++){for(j=1;j<=i-1;j++)printf("%d %d ",a[i][j].x,a[i][j].y);printf("%d %d\n",a[i][j].x,a[i][j].y);}}/*memset(map,0,sizeof(map));for(i=1;i<=n;i++)for(j=1;j<=i;j++)map[a[i][j].x][a[i][j].y]=i;for(i=1;i<=n/2+1;i++){for(j=1;j<=n+1;j++){if(map[i][j]<10)printf(" ");printf("%d ",map[i][j]);}printf("%d\n",map[i][j]);}*/}return 0;}


 
0 0
原创粉丝点击