目录

题面

思路

思路

AC代码


题面

Square Ice

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4526   Accepted: 1759

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*
*******************

Source

East Central North America 2001

思路

没怎么看懂题意。但是知道了这么几个点:

1. 竖直的水分子不会出现在第一行和最后一行。

2. 给出的矩阵1,-1是交替出现的。

从样例输出观察出了输出行数,输出列数,H原子,O原子的规律。

不包括边界星号的话,行数是4*m-3,列数是4*m+1.

思路

先把星号,氧原子,氢原子放好,其余空格填充。

另外开一个数组该位置的原子已经连了几个键。

首先根据输入把水平水分子和竖直水分子的键连了,并且更新“连键”数组。

H原子分成两种类型。

一种是其键只能水平连的(横纵坐标都是模4余1),一种是其键只能竖直连的(横纵坐标都是模4余3)。

先只考虑水平型,我们连向与它相邻的O原子(必须空余键值为0,否则可能可能两个水平型H原子连到一个O原子变成一个水平水分子)。

按理来说,处理完完水平型的H原子,应该没有O原子剩余键数是2(否则它就只能通过两个竖直型H原子来连,那样就构成了竖直型水分子)。

然后考虑竖直型H原子,显然只能连到相邻的已经连了一个H原子的O原子上。

因为没怎么看懂题意,所以是连蒙带猜的,为了好检验我是否猜错了。我的代码中见了抛出异常。

如果我猜错了,那么就会抛出异常,评测状态是RE.

结果WA了好几次——因为Case,空行的格式

AC代码

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int kMaxSquareSize = 11;
char board[4*kMaxSquareSize-1][4*(kMaxSquareSize+1)];
int link_cnt[4*(kMaxSquareSize)+3][4*(kMaxSquareSize+1)]; int in[kMaxSquareSize][kMaxSquareSize]; inline bool is_border(int h,int w,int i,int j) {
return i == 0 || i+1 == h || j == 0 || j+1 == w;
} inline bool is_oxygen(int i,int j) {
return i%4 == 1 && j%4 == 3;
} inline bool is_hydrogen(int i,int j) {
return (i%4 == 3 && j%4 == 3) || (i%4 == 1 && j%4 == 1);
} int main()
{
int m;
memset(board,0,sizeof(board));
int id = 0;
while (scanf("%d",&m) && m) {
if (id)
printf("\n");
printf("Case %d:\n\n",++id);
memset(link_cnt,false,sizeof(link_cnt));
int h = m*4-1, w = m*4+3;
for (int i = 0;i < h; ++i)
for (int j = 0;j < w; ++j) {
if (is_border(h,w,i,j))
board[i][j] = '*';
else if (is_oxygen(i,j))
board[i][j] = 'O';
else if (is_hydrogen(i,j))
board[i][j] = 'H';
else
board[i][j] = ' ';
}
for (int i = 0;i < m; ++i)
for (int j = 0;j < m; ++j) {
scanf("%d",in[i]+j);
if (in[i][j] == 1) {
board[4*i+1][4*j+2] = '-'; link_cnt[4*i+1][4*j+1] = 1;
board[4*i+1][4*j+4] = '-'; link_cnt[4*i+1][4*j+5] = 1;
link_cnt[4*i+1][4*j+3] = 2;
} else if (in[i][j] == -1) {
board[4*i][4*j+3] = '|'; link_cnt[4*i-1][4*j+3] = 1;
board[4*i+2][4*j+3] = '|'; link_cnt[4*i+3][4*j+3] = 1;
link_cnt[4*i+1][4*j+3] = 2;
}
}
// 1型氢原子,水平短线连接
for (int i = 1;i < h; i += 4)
for (int j = 1;j < w; j += 4) {
if (link_cnt[i][j]) continue;
if ((j > 3) && (link_cnt[i][j-2] == 0)) {
board[i][j-1] = '-';
++link_cnt[i][j-2];
link_cnt[i][j] = 1;
} else if ((j + 2 < w) && (link_cnt[i][j+2] == 0)) {
board[i][j+1] = '-';
++link_cnt[i][j+2];
link_cnt[i][j] = 1;
} else {
//cout<<i<<","<<j<<endl;
throw 5.0;
}
}
// 2型 竖直线连接
for (int i = 3;i < h; i += 4)
for (int j = 3;j < w; j += 4) {
if (link_cnt[i][j]) continue;
if (link_cnt[i-2][j] == 1) {
board[i-1][j] = '|';
++link_cnt[i-2][j];
link_cnt[i][j] = 1;
} else if (link_cnt[i+2][j] == 1) {
board[i+1][j] = '|';
++link_cnt[i+2][j];
link_cnt[i][j]= 1;
} else {
//cout<<i<<","<<j<<endl;
throw "wrong";
}
}
for (int i = 0;i < h; ++i) {
board[i][w] = 0;
puts(board[i]);
}
}
return 0;
}

