[ACM_其他] Square Ice (poj1099 规律)

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*
*******************
Source
题目的大致意思就是给你一个矩阵,里面包含有1,-1,或者0,分别代表三种水分子的排列方式,现在要求根据矩阵来还原水分子的排列,并在周围加上一圈的“*”。仔细观察会发现其实H和O的分布是不变的,唯一不同的是氢氧建的位置,我首先把H和O的位置放好,然后分别根据横竖排列限制确定某些H(这里游离H用4表示,非游离的用5来表示),然后针对非竖非横的排列情况单独分析该氧元素周围4个氢原子的情况。(特别说明:这里用square[i][j]来标记(i,j)位置的对应符号,最后才把结果输出来)
#include<iostream>
#include<string.h>
using namespace std;
int n,ASM[][],square[][];//square[][]用来存放结果符号映射,其中set[square[i][j]]即为i,j位置的符号
char set[]={' ','-','|','O','H','H'};//4代表游离H,5代表固定H
bool Valid(int i,int j){return (<=i&&i<*n-&&<=j&&j<*n+);}
void output(){
for(int i=;i<*n+;i++) cout<<'*';cout<<'\n';//输出第一行的4n+3个*
for(int i=;i<*n-;i++){//中间图像
cout<<'*';
for(int j=;j<*n+;j++)cout<<set[square[i][j]];
cout<<'*'<<'\n';
}
for(int i=;i<*n+;i++) cout<<'*';cout<<'\n';//输出最后一行的4n+3个*
}
void solve(){
memset(square,,sizeof(square));
for(int i=;i<n;i++){
for(int j=;j<n;j++){
square[*i][*j+]=;square[*i][*j+]=;
if(j==) square[*i][*j]=;
if(i<n-) square[*i+][*j+]=; //氧氢位置是固定的,变化的是连线!!! if(ASM[i][j]==){//横着
square[*i][*j+]=square[*i][*j+]=;
square[*i][*j]=square[*i][*j+]=;
}
else if(ASM[i][j]==-){//竖着(不用考虑越界因为i不可能为0)
square[*i-][*j+]=square[*i+][*j+]=;
square[*i-][*j+]=square[*i+][*j+]=;
}
}
}
for(int i=;i<n;i++){//其他情况配对
for(int j=;j<n;j++){
if(ASM[i][j]==){//对i,j周围4个H进行判断,并固定其中游离的H
if(Valid(*i,*j)&&square[*i][*j]!=)
square[*i][*j]=, square[*i][*j+]=;
else if(Valid(*i,*j+)&&square[*i][*j+]!=)
square[*i][*j+]=, square[*i][*j+]=;
if(Valid(*i-,*j+)&&square[*i-][*j+]!=)
square[*i-][*j+]=, square[*i-][*j+]=;
else if(Valid(*i+,*j+)&&square[*i+][*j+]!=)
square[*i+][*j+]=, square[*i+][*j+]=;
}
}
}
}
int main(){
int casee=;
while(cin>>n && n>){
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>ASM[i][j];
cout<<(casee== ? "":"\n");
cout<<"Case "<<casee++<<":\n\n";
solve();
output();
}return ;
}
//poj1099
[ACM_其他] Square Ice (poj1099 规律)的更多相关文章
- POJ 1099 Square Ice 连蒙带猜+根据样例找规律
目录 题面 思路 思路 AC代码 题面 Square Ice Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4526 A ...
- POJ 1099 Square Ice
Square Ice Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxyg ...
- ACM_同余+暴力找规律
小光的忧伤 Time Limit: 2000/1000ms (Java/Others) Problem Description: 锴神:我尊重作者原意,你们出什么我就加什么.于是小光打了道水题(也就是 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- 狗狗40题~(Volume A)
A - The Willy Memorial Program 大模拟题…… 一开始的思路不对,修修补补WA了十发.当时想直接一个并查集做连通来搞定它,结果发现不能很好地判断各管的水位.究其原因还是因为 ...
- 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 ...
- [ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)
Description ycc 喜欢古典音乐是一个 ZJUTACM 集训队中大家都知道的事情.为了更方便地聆听音乐,最近 ycc 特意把他的家搬到了爱乐大街(德语Philharmoniker-Stra ...
随机推荐
- 分布式HBase-0.98.4环境搭建
fesh个人实践,欢迎经验交流!Blog地址:http://www.cnblogs.com/fesh/p/3804072.html 本文有点简单,详细版本请参见<分布式Hbase-0.98.4在 ...
- B站运维团队成长的血泪史
胡凯,bilibili运维负责人,曾经就职于金山软件.金山网络.猎豹移动,负责运维相关工作.Bilibili是国内最大的年轻人潮流文化娱乐社区,银河系知名弹幕视频分享UGC平台. 95后二次元新人 ...
- 绕过/*,web.xml直接访问jsp
web.xml中如果配置了/* 全匹配,那么不能用servet去响应页面返回了,因为全都被会/*拦截. <servlet> <servlet-name>validateAuth ...
- JAVA通过HTTP访问:Post+Get方式(转)
public class TestGetPost { /** * 向指定URL发送GET方法的请求 * @param url 发送请求的URL * @param param 请求参数,请求参数应该是n ...
- jquery 滚动到底部加载
var body_ = $(window); var indexPage = 2; var pageCount = <?php echo $pageCount;?>; var _ajaxR ...
- 验证码(网页的某些图片)在ie 360不显示,在火狐下显示正常
解决办法: 开始->运行,在运行输入框中输入“regsvr32 c:\windows\system32\pngfilt.dll”(不包含双引号),然后点击确定,如果在出现“已加载c:\windo ...
- 。net 添加或获取文件关联
文件关联设置 2011-02-07 14:25:36| 分类: VB.net2008或2010 | 标签:文件关联 |举报|字号 订阅 原理:以后缀名为.txt为例 方式一: 1.在注册 ...
- .net web获取自己的ip地址
using System;using System.Text;using System.Web;using System.Text.RegularExpressions; namespace MxWe ...
- linux 管道命令 竖线 ‘ | ’
管道符号,是unix功能强大的一个地方,符号是一条竖线:"|", 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为comm ...
- 再详细的介绍一下Unity5的AssetBundle
之前曾经写了一篇博客介绍Unity5的AssetBundle,结果似乎很受关注.不过似乎很多人看了之后都不懂,主要是因为不太明白AssetBundle是什么,它的依赖关系和结构是什么的,就直接想拿代码 ...