Uva232 Crossword Answers

根据题意首先找出可以当作起始点的点,并用数字作顺序标记。

之后从这些起始点开始,向右开始搜寻字母组成单词,直至到边界或到黑块

之后依旧从这些起始点开始,向下开始搜寻字母组成单词,直至到边界或到黑块

其中注意输出格式如"  1.AT",题目并不是要求在数字前加2个空格,不难发现题目样例输出中有“ 19.DEA”,可以得知,题目的意思是:数字及数字前的空格总共占3个字符的位置

/*
    UvaOJ 232
    Emerald
    Sat 18 Apr, 2015
*/
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 10 + 2;
char puzzle[ MAXN ][ MAXN ];
int isStart[ MAXN ][ MAXN ];
//bool used[ MAXN ][ MAXN ];
int rows, cols, posCount;

const char BLACK_GRID = '*';

int isStarted( int i, int j ) { // judge whether the puzzle[i][j] is a starting point
    if( puzzle[i][j] == BLACK_GRID ) {
        return -1; // 这个
    }
    if( i-1<0 || j-1<0 ) {
        return ++posCount;
    }
    if( puzzle[i-1][j]==BLACK_GRID || puzzle[i][j-1]==BLACK_GRID ) {
        return ++posCount;
    }
    return -1;
}

void across( int x, int y ) { // whether can output a word
    if( ( y-1<0 || puzzle[x][y-1]==BLACK_GRID ) && puzzle[x][y]!=BLACK_GRID ) { // presention !!!
        printf( "%3d.", isStart[x][y] );
        while( y<cols && puzzle[x][y]!=BLACK_GRID ) {
            printf( "%c", puzzle[x][y++] );
        }
        printf("
");
    }
}

void down( int x, int y ) { // whether can output a word
    if( ( x-1<0 || puzzle[x-1][y]==BLACK_GRID ) && puzzle[x][y]!=BLACK_GRID ) {
        printf( "%3d.", isStart[x][y] ); // presention !!!
        while( x<rows && puzzle[x][y]!=BLACK_GRID ) {
            printf( "%c", puzzle[x++][y] );
        }
        printf("
");
    }
}

int main() {
    int i, j;
    int counter = 0;
    while( scanf( "%d", &rows )!=EOF && rows ) {
        scanf( "%d", &cols );
        posCount = 0;
        for( i=0; i<rows; i++ ) {
            getchar();
            for( j=0; j<cols; j++ ) {
                scanf( "%c", &puzzle[i][j] );
                isStart[i][j] = isStarted( i, j );
            }
        }
        if( counter ) {
            printf( "
" );
        }
        printf( "puzzle #%d:
", ++counter );
        /* --------Across---------- */
        printf( "Across
" );
        for( i=0; i<rows; i++ ) {
            for( j=0; j<cols; j++ ) {
                across( i, j );
            }
        }
        /* --------Down---------- */
        printf( "Down
" );
        for( i=0; i<rows; i++ ) {
            for( j=0; j<cols; j++ ) {
                down( i, j );
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Emerald/p/4438071.html