zoukankan      html  css  js  c++  java
  • POJ 1099 Square Ice

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

    题目大意:模拟

    分析: 1.1个'O'连接2个'H',1个'H'只连接1个'O'

        2.初始化不可以用memset(map,0,sizeof(map)) 应该初始化为空格

        3.1对应两个'-',-1对应两个'|',0必须对应一个'-'和一个'|'

        4.模拟题目尽量少开flag标记,容易出错又不好检查出来,尽量按照正常思维模拟。

    代码如下:

     1 # include <iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 # include<cmath>
     5 using namespace std;
     6 char map[100][100];
     7 int s[15][15];
     8 
     9 bool judge(int i,int j)
    10 {
    11     if(map[i-1][j]=='|'||map[i+1][j]=='|'||map[i][j-1]=='-'||map[i][j+1]=='-')
    12         return false;
    13     return true;
    14 }
    15 
    16 int main()
    17 {
    18     int T,cas=1,i,j,n,a,b;
    19     while(scanf("%d",&n) && n)
    20     {
    21         for(i=0; i<n; i++)
    22             for(j=0; j<n; j++)
    23                 scanf("%d",&s[i][j]);
    24         int y = 4*n+3;
    25         int x = 4*n-1;
    26         for(i=0; i<=x; i++)
    27             for(j=0; j<=y; j++)
    28                 map[i][j] = ' ';
    29 
    30         for(i=0; i<x; i++)
    31             map[i][0] = map[i][y-1] = '*';
    32         for(j=0; j<y; j++)
    33             map[0][j] = map[x-1][j] = '*';
    34 
    35         for(i=1; i<=x-2; i+=4)
    36         {
    37             for(j=1; j<=y-2; j+=4)
    38                 map[i][j] = 'H';
    39             for(j=3; j<=y-2; j+=4)
    40                 map[i][j] = 'O';
    41         }
    42         for(i=3; i<=x-3; i+=4)
    43         {
    44             for(j=3; j<=y-4; j+=4)
    45                 map[i][j] = 'H';
    46         }
    47 
    48         for(i=0; i<n; i++)
    49         {
    50             a = i*4+1;
    51             for(j=0; j<n; j++)
    52             {
    53                 b= j*4+3;
    54                 if(s[i][j] == 1)
    55                 {
    56                     map[a][b-1] = '-';
    57                     map[a][b+1] = '-';
    58                 }
    59                 else  if(s[i][j] == -1)
    60                 {
    61                     map[a+1][b] = '|';
    62                     map[a-1][b] = '|';
    63                 }
    64             }
    65         }
    66 
    67         for(i=0; i<n; i++)
    68         {
    69             for(j=0; j<n; j++)
    70             {
    71                 if(s[i][j]==0)
    72                 {
    73                     a = i*4+1;
    74                     b= j*4+3;
    75                     if(judge(a,b-2))
    76                         map[a][b-1] = '-';
    77                     else
    78                         map[a][b+1] = '-';
    79                     if(a-2>0&&judge(a-2,b))
    80                         map[a-1][b] = '|';
    81                     else
    82                         map[a+1][b] = '|';
    83                 }
    84             }
    85         }
    86 
    87         printf("Case %d:
    
    ",cas++);
    88         for(i=0; i<x; i++)
    89         {
    90             for(j=0; j<y; j++)
    91             {
    92                 printf("%c",map[i][j]);
    93             }
    94             printf("
    ");
    95         }
    96         printf("
    ");
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    Windows下搭建JSP开发环境
    ssh 学习笔记
    18 11 27 长连接 短链接
    18 11 26 用多进程 多线程 携程 实现 http 服务器的创建
    18 11 24 简单的http服务器
    关于 某个智慧树课堂的 机器与机器交流方法
    18 11 23 正则学习
    尝试解决 : Microsoft Visual C++ 14.0 is required 的问题
    18 11 20 网络通信 ----多任务---- 携程 ----生成器
    18 11 20 网络通信 ----多任务---- 携程 ----迭代器
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3625354.html
Copyright © 2011-2022 走看看