Corssword Answer UVa232

code:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define maxn 150
//#define LOCAL

char puzzle[maxn];
int indices[maxn];
int saver[maxn];
//judge whether it is a eligibal white square or not.
int judgement(int n, int cols,int index)
{
    if(puzzle[index] == '*')return 0;
    if((index%cols-1)<0||puzzle[index-1] == '*'||(index-cols)<0||puzzle[index-cols] == '*') return 1;
    else return 0;
    
}


//fine the eligibal white square into indices .
void get_eligibalws(int n, int cols)
{
    int i = 0;
    int index(0);
    for(; i < n; i++)
    {
        if(judgement(n,cols,i)) { indices[index] = i; index++; }

    }
}
//calculate the range of the indices.
int cal_range()
{
    int i = 0;
    for(; i < 103; i++)
    {
        if( indices[i] > 100 ) break;
    }
    return i;

}
//judge the existence of the index in indices
//if exist return the index.
int cmp(int a)
{
    int i = 0;
    int n = cal_range();
    for(; i < n; i++)
    {
        if(indices[i] == a) break;
    }
    if( i < n) return i;
    else return 0;
}


void read_across(int rgnum, int cols)
{
    printf("Across\n");
    int i = 0;
    for(; i<rgnum; i++)
    {
        if(i+1<10) printf("  %d.",i+1);
        if(i+1>= 10) printf(" %d.",i+1);
        int index = indices[i];
        while(isalpha(puzzle[index]) )
        {
            int d = cmp(index);
            if(d) i = d;
            putchar(puzzle[index]);
            index++;
            if(index-(index/cols)*cols==0)break;
        }
        putchar('\n');
    }
}

void read_down(int rgnum, int n, int cols)
{
    printf("Down\n");
    int i = 0;
    for(; i<rgnum; i++)
    {    
        if(saver[i])continue;    
        if(i+1<10) printf("  %d.",i+1);
        if(i+1>=10) printf(" %d.",i+1);
        int index = indices[i];
        while(isalpha(puzzle[index]))
        {
            int d = cmp(index);
            if(d)saver[d] = 1;           //save the existence.
            if(index>n)break;
            putchar(puzzle[index]);
            index += cols;
        }
        putchar('\n');
    }

}
int main()
{
    int count = 0;
    int first = 1;
    //be used to test.
    #ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
    #endif
    while(true)
    {
        count++;
        memset(indices,50,sizeof(int)*maxn);
        memset(puzzle,0,sizeof(char)*maxn);
        memset(saver,0,sizeof(int)*maxn);
        char a;
        int rows(0), cols(0);
        scanf("%d", &rows);
        if(rows == 0) break;
        scanf("%d", &cols);
        getchar();
        int n = rows*cols;
        int i = 0;
        int j = 0;
        for(; i < rows; i++)
        {
            j = 0;
            for(; j < cols; j++)
            {
                a = getchar();
                if (a == '\n') a = getchar();
                puzzle[i*cols+j] = a;
            }
        }
        get_eligibalws(n,cols);
        int rgnum = cal_range();
        if(first) first = 0;
        else putchar('\n');
        printf("puzzle #%d:\n", count);
        read_across(rgnum,cols);
        read_down(rgnum,n,cols);
    }
    
    return 0;
}

 这里是通过一位数组的方式写的,或许也可以使用二维数组会更方便一点,代码量会少一点。

猜你喜欢

转载自www.cnblogs.com/dreamworldclark/p/9429563.html