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

  1. 2
  2. 0 1
  3. 1 0
  4. 4
  5. 0 1 0 0
  6. 1 -1 0 1
  7. 0 0 1 0
  8. 0 1 0 0
  9. 0

Sample Output

  1. Case 1:
  2.  
  3. ***********
  4. *H-O H-O-H*
  5. * | *
  6. * H H *
  7. * | *
  8. *H-O-H O-H*
  9. ***********
  10.  
  11. Case 2:
  12.  
  13. *******************
  14. *H-O H-O-H O-H O-H*
  15. * | | | *
  16. * H H H H *
  17. * | *
  18. *H-O-H O H-O H-O-H*
  19. * | | *
  20. * H H H H *
  21. * | | *
  22. *H-O H-O H-O-H O-H*
  23. * | *
  24. * H H H H *
  25. * | | | *
  26. *H-O H-O-H O-H O-H*
  27. *******************

Source

 

题目的大致意思就是给你一个矩阵,里面包含有1,-1,或者0,分别代表三种水分子的排列方式,现在要求根据矩阵来还原水分子的排列,并在周围加上一圈的“*”。仔细观察会发现其实H和O的分布是不变的,唯一不同的是氢氧建的位置,我首先把H和O的位置放好,然后分别根据横竖排列限制确定某些H(这里游离H用4表示,非游离的用5来表示),然后针对非竖非横的排列情况单独分析该氧元素周围4个氢原子的情况。(特别说明:这里用square[i][j]来标记(i,j)位置的对应符号,最后才把结果输出来)

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int n,ASM[][],square[][];//square[][]用来存放结果符号映射,其中set[square[i][j]]即为i,j位置的符号
  5. char set[]={' ','-','|','O','H','H'};//4代表游离H,5代表固定H
  6. bool Valid(int i,int j){return (<=i&&i<*n-&&<=j&&j<*n+);}
  7. void output(){
  8. for(int i=;i<*n+;i++) cout<<'*';cout<<'\n';//输出第一行的4n+3个*
  9. for(int i=;i<*n-;i++){//中间图像
  10. cout<<'*';
  11. for(int j=;j<*n+;j++)cout<<set[square[i][j]];
  12. cout<<'*'<<'\n';
  13. }
  14. for(int i=;i<*n+;i++) cout<<'*';cout<<'\n';//输出最后一行的4n+3个*
  15. }
  16. void solve(){
  17. memset(square,,sizeof(square));
  18. for(int i=;i<n;i++){
  19. for(int j=;j<n;j++){
  20. square[*i][*j+]=;square[*i][*j+]=;
  21. if(j==) square[*i][*j]=;
  22. if(i<n-) square[*i+][*j+]=; //氧氢位置是固定的,变化的是连线!!!
  23.  
  24. if(ASM[i][j]==){//横着
  25. square[*i][*j+]=square[*i][*j+]=;
  26. square[*i][*j]=square[*i][*j+]=;
  27. }
  28. else if(ASM[i][j]==-){//竖着(不用考虑越界因为i不可能为0)
  29. square[*i-][*j+]=square[*i+][*j+]=;
  30. square[*i-][*j+]=square[*i+][*j+]=;
  31. }
  32. }
  33. }
  34. for(int i=;i<n;i++){//其他情况配对
  35. for(int j=;j<n;j++){
  36. if(ASM[i][j]==){//对i,j周围4个H进行判断,并固定其中游离的H
  37. if(Valid(*i,*j)&&square[*i][*j]!=)
  38. square[*i][*j]=, square[*i][*j+]=;
  39. else if(Valid(*i,*j+)&&square[*i][*j+]!=)
  40. square[*i][*j+]=, square[*i][*j+]=;
  41. if(Valid(*i-,*j+)&&square[*i-][*j+]!=)
  42. square[*i-][*j+]=, square[*i-][*j+]=;
  43. else if(Valid(*i+,*j+)&&square[*i+][*j+]!=)
  44. square[*i+][*j+]=, square[*i+][*j+]=;
  45. }
  46. }
  47. }
  48. }
  49. int main(){
  50. int casee=;
  51. while(cin>>n && n>){
  52. for(int i=;i<n;i++)
  53. for(int j=;j<n;j++)
  54. cin>>ASM[i][j];
  55. cout<<(casee== ? "":"\n");
  56. cout<<"Case "<<casee++<<":\n\n";
  57. solve();
  58. output();
  59. }return ;
  60. }
  61. //poj1099

