ZOJ 1066(POJ 1099) Square Ice(…

来源:互联网 发布:天涯明月刀巅峰数据 编辑:程序博客网 时间:2024/06/06 00:46
Square Ice

Time Limit: 2 Seconds    Memory Limit: 65536 KB

Square Ice is a two-dimensional arrangement of water moleculesH2O, with oxygen at the vertices of a square lattice and onehydrogen atom between each pair of adjacent oxygen atoms. Thehydrogen atoms must stick out on the left and right sides but arenot allowed to stick out the top or bottom. One 5 x 5 example isshown below.

ZOJ <wbr>1066(POJ <wbr>1099) <wbr>Square <wbr>Ice(模拟题)

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

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

ZOJ <wbr>1066(POJ <wbr>1099) <wbr>Square <wbr>Ice(模拟题)

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

Your job is to display the square ice pattern, in the sameformat as the example above, for a given ASM. Use dashes (-) forhorizontal attachments and vertical bars (|) for verticalattachments. The pattern should be surrounded with a border ofasterisks (*), be left justified and there should be exactly onecharacter 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 apositive integer m (<= 11) on a line followed by mlines giving the entries of an ASM. Each line gives a row of theASM with entries separated by a single space. The end of input isindicated by a line containing m = 0.

Output

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

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-O-H O-H*
***********

Case 2:

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


Source: East Central North America2001

 

这道题原来做过,只是忘记上次到底做出来没有,所以感觉读题还是比较轻松的,思路也比较流畅,但是却在阴沟里翻了船,无语啊,没仔细读题,PE了两次……ZOJ <wbr>1066(POJ <wbr>1099) <wbr>Square <wbr>Ice(模拟题)ZOJ <wbr>1066(POJ <wbr>1099) <wbr>Square <wbr>Ice(模拟题)

 

言归正传:题目的大致意思就是给你一个矩阵,里面包含有1,-1,或者0,分别代表三种水分子的排列方式,现在要求根据矩阵来还原水分子的排列,并在周围加上一圈的“*”(是不是因为这样比较好看一点儿呢ZOJ <wbr>1066(POJ <wbr>1099) <wbr>Square <wbr>Ice(模拟题)

这题我记得原来第一次做的时候是想着一边输出,一边判断,后来发现这个方法很弱智,因为每一个分子的前后左右是紧密相关的,所以就开辟了一个二维数组来存储这个分子矩阵,这样的话可以不受输出的限制,位置可以随便进行赋值,首先把二维数组进行初始化,每个元素都赋值为空格(因为最后剩余的位置都是用空格代替的),把O原子的位置和H原子的位置先写上(O:4*i-1  H:4*i-3),然后判断如果是1的话直接把O原子的左右两端连接起来就行了。要是-1的话把O原子的上下两端连起来,同时把上下两组的H原子写上。这样基本上就可以了

PS:每个O原子和H原子的位置都是有规律的,找好规律这题就不难做

 

 

附代码:

 

#include<stdio.h>char str[50][50];void up(int i,int j){        str[4*i-4][4*j-1]='|';        str[4*i-5][4*j-1]='H';}void down(int i,int j){        str[4*i-2][4*j-1]='|';        str[4*i-1][4*j-1]='H';}int main(){        int i,j,Case=0,n,flag[15][15];        while(scanf("%d",&n),n!=0)        {                for(i=0;i<50;i++)                        for(j=0;j<50;j++)                                str[i][j]=' ';                for(i=1;i<=n;i++)                        for(j=1;j<=n;j++)                        {                                scanf("%d",&flag[i][j]);                                str[4*i-3][4*j-1]='O';                                str[4*i-3][4*j-3]='H';                                if(j==n)str[4*i-3][4*j+1]='H';                        }                for(i=1;i<=n;i++)                        for(j=1;j<=n;j++)                        {                                if(flag[i][j]==1)                                        str[4*i-3][4*j-2]=str[4*i-3][4*j]='-';                                else if(flag[i][j]==-1)                                {                                        str[4*i-2][4*j-1]=str[4*i-4][4*j-1]='|';                                        str[4*i-1][4*j-1]=str[4*i-5][4*j-1]='H';                                }                                else{                                        if(i==1){                                                down(i,j);                                        }                                        else if(i==n){                                                up(i,j);                                        }                                        else{                                                if(str[4*i-5][4*j-1]==' ')                                                        up(i,j);                                                else down(i,j);                                        }                                        if(j==1)str[4*i-3][4*j-2]='-';                                        else if(j==n)str[4*i-3][4*j]='-';                                        else{                                                if(str[4*i-3][4*j-4]=='-')                                                        str[4*i-3][4*j]='-';                                                else str[4*i-3][4*j-2]='-';                                        }                                }                        }                        Case++;                        if(Case>1)puts("");                        printf("Case %d:\n\n",Case);                        for(i=1;i<=4*n+3;i++)                                printf("*");                        printf("\n");                        for(i=1;i<=4*n-3;i++)                        {                                printf("*");                                for(j=1;j<=4*n+1;j++)                                        printf("%c",str[i][j]);                                printf("*\n");                        }                        for(i=1;i<=4*n+3;i++)                                printf("*");                        printf("\n");        }        return 0;}
原创粉丝点击