ZOJ 1066(POJ 1099) Square Ice(…
来源:互联网 发布:天涯明月刀巅峰数据 编辑:程序博客网 时间:2024/06/06 00:46
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.
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:
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了两次……
言归正传:题目的大致意思就是给你一个矩阵,里面包含有1,-1,或者0,分别代表三种水分子的排列方式,现在要求根据矩阵来还原水分子的排列,并在周围加上一圈的“*”(是不是因为这样比较好看一点儿呢)
这题我记得原来第一次做的时候是想着一边输出,一边判断,后来发现这个方法很弱智,因为每一个分子的前后左右是紧密相关的,所以就开辟了一个二维数组来存储这个分子矩阵,这样的话可以不受输出的限制,位置可以随便进行赋值,首先把二维数组进行初始化,每个元素都赋值为空格(因为最后剩余的位置都是用空格代替的),把O原子的位置和H原子的位置先写上(O:4*i-1
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;}
- ZOJ 1066(POJ 1099) Square Ice(…
- ZOJ 1060 (POJ 1094) Sorting It …
- ZOJ 1504(POJ 1244) Slots of Fun…
- ZOJ 1041(POJ 1106) Transmitters(…
- HDOJ 1398 Square Coins
- POJ 1005 I Think I Need a Houseb…
- ZZULI_TEAM_PRACTICE(1) POJ 1099…
- poj 2488
- ZZULI_SummerPractice(3) POJ 12…
- POJ 2594 Treasure Exploration(…
- poj 2421 Constructing Roads mst(…
- poj 1135 Domino Effect 单源最短…
- poj 1980 Unit Fraction Partition…
- poj 3280 Cheapest Palindrome dp(…
- poj 2239 Selecting Courses (最大…
- poj 3160 Father Christmas flymou…
- poj 3177 Redundant Paths (双连…
- poj 2455 Secret Milking Machine…
- 程序员的爱情 -经典啊!!
- 七种qsort排序方法
- Spring注解@Component、@Repository、@Service、@Controller
- 交流与投票:本科毕业是先考…
- Android布局学习四:layout_weight 的使用
- ZOJ 1066(POJ 1099) Square Ice(…
- GNU ARM 汇编指令
- ZOJ 1048 Gridland(水水更健康!*…
- ZOJ 1048 Financial Management(…
- 百度招聘介绍(看来计算机之路任重…
- 最失败的面试经历 -- 哥就是…
- ACM大牛!!!!!!
- ZZULI 1595 Pills
- ZZULI 1596 Assign the task(暴力…