您的位置:首页 > 其它

poj1099

2016-05-17 21:35 190 查看
Square Ice

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 4162Accepted: 1624
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*
*******************


需要细心,对于数组和横纵向和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了几次,我也是醉了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: