poj1099
2016-05-17 21:35
190 查看
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.
![](http://poj.org/images/1099/1099_1.gif)
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:
![](http://poj.org/images/1099/1099_2.gif)
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
Sample Output
需要细心,对于数组和横纵向和x y 轴的转换有点不熟练啊
WA了几次就是自己在对于0 和o的区别在输出的时候搞错了,没有复制粘贴,导致wa了几次,我也是醉了
需要细心,对于数组和横纵向和x y 轴的转换有点不熟练啊
WA了几次就是自己在对于0 和o的区别在输出的时候搞错了,没有复制粘贴,导致wa了几次,我也是醉了
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4162 | Accepted: 1624 |
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.
![](http://poj.org/images/1099/1099_1.gif)
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:
![](http://poj.org/images/1099/1099_2.gif)
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* *******************
需要细心,对于数组和横纵向和x y 轴的转换有点不熟练啊
WA了几次就是自己在对于0 和o的区别在输出的时候搞错了,没有复制粘贴,导致wa了几次,我也是醉了
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n; char ans[100][100]; int a[20][20]; int coun=1; while(scanf("%d",&n),n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); memset(ans,' ',sizeof(ans)); //------------行---------------------------------// for(int i=0;i<=4*n+2;i++) ans[0][i]='*'; int t=4*n-2; for(int i=0;i<=4*n+2;i++) ans[t][i]='*'; //------------列---------------------------------// for(int j=1;j<=4*n-3;j++) ans[j][0]='*'; t=4*n+2; for(int j=1;j<=4*n-3;j++) ans[j][t]='*'; //---------------------------------------------// for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int temp_x=4*i-3; int temp_y=4*j-1; ans[temp_x][temp_y]='O'; if(a[i][j]==1){ ans[temp_x][temp_y-1]='-'; ans[temp_x][temp_y+1]='-'; ans[temp_x][temp_y-2]='H'; ans[temp_x][temp_y+2]='H'; } else if(a[i][j]==-1){ ans[temp_x+1][temp_y]='|'; ans[temp_x-1][temp_y]='|'; ans[temp_x+2][temp_y]='H'; ans[temp_x-2][temp_y]='H'; } else{ int sum_x=0,sum_y=0; for(int p=1;p<j;p++) sum_x+=a[i][p]; for(int k=1;k<i;k++) sum_y+=a[k][j]; if(sum_x==0&&sum_y==0){ ans[temp_x+1][temp_y]='|'; ans[temp_x][temp_y-1]='-'; ans[temp_x+2][temp_y]='H'; ans[temp_x][temp_y-2]='H'; } else if(sum_x==1&&sum_y==0){ ans[temp_x+1][temp_y]='|'; ans[temp_x][temp_y+1]='-'; ans[temp_x+2][temp_y]='H'; ans[temp_x][temp_y+2]='H'; } else if(sum_x==0&&sum_y==1){ ans[temp_x-1][temp_y]='|'; ans[temp_x][temp_y-1]='-'; ans[temp_x-2][temp_y]='H'; ans[temp_x][temp_y-2]='H'; } else{ ans[temp_x-1][temp_y]='|'; ans[temp_x][temp_y+1]='-'; ans[temp_x-2][temp_y]='H'; ans[temp_x][temp_y+2]='H'; } } } } if(coun>1) puts(""); printf("Case %d:\n\n",coun++); for(int i=0;i<=4*n-2;i++){ for(int j=0;j<=4*n+2;j++) printf("%c",ans[i][j]); puts(""); } } return 0; }
需要细心,对于数组和横纵向和x y 轴的转换有点不熟练啊
WA了几次就是自己在对于0 和o的区别在输出的时候搞错了,没有复制粘贴,导致wa了几次,我也是醉了
相关文章推荐
- 构建之法阅读笔记08
- #include”* .h“ 在查找预编译头使用时跳过
- hdu3308 LCIS(区间求和)
- HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash
- 链表指定区间元素的反转
- bzoj 1002: [FJOI2007]轮状病毒(打表找规律或基尔霍夫矩阵)
- 膨胀和腐蚀
- 面向接口编程以及面向接口编程的好处
- 排序算法:快速排序
- Spring解析
- Android开发中图标像素大小规范
- windows 8 wmware player centos 安装 伪分布式 hadoop
- 数字序列
- cccc PAT 1023组个最小数
- Spring的IOC的解析
- HashMap的默认容量和加载因子
- python笔记之ArcPy简介
- [Book Note] Haskell: Functor & Applicative
- 第一个程序HelloWord
- hdu4638 莫对算法 离线区间查询(非更新)