[ACM_其他] Square Ice (poj1099 规律)的更多相关文章

  1. POJ 1099 Square Ice 连蒙带猜+根据样例找规律

    目录 题面 思路 思路 AC代码 题面 Square Ice Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4526   A ...

  2. POJ 1099 Square Ice

    Square Ice Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxyg ...

  3. ACM_同余+暴力找规律

    小光的忧伤 Time Limit: 2000/1000ms (Java/Others) Problem Description: 锴神:我尊重作者原意,你们出什么我就加什么.于是小光打了道水题(也就是 ...

  4. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  5. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  6. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  7. 狗狗40题~(Volume A)

    A - The Willy Memorial Program 大模拟题…… 一开始的思路不对,修修补补WA了十发.当时想直接一个并查集做连通来搞定它,结果发现不能很好地判断各管的水位.究其原因还是因为 ...

  8. Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))

    C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. [ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)

    Description ycc 喜欢古典音乐是一个 ZJUTACM 集训队中大家都知道的事情.为了更方便地聆听音乐,最近 ycc 特意把他的家搬到了爱乐大街(德语Philharmoniker-Stra ...

随机推荐

  1. 开源GIS简介.学习

    开发者都希望自己的软件能够运行在尽可能多的计算机上.然而事与愿违,摆在 GIS开发者面前的仍然是对峙的平台.J2EE随着Java5.0的发布,已经正式更名为JavaEE,而微软也正式发布了.NET2. ...

  2. VC++中操作XML(MFC、SDK)转

    [转]VC++中操作XML(MFC.SDK) XML在Win32程序方面应该没有在Web方面应用得多,很多Win32程序也只是用XML来存存配置信息而已,而且没有足够的好处的话还不如用ini.VC++ ...

  3. Installshield 打包安装包心得

     制作简单的安装软件 声明:下面的教程,是把读者当做完全没接触过IS的角度来制作的. 1. 启动InstallShield 12.建立一个InstallShield MSI Project,如图: 2 ...

  4. vs2015连接oracle 11g(.net自带方式 using System.Data.OracleClient;)

    1,添加引用 System.Data.OracleClient 2,连接语句 string connectionString; string queryString; connectionString ...

  5. JavaScript初学者应注意的七个细节(转)

    http://www.cnblogs.com/lhb25/archive/2011/01/10/1932284.html 每种语言都有它特别的地方,对于JavaScript来说,使用var就可以声明任 ...

  6. MySQL高可用方案选型参考

    http://imysql.com/2015/09/14/solutions-of-mysql-ha.shtml#rd?sukey=b0cb5c5b9e501303123bd48e86555c35e0 ...

  7. laravel遇到的问题

    这是由于访问laravel项目报错的,解决几种可能出现的错误. 1)打开:D:\java\wamp\www\subway\app\config\app.php 修改:'debug' => tru ...

  8. linux系统man命令用法和安装方法

    Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可. Linux的man手册共有以下几个章节: 代號 代表內容 1 使用者在shell中可以操作的指令或可 ...

  9. reason: '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance

    类型的不匹配,把类型转化对应的数据类型,例: model.price 是模型数据,其值为1550: cell.label.text = [NSString stringWithFormat:@&quo ...

  10. LoadRunner录制不弹出IE浏览器,event=0

    系统环境:win7,安装了IE.360.firefox.搜狗高速浏览器: 起初刚装完loadrunner11的时候,选了IE进行录制,就是不能打开IE,算了,那就用loadrunner默认的设置,居然 ...