正文

ACM题,英文的2007-04-24 21:27:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/25225.html

分享到:

Square Ice
Time Limit:1000MS  Memory Limit:10000K
Total Submit:615 Accepted:228

Description
Square Ice is a two-dimensional arrangement of water molecules H2O, with oxygen at the vertices of a square lattice and one hydrogen atom between each pair of adjacent oxygen atoms. The hydrogen atoms must stick out on the left and right sides but are not allowed to stick out the top or bottom. One 5 x 5 example is shown below.

H-O H-O-H O-H O-H
  |       |   | 
  H   H   H   H 
      |         
H-O-H O H-O H-O-H
      |   |     
  H   H   H   H 
  |           | 
H-O H-O H-O-H O-H
      |         
  H   H   H   H 
  |       |   | 
H-O H-O-H O-H O-H

Note that each hydrogen atom is attached to exactly one of its neighboring oxygen atoms and each oxygen atom is attached to two of its neighboring hydrogen atoms. (Recall that one water molecule is a unit of one O linked to two H's.)

It turns out we can encode a square ice pattern with what is known as an alternating sign matrix (ASM): horizontal molecules are encoded as 1, vertical molecules are encoded as -1 and all other molecules are encoded as 0. So, the above pattern would be encoded as:

 

An ASM is a square matrix with entries 0, 1 and -1, where the sum of each row and column is 1 and the non-zero entries in each row and in each column must alternate in sign. (It turns out there is a one-to-one correspondence between ASM's and square ice patterns!)

Your job is to display the square ice pattern, in the same format as the example above, for a given ASM. Use dashes (-) for horizontal attachments and vertical bars (|) for vertical attachments. The pattern should be surrounded with a border of asterisks (*), be left justified and there should be exactly one character between neighboring hydrogen atoms (H) and oxygen atoms (O): either a space, a dash or a vertical bar.

 

Input
Input consists of multiple cases. Each case consists of a positive integer m (<= 11) on a line followed by m lines giving the entries of an ASM. Each line gives a row of the ASM with entries separated by a single space. The end of input is indicated by a line containing m = 0.

Output
For each case, print the case number (starting from 1), in the format shown in the Sample Output, followed by a blank line, followed by the corresponding square ice pattern in the format described above. Separate the output of different cases by a blank line.

Sample Input


2
0 1
1 0
4
0 1 0 0
1 -1 0 1
0 0 1 0
0 1 0 0
0

Sample Output


Case 1:

***********
*H-O H-O-H*
*  |      *
*  H   H  *
*      |  *
*H-O-H O-H*
***********

Case 2:

*******************
*H-O H-O-H O-H O-H*
*  |       |   |  *
*  H   H   H   H  *
*      |          *
*H-O-H O H-O H-O-H*
*      |   |      *
*  H   H   H   H  *
*  |           |  *
*H-O H-O H-O-H O-H*
*      |          *
*  H   H   H   H  *
*  |       |   |  *
*H-O H-O-H O-H O-H*
*******************

Source
East Central North America 2001

 

#include <stdio.h>
#include <memory.h>
#include <string.h>

int main(){
 char cStr[60][60];
 int t,m,i,j,is,js,num = 0;

 while(1){
  num ++;
  memset(cStr,' ',3600);
  for(i = 0; i < 60; i ++)
   cStr[i][0] = cStr[0][i] = '*';
  scanf("%d",&m);
  if( !m )
   break;
  for(i = 0,is = 2; i < m; i ++){
   for(j = 0,js = 3; j < m; j ++){
    scanf("%d",&t);
    cStr[is][js] = 'O';
    if(t == -1){
     cStr[is-1][js] = '|';
     cStr[is+1][js] = '|';
     cStr[is-2][js] = 'H';
     cStr[is+2][js] = 'H';
    }
    else if(t == 1){
     cStr[is][js-1] = '-';
     cStr[is][js+1] = '-';
     cStr[is][js-2] = 'H';
     cStr[is][js+2] = 'H';
    }
    else {
     if(cStr[is][js-2] == ' '){
      cStr[is][js-2] = 'H';
      cStr[is][js-1] = '-';
     }
     else{
      cStr[is][js+2] = 'H';
      cStr[is][js+1] = '-';
     }
     if(cStr[is-2][js] == ' '){
      cStr[is-2][js] = 'H';
      cStr[is-1][js] = '|';
     }
     else{
      cStr[is+2][js] = 'H';
      cStr[is+1][js] = '|';
     }
    }
    js += 4;
   }
   is += 4;
  }
  printf("Case %d:\n\n",num);
  m = is + 1;
  for(j = 0,is -= 3,js -= 1; j < m; j ++)
   printf("*");
  printf("\n");
  for(i = 2; i < is; i ++){
   for(j = 0; j < js; j ++)
    printf("%c",cStr[i][j]);
   printf("*\n");
  }
  for(j = 0; j < m; j ++)
   printf("*");
  printf("\n\n");
 }
 return 0;
}
  


 

阅读(2820) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册