[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

A children’s board game consists of a square array of dots that contains lines connecting some of the
pairs of adjacent dots. One part of the game requires that the players count the number of squares of
certain sizes that are formed by these lines. For example, in the figure shown below, there are 3 squares
— 2 of size 1 and 1 of size 2. (The “size” of a square is the number of lines segments required to form
a side.)
Your problem is to write a program that automates the process of counting all the possible squares.
Input
The input file represents a series of game boards. Each board consists of a description of a square array
of n
2 dots (where 2 ≤ n ≤ 9) and some interconnecting horizontal and vertical lines. A record for a
single board with n
2 dots and m interconnecting lines is formatted as follows:
Line 1: n the number of dots in a single row or column of the array
Line 2: m the number of interconnecting lines
Each of the next m lines are of one of two types:
H i j indicates a horizontal line in row i which connects
the dot in column j to the one to its right in column j + 1
or
V i j indicates a vertical line in column i which connects
the dot in row j to the one below in row j + 1
Information for each line begins in column 1. The end of input is indicated by end-of-file. The first
record of the sample input below represents the board of the square above.
Output
For each record, label the corresponding output with ‘Problem #1’, ‘Problem #2’, and so forth. Output
for a record consists of the number of squares of each size on the board, from the smallest to the largest.
lf no squares of any size exist, your program should print an appropriate message indicating so. Separate
output for successive input records by a line of asterisks between two blank lines, like in the sample
below.
Sample Input
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
Sample Output
Problem #1
2 square (s) of size 1
1 square (s) of size 2
‘**********************************’(两个单引号是我加的,因为我用的是MarkDown编辑器。。。)
Problem #2
No completed squares can be found.
题意:有n行n列(2<=n<=9)的点,还有m条线段连接其中的一些点。统计这些线段练成了多少个正方形。

解题思路:将读进来的要添加的边分别用两个bool数组存,添加后就将相应的位置赋值成true,然后枚举正方形的边长和左上角顶点的位置,统计答案。

代码

#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int ans[20];
bool a[20][20],b[20][20];
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main(){
    while(scanf("%d%d\n",&n,&m)!=EOF){
        cnt++;
        if(cnt!=1)puts("\n**********************************\n");
        memset(ans,0,sizeof(ans));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=1;i<=m;i++){
            char ch=getchar();int x=read(),y=read();
            if(ch=='H')a[x][y]=true;
            else b[y][x]=true;//这个lrj的紫书上的描述应该是b[x][y],但是原题上的描述是b[y][x]
        }
        for(int s=1;s<n;s++){//枚举正方形的边长
            int Ans=0;bool flag;
            for(int i=1;i<=n-s;i++)
                for(int j=1;j<=n-s;j++){//枚举正方形左上角顶点的位置
                    flag=true;
                    for(int x=i,y=j;flag&&x<i+s;x++)
                        if(!b[x][y]||!b[x][y+s])flag=false;//处理列
                    for(int x=i,y=j;flag&&y<j+s;y++)
                        if(!a[x][y]||!a[x+s][y])flag=false;//处理行
                    if(flag)Ans++;
                }
            ans[s]=Ans;
        }
        printf("Problem #%d\n\n",cnt);
        bool flag=false;
        for(int i=1;i<n;i++)
            if(ans[i]){printf("%d square (s) of size %d\n",ans[i],i);flag=true;}
        if(!flag)printf("No completed squares can be found.\n");//输出
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lty26319/p/8434854.html

时间: 2024-08-08 08:29:58

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码的相关文章

[算法竞赛入门经典]Message Decoding,ACM/ICPC World Finals 1991,UVa213

Description Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write

UVa 213 Message Decoding(World Finals1991,字符串)

 Message Decoding  Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must

UVa 213 - Message Decoding

将字符用01串进行编码,并把下面的01串转换成对应字符串 打了一遍书上的样例程序.. 读取单个字符的函数来忽略换行符还是很神奇的 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 char code[8][1<<8]; 6 char readchar(){ 7 while(1){ 8 char ch=getchar(); 9 if(c

uva 213 - Message Decoding (我觉得我的方法要比书上少很多代码,不保证好……)

#include<stdio.h> #include<math.h> #include<string.h> char s[250]; char a[10][250]; int a1[4]; int a2[250]; char ch; int init(int len) { int tt=0; for(int i=1;i<=7;i++) { for(int j=0;j<(int)pow(2,i)-1;j++) { a[i][j]=s[tt++]; if(tt&

UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)

POINT: 关于表示一个编码:利用code字符数组表示一个编码字符,其中code[len][val]表示长度为len,二进制值为val的字符: 主程序如下: 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <v

UVa 213 Message Decoding (信息编码)

该题目作为放假回归正轨的第一道正式做的题目,被卡了好久,唉,怪我有颗太浪的心 解题思路: 1.把编码头用二维字符数组存储起来,a[x][y]存储对应的字符,x表示编码的长度,y表示编码的大小, 存储时注意y<2^x - 1的 2.由于编码文本可能是由多行组成,所以需要判断和跳过对换行符的处理,为方便处理,将编码文本视作字符处理 #include<iostream> #include<CString> #include<cmath> #include<stdl

信息解码,ACM/ICPC World Finals 1991

问题 考虑下面的01串序列: 0, 00, 01, 10, 00, 001, 010, 011, 100, 101, 110, 0000, 0001, --, 1101, 1110, 00000, -- 首先是长度为1的串,然后是长度为2的串,依次推类.如果看成二进制,相同长度的后一个串等于前一个串加1.上述序列中不存在全为1的串: 编写一个解码程序.首先输入一个编码头(例如 AB#TANCnrtXc), 则上述序列的每个串依次对应编码头的每个字符.例如0对应A,00对应B,01对应#,--:

UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告

1.题目大意 把前n$n\le 10000$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更好的办法了,写完看了一下其它人的思路好像也差不多是打表的思路. 3.应注意的问题 (1)首先是格式问题,我第一次提交的时候PE了,因为没有意识到空格也会有影响.最开始我的最后一段代码是: for(i=0;i<10;i++) printf("%d ",s[n][i]); printf(&q

Puzzle, ACM/ICPC World Finals 1993, UVa227

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母.一共有4种指令:A, B, L, R,分别表示把空格上.下.左.右的相邻字母移到空格中.输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格.如果有非法指令,应输出"Thispuzzle has no final configuration.",例如,图3-5中执行ARRBBL0后,效果如图3-6所示. 写的比较简陋,先写了容器,然后填充,写移动条件. 以下是用C写的源码: #include<stdi