UVA232 UVALive5171 POJ1888 Crossword Answers

World Finals >> 1994 - Phoenix

问题链接:UVA232 UVALive5171 POJ1888 Crossword Answers。基础训练级的问题,用C语言编写程序。

典型单词谜题中,计算单词个数问题。

给网格扩充了四个方向的边界。这样可以简化程序逻辑,使得边缘格的处理逻辑与中间格的处理逻辑相同。使得判断网格是否为单词的开始网格条件统一;同时,计算行单词和列单词的逻辑也变得简单。

另外一点就是要明确单纯开始网格的条件,以及如何计算单词的序号(序号增加的条件)。

程序中,将边界设置为“*”,程序逻辑比较简洁。

AC的C语言程序如下:

/* UVA232 UVALive5171 POJ1888 Crossword Answers */

#include <stdio.h>

#define MAXN 10

char grid[MAXN+2][MAXN+2];

/* 输出行单词 */
void rowoutput(int no, int row, int col)
{
    printf("%3d.", no);
    while(grid[row][col] != '*')
        putchar(grid[row][col++]);
    putchar('\n');
}

/* 输出列单词 */
void coloutput(int no, int row, int col)
{
    printf("%3d.", no);
    while(grid[row][col] != '*')
        putchar(grid[row++][col]);
    putchar('\n');
}

int main(void)
{
    int r, c, caseno=0, wcount, i, j;

    while(scanf("%d", &r) != EOF && r != 0) {
        /* 已经读入行,继续读入列 */
        scanf("%d", &c);
        getchar();

        /* 读入网格 */
        for(i=1; i<=r; i++) {
            for(j=1; j<=c; j++)
                grid[i][j] = getchar();
            getchar();      /* '\n' */
        }

        // 设置边界
        for(i=0; i<r+2; i++) {
            grid[i][0] = '*';
            grid[i][c+1] = '*';
        }
        for(i=0; i<c+2; i++) {
            grid[0][i] = '*';
            grid[r+1][i] = '*';
        }

        /* 计算并且输出结果 */
        if(++caseno > 1)
            printf("\n");
        printf("puzzle #%d:\n", caseno);

        /* 行计算并且输出结果 */
        printf("Across\n");
        wcount = 0;
        for(i=1; i<=r; i++) {
            for(j=1; j<=c; j++)
                if(grid[i][j] != '*' && (grid[i-1][j] == '*' || grid[i][j-1] == '*')) {
                    wcount++;
                    if(grid[i][j-1] == '*')
                        rowoutput(wcount, i, j);
                }
        }

        /* 列计算并且输出结果 */
        printf("Down\n");
        wcount = 0;
        for(i=1; i<=r; i++) {
            for(j=1; j<=c; j++)
                if(grid[i][j] != '*' && (grid[i-1][j] == '*' || grid[i][j-1] == '*')) {
                    wcount++;
                    if(grid[i-1][j] == '*')
                        coloutput(wcount, i, j);
                }
        }
    }

    return 0;
}
时间: 2024-10-12 03:31:45

UVA232 UVALive5171 POJ1888 Crossword Answers的相关文章

poj 1888 Crossword Answers

Crossword Answers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 869   Accepted: 405 Description A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions). One list of de

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

Uva232 Crossword Answers

根据题意首先找出可以当作起始点的点,并用数字作顺序标记. 之后从这些起始点开始,向右开始搜寻字母组成单词,直至到边界或到黑块 之后依旧从这些起始点开始,向下开始搜寻字母组成单词,直至到边界或到黑块 其中注意输出格式如"  1.AT",题目并不是要求在数字前加2个空格,不难发现题目样例输出中有“ 19.DEA”,可以得知,题目的意思是:数字及数字前的空格总共占3个字符的位置 /* UvaOJ 232 Emerald Sat 18 Apr, 2015 */ #include <ios

UVa 232 - Crossword Answers

横排纵排找单词 先标记,再记录答案最后排序,排序时一定要对准首末位置 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <iomanip> 5 using namespace std; 6 struct T{ 7 int num; 8 char ans[15]; 9 }down[300],acr[300]; 10 int r,c,cnt1,cnt2,cnt

纵横字谜的答案 (UVa232)

纵横字谜的答案 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Crossword Answers  A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions).

ACM今日学习总结(2015.08.16)

今日完成情况 UVa 第3章 数组与字符串 六道例题已全部完成. 例题3-1 272 - TEX Quotes 例题3-2 10082 - WERTYU 例题3-3 401 - Palindromes 例题3-4 340 - Master-Mind Hints 例题3-5 1583 - Digit Generator 例题3-6 1584 - Circular Sequence 十二道习题已完成(7道). 习题3-1 1585 - Score 习题3-2 1586 - Molar mass 习题

HDU 3038 - How Many Answers Are Wrong

Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of intege

HDU3038 How Many Answers Are Wrong 【并查集】

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3145 Accepted Submission(s): 1202 Problem Description TT and FF are - friends. Uh- very very good friends -__-b FF is a b

HDU - 3038 How Many Answers Are Wrong (带权并查集)

题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用,[a, b]和为s,所以a-1与b就可以确定一次关系,通过计算与根的距离可以判断出询问的正确性 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int f[MAXN],a