[算法竞赛入门经典] Crossword Answers ACM/ICPC World Finals 1994,UVa232

Description

A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions).

One list of definitions is for “words” to be written left to right across white squares in the rows and the other list is for words to be written down white squares in the columns. (A word is a sequence of alphabetic characters.)

To solve a crossword puzzle, one writes the words corre- sponding to the definitions on the white squares of the grid.

The definitions correspond to the rectangular grid by

means of sequential integers on “eligible” white squares.

White squares with black squares immediately to the left or

above them are “eligible.” White squares with no squares ei-

ther immediately to the left or above are also “eligible.” No other squares are numbered. All of the squares on the first row are numbered.

The numbering starts with 1 and continues consecutively across white squares of the first row, then across the eligible white squares of the second row, then across the eligible white squares of the third row and so on across all of the rest of the rows of the puzzle. The picture below illustrates a rectangular crossword puzzle grid with appropriate numbering.

An “across” word for a definition is written on a sequence of white squares in a row starting on a numbered square that does not follow another white square in the same row.

The sequence of white squares for that word goes across the row of the numbered square, ending immediately before the next black square in the row or in the rightmost square of the row.

A “down” word for a definition is written on a sequence of white squares in a column starting on a numbered square that does not follow another white square in the same column.

The sequence of white squares for that word goes down the column of the numbered square, ending immediately before the next black square in the column or in the bottom square of the column.

Every white square in a correctly solved puzzle contains a letter.

You must write a program that takes several solved crossword puzzles as input and outputs the lists of across and down words which constitute the solutions.

Input

Each puzzle solution in the input starts with a line containing two integers r and c (1 ≤ r ≤ 10 and 1 ≤ c ≤ 10), where r (the first number) is the number of rows in the puzzle and c (the second number) is the number of columns.

The r rows of input which follow each contain c characters (excluding the end-of-line) which describe the solution. Each of those c characters is an alphabetic character which is part of a word or the character ‘*’, which indicates a black square.

The end of input is indicated by a line consisting of the single number

Output