POJ 1099 Square Ice 连蒙带猜+根据样例找规律的更多相关文章

  1. POJ 1099 Square Ice

    Square Ice Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxyg ...

  2. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

  3. poj 1099

    http://poj.org/problem?id=1099 #include<stdio.h> #include<string.h> #include <iostrea ...

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

    Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxygen at the v ...

  5. DFS POJ 2362 Square

    题目传送门 /* DFS:问能否用小棍子组成一个正方形 剪枝有3:长的不灵活,先考虑:若根本构不成正方形,直接no:若第一根比边长长,no 这题是POJ_1011的精简版:) */ #include ...

  6. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. 【poj 1182】食物链(图论--带权并查集)

    题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...

  8. POJ 1740 A New Stone Game 又是博弈论配对找规律orz 博弈论 规律

    http://poj.org/problem?id=1740 这个博弈一眼看上去很厉害很高大上让人情不自禁觉得自己不会写,结果又是找规律…… 博弈一般后手胜都比较麻烦,但是主要就是找和先手的对应关系, ...

  9. 最简单的基于FFmpeg的移动端样例附件:Android 自带播放器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

随机推荐

  1. [jQuery]jQuery和DOM对象(三)

    iQuery和DOM对象 用原生js获取来的对象就是DOM对象 // 1. DOM对象 var myDiv = document.get.querySelector('div'); // myDiv ...

  2. JavaScript 箭头函数(Lambda表达式)

    Lambda表达式(箭头函数)用于表示一个函数,所以它和函数一样,也拥有参数.返回值.函数体,但它没有函数名,所以Lambda表达式相当于一个匿名函数. 使用方法: ()=>{} 小括号里放参数 ...

  3. Chrome Vue Devtools插件安装和使用

    安装:fq后在chrome应用商店搜索 Vue Devtools并安装,安装成功后浏览器右上角有vue的图标 安装完毕后,打开含有vue框架的网站,这是vue图标会变亮,进入开发者工具,再右侧vue选 ...

  4. 零基础学到什么程度可以找一份web前端工作?

    能找到一份前端开发工作,首先你起码得是一个合格的初级前端工程师.那么,什么是初级前端工程师?初级前端工程师都会做些什么?这个问题需要分为以下几个方面来说: 一.对应岗位的工作职责初级前端,主要负责产品 ...

  5. SAP MM 自定义条件类型出现在采购信息记录的'条件'界面里 ?

    SAP MM 自定义条件类型出现在采购信息记录的'条件'界面里 ? 我在SAP系统里复制某个标准的采购条件类型,创建了一个新的自定义条件类型ZC05,并将其分配采购定价过程RM0000. 结果却出现一 ...

  6. Android布局管理器-从实例入手学习相对布局管理器的使用

    场景 AndroidStudio跑起来第一个App时新手遇到的那些坑: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797 ...

  7. Qt编写的项目作品3-输入法V2018

    一.功能特点 未采用Qt系统层输入法框架,独创输入切换机制. 纯QWidget编写,支持任何目标平台(亲测windows.linux.嵌入式linux等),支持任意Qt版本(亲测Qt4.6.0到Qt5 ...

  8. (Hourglass)Windows倒计时软件 v1.9 电脑版

    (Hourglass)Windows倒计时软件是一款电脑系统小工具,能帮助大家快速进行最新的电脑系统倒计时设计,你可以设置自己的关机时间,帮助大家更好的管理自己的电脑应用. 链接:https://pa ...

  9. Django 表关系的创建

    Django 表关系的创建 我们知道,表关系分为一对多,多对多,一对一 我们以一个图书管理系统为背景,设计了下述四张表,让我们来找一找它们之间的关系 Book与Publish表 找关系:一对多 左表( ...

  10. input禁止输入的方法

    1: readonly规定输入字段为只读可复制,但是,用户可以使用Tab键切换到该字段,可选择,可以接收焦点,还可以选中或拷贝其文本. <input type="text" ...