Description
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
Output
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标记,容易出错又不好检查出来,尽量按照正常思维模拟。
代码如下:
# include <iostream>
# include<cstdio>
# include<cstring>
# include<cmath>
using namespace std;
char map[][];
int s[][]; bool judge(int i,int j)
{
if(map[i-][j]=='|'||map[i+][j]=='|'||map[i][j-]=='-'||map[i][j+]=='-')
return false;
return true;
} int main()
{
int T,cas=,i,j,n,a,b;
while(scanf("%d",&n) && n)
{
for(i=; i<n; i++)
for(j=; j<n; j++)
scanf("%d",&s[i][j]);
int y = *n+;
int x = *n-;
for(i=; i<=x; i++)
for(j=; j<=y; j++)
map[i][j] = ' '; for(i=; i<x; i++)
map[i][] = map[i][y-] = '*';
for(j=; j<y; j++)
map[][j] = map[x-][j] = '*'; for(i=; i<=x-; i+=)
{
for(j=; j<=y-; j+=)
map[i][j] = 'H';
for(j=; j<=y-; j+=)
map[i][j] = 'O';
}
for(i=; i<=x-; i+=)
{
for(j=; j<=y-; j+=)
map[i][j] = 'H';
} for(i=; i<n; i++)
{
a = i*+;
for(j=; j<n; j++)
{
b= j*+;
if(s[i][j] == )
{
map[a][b-] = '-';
map[a][b+] = '-';
}
else if(s[i][j] == -)
{
map[a+][b] = '|';
map[a-][b] = '|';
}
}
} for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
if(s[i][j]==)
{
a = i*+;
b= j*+;
if(judge(a,b-))
map[a][b-] = '-';
else
map[a][b+] = '-';
if(a->&&judge(a-,b))
map[a-][b] = '|';
else
map[a+][b] = '|';
}
}
} printf("Case %d:\n\n",cas++);
for(i=; i<x; i++)
{
for(j=; j<y; j++)
{
printf("%c",map[i][j]);
}
printf("\n");
}
printf("\n");
}
return ;
}