Output for each puzzle consists of an identifier for the puzzle (puzzle #1:, puzzle #2:, etc.) and the list of across words followed by the list of down words. Words in each list must be output one-per-line in increasing order of the number of their corresponding definitions.

The heading for the list of across words is ‘Across’. The heading for the list of down words is ‘Down’.

In the case where the lists are empty (all squares in the grid are black), the ‘Across’ and ‘Down’ headings should still appear.

Separate output for successive input puzzles by a blank line.

Sample Input

22
AT
*O
67
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0

Sample Output

puzzle #1:
Across
  1.AT
  3.O
Down
  1.A
  2.TO
puzzle #2:
Across
  1.AIM
  4.DEN
  7.ME
  8.ONE
  9.UPON
 11.TO
 12.SO
 13.ERIN
 15.SA
 17.OR
 18.IES
 19.DEA
Down 1.A
  2.IMPOSE
  3.MEO
  4.DO
  5.ENTIRE
  6.NEON
  9.US
 10.NE
 14.ROD
 16.AS
 18.I
 20.A
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cctype>
#define MAX 1005
using namespace std; 

char s[MAX][MAX];       // 储存网格
int flag[MAX][MAX];     // 记录起始点
char in[5];

int main()
{
    int cnt = 0;
    while(~scanf("%[^\n]%*c",in) && (in[0]-‘0‘)){
        memset(flag,0,sizeof(flag)); memset(s,0,sizeof(s)); // 清除内容
        int r = in[0]-‘0‘,c = in[2]-‘0‘;
        for(int i = 0;i < r;i ++) scanf("%s",s[i]);         // 输入网格
        int flagCNT = 0;
        for(int i = 0;i < r;i ++)                           // 判断起始点
        for(int j = 0;j < c;j ++)
            if((s[i][j]!=‘*‘) &&
                (i == 0 || j == 0 || s[i-1][j]==‘*‘ || s[i][j-1]==‘*‘))
                flag[i][j] = ++flagCNT;
        printf("puzzle #%d:\nAcross\n",++cnt);
/*--------------------- 横向输出 --------------------------------*/
        bool next = true; bool fis = true;
        for(int i = 0;i < r;i ++)
        for(int j = 0;j < c;j ++){
            if(next && s[i][j]!=‘*‘){
                fis ? printf("%4d.",flag[i][j]) :
                printf("\n%4d.",flag[i][j]);
            }
            if(s[i][j]!=‘*‘) {
                putchar(s[i][j]); next = false;
                if(j==c-1 && s[i+1][0]!=‘*‘) next = true;
            }
            else { fis = false; next = true; }
        }
        printf("\nDown\n");
/*--------------------- 纵向输出 --------------------------------*/
        next = true; fis = true;
        for(int i = 0;i < r;i ++)
        for(int j = 0;j < c;j ++){
            if(next && s[i][j]!=‘*‘ && flag[i][j] && (i==0 || s[i-1][j]==‘*‘)){
                fis ? printf("%4d.",flag[i][j]) :
                printf("\n%4d.",flag[i][j]);
            }
            if(flag[i][j] && (i==0 || s[i-1][j]==‘*‘)){
                int down = i;
                while(s[down][j]!=‘*‘ && down < r) {
                    putchar(s[down++][j]); next = false;
                }
                fis = false; next = true;
            }
        }
        printf("\n");
        getchar();          // 吞掉输入后的回车
    }
    return 0;
}

原文地址:https://www.cnblogs.com/1Kasshole/p/9685988.html

时间: 2024-10-11 14:33:48

[算法竞赛入门经典] Crossword Answers ACM/ICPC World Finals 1994,UVa232的相关文章

[算法竞赛入门经典]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

《算法竞赛入门经典(第二版)》pdf

下载地址:网盘下载 内容简介  · · · · · · <算法竞赛入门经典(第2版)>是一本算法竞赛的入门与提高教材,把C/C++语言.算法和解题有机地结合在一起,淡化理论,注重学习方法和实践技巧.全书内容分为12 章,包括程序设计入门.循环结构程序设计.数组和字符串.函数和递归.C++与STL入门.数据结构基础.暴力求解法.高效算法设计.动态规划初步.数学概念与方法.图论模型与算法.高级专题等内容,覆盖了算法竞赛入门和提高所需的主要知识点,并含有大量例题和习题.书中的代码规范.简洁.易懂,不

《算法竞赛入门经典第二版》 P35 习题2-4 子序列的和(subsequence)

/* <算法竞赛入门经典第二版> P35 习题2-4: 输入两个正整数 n < m < 10^6,输出 (1/n)^2 + 1/(n+1)^2 +……+ 1/m^2,保留5位小数. 输入包含多组数据,结束标志为 m=n=0. 有错欢迎指出^_^ */ #include<stdio.h> int main() { int m,n,i,j=1; while(scanf("%d%d",&m,&n) != EOF) { double sum

算法竞赛入门经典_4.3_递归

看代码 #include <stdio.h> int f(int n){ return n == 0?1:f(n-1)*n; } int main() { printf("%d\n", f(5)); return 0; } 上面f函数使用了递归,递归由两部分组成,一是递归头,二是递归体. 我们使用gcc调试工具 H:\编程书籍学习\算法竞赛入门经典2代码\算法入门经典第四章>b f 'b' 不是内部或外部命令,也不是可运行的程序 或批处理文件. H:\编程书籍学习\算

《算法竞赛入门经典》动态规划复习

codevs 4979 数塔 1 #define N 100 2 #include<iostream> 3 using namespace std; 4 #include<cstdio> 5 int a[N][N],b[N][N],n; 6 int main() 7 { 8 scanf("%d",&n); 9 for(int i=1;i<=n;++i) 10 for(int j=1;j<=i;++j) 11 { 12 scanf("

算法竞赛入门经典训练指南

最近在看算法竞赛入门经典训练指南这本书,书中不错的算法我将在博客中发布,和大家共同学习. 题目: 在你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士可以雇佣,一个能力值为m的骑士可以砍掉一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才能砍掉恶龙的所有头,且需要支付的金币最少?注意,一个骑士只能砍一个头(且不能被雇佣两次). 输入格式: 输入包含多组数据.每组数据的第一行为正整数m和n(1<=m,n<=20 000):以下m行每行为一个整数,即恶龙每

算法竞赛入门经典-训练指南(10881-Piotr&#39;s Ants)

题目大意: 一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置: 输入:t,(t个样例),每个样例输入 L,T,n,接下来是n行每行两个数据,一个POS(位置),一个dir(方向): 输出:按输入顺序输出每只蚂蚁的最终位置,若处于碰撞状态则输出Turning,掉下去输出"Fell off": 解题思路: 本题类似于<挑战程序设计>的一道水题(POJ -1852  Ants),思路题:不过本题输入并不一

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

棋盘覆盖问题(算法竞赛入门经典)

在一个 2^k * 2^k 个方格组成的棋盘中,若恰有一个方格与其它方格不同,则称该方格为一特殊方格,称该棋盘为一特殊棋盘.显然特殊方格在棋盘上出现的位置有 4^k 种情形.因而对任何 k>=0 ,有 4^k 种不同的特殊棋盘.下图所示的特殊棋盘为 k=2 时 16 个特殊棋盘中的一个. 在棋盘覆盖问题中,要用下图中 4 中不同形态的 L 型骨牌覆盖一个给定的特殊棋牌上除特殊方格以外的所有方格,且任何 2 个 L 型骨牌不得重叠覆盖.易知,在任何一个 2^k * 2^k 的棋盘中,用到的 L 型