Crossword Answers

Uva232

这道题只要根据输出来输出,另外注意不管行还是列每个字符都只会用一次,所以标记就行了。

#include <stdio.h>
#include <string.h>
#define clr( a ) memset ( a, 0, sizeof ( a ) )
const int maxn = 15;
int vis[maxn][maxn], value[maxn][maxn];
char str[maxn][maxn];
int main ( )
{
    int n, m, begin, cas = 0;
    while ( ~ scanf ( "%d", &n ) && n )
    {
        clr ( value );
        scanf ( "%d", &m );
        for ( int i = 0; i < n; i ++ )
            scanf ( "%s", str[i] );
        begin = 1;
        for ( int i = 0; i < n; i ++ )
            for ( int j = 0; j < m; j ++ )
                if ( ( i == 0 || j == 0 || str[i-1][j] == '*' || str[i][j-1] == '*' ) && str[i][j] != '*' )
                //注意还需要判断本身是否是*,不然就不赋值
                    value[i][j] = begin ++;
        if ( cas ++ )
            printf ( "\n" );
        printf ( "puzzle #%d:\n", cas );
        clr ( vis );    //标记哪些字符被用了
        printf ( "Across\n" );
        for ( int i = 0; i < n; i ++ )
            for ( int j = 0; j < m; j ++ )
            {
                if ( vis[i][j] || value[i][j] == 0 )
                    continue ;
                printf ( "%3d.", value[i][j] );
                //占三位
                while ( j < m && str[i][j] != '*' )
                {
                    printf ( "%c", str[i][j] );
                    vis[i][j] = 1;  //行其实不用标记
                    j ++;
                }
                j --;
                printf ( "\n" );
            }
        clr ( vis );
        printf ( "Down\n" );    //这部分要根据样例输出来输出
        for ( int i = 0; i < n; i ++ )
            for ( int j = 0; j < m; j ++ )
            {
                if ( vis[i][j] || value[i][j] == 0 )
                    continue ;
                printf ( "%3d.", value[i][j] );
                int k = i;
                while ( k < n && str[k][j] != '*' )
                {
                    printf ( "%c", str[k][j] );
                    vis[k][j] = 1;  //列必须标记
                    k ++;
                }
                printf ( "\n" );
            }
    }
    return 0;
}


你可能感兴趣的:(Crossword Answers)