POJ 1099 Square Ice

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.  

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*
*******************
看似是一道相当繁琐的模拟题,实际上要注意
H和O的排布是有规律的,可以先填好
然后再确定|和-的位置
当然请注意格式
 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int m,a[12][12],cnt=0;
    char s[100][100];
    int flag[100][100];
    while(cin>>m&&m)
    {
        memset(s,0,sizeof(s));
        memset(flag,0,sizeof(flag));
        //依照规律,将区域初始化
        for(int i=0;i<4*m+3;++i)
        {
            s[0][i]='*';
            s[4*m-2][i]='*';
        }
        for(int i=0;i<4*m-1;++i)
        {
            s[i][0]='*';
            s[i][4*m+2]='*';
        }
        /*for(int i=0;i<4*m+3;++i)
            for(int j=0;j<4*m+3;++j)
        {
            cout<<s[i][j];
            if(j==4*m+2) cout<<endl;
        }*/
        for(int i=1;i<=4*m-3;i=i+4)
            for(int j=1;j<=4*m+1;++j)
        {
            if(j%4==1) s[i][j]='H';
            if(j%4==2||j%4==0) s[i][j]=' ';
            if(j%4==3) s[i][j]='O';
        }
        for(int i=3;i<=4*m-5;i=i+4)
            for(int j=1;j<=4*m+1;++j)
        {
            if(j%4==3) s[i][j]='H';
            else s[i][j]=' ';
        }
        for(int i=2;i<=4*m-4;i=i+2)
            for(int j=1;j<=4*m+1;++j)
        {
            s[i][j]=' ';
        }
        /*for(int i=0;i<4*m+3;++i)
            for(int j=0;j<4*m+3;++j)
        {
            cout<<s[i][j];
            if(j==4*m+2) cout<<endl;
        }*/
        for(int i=0;i<m;++i)
            for(int j=0;j<m;++j)
        {
            cin>>a[i][j];
        }
        //将易确定的1和-1先确定下来
        for(int i=0;i<m;++i)
            for(int j=0;j<m;++j)
        {
            if(a[i][j]==1)
            {
                flag[4*i+1][4*j+3]=1;
                flag[4*i+1][4*j+1]=1;
                flag[4*i+1][4*j+5]=1;
                s[4*i+1][4*j+2]='-';
                s[4*i+1][4*j+4]='-';
            }
            else if(a[i][j]==-1)
            {
                flag[4*i+1][4*j+3]=1;
                flag[4*i-1][4*j+3]=1;
                flag[4*i+3][4*j+3]=1;
                s[4*i][4*j+3]='|';
                s[4*i+2][4*j+3]='|';
            }
        }
        for(int i=0;i<m;++i)
            for(int j=0;j<m;++j)
        {
            if(a[i][j]==0)
            {
                if(flag[4*i+1][4*j+1]==1)
                {
                    s[4*i+1][4*j+4]='-';
                    flag[4*i+1][4*j+5]=1;
                }
                else
                {
                    flag[4*i+1][4*j+1]=1;
                    s[4*i+1][4*j+2]='-';
                }
                if(4*i-1>0&&flag[4*i-1][4*j+3]!=1)
                {
                    flag[4*i-1][4*j+3]=1;
                    s[4*i][4*j+3]='|';
                }
                else if(4*i+3<4*m-2&&flag[4*i+3][4*j+3]!=1)
                {
                    flag[4*i+3][4*j+3]=1;
                    s[4*i+2][4*j+3]='|';
                }
            }
        }
        cout<<"Case "<<++cnt<<":"<<endl<<endl;
        for(int i=0;i<4*m-1;++i)
            for(int j=0;j<4*m+3;++j)
        {
            cout<<s[i][j];
            if(j==4*m+2) cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值