Corssword Answer UVa232

Posted dreamworldclark

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
");
    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(
);
    }
}

void read_down(int rgnum, int n, int cols)
{
    printf("Down
");
    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(
);
    }

}
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 == 
) a = getchar();
                puzzle[i*cols+j] = a;
            }
        }
        get_eligibalws(n,cols);
        int rgnum = cal_range();
        if(first) first = 0;
        else putchar(
);
        printf("puzzle #%d:
", count);
        read_across(rgnum,cols);
        read_down(rgnum,n,cols);
    }
    
    return 0;
}

 

 

 

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

 

以上是关于Corssword Answer UVa232的主要内容,如果未能解决你的问题,请参考以下文章

每日一题 uva-232 模拟+输出要求很严格

uva 232 Crossword Answers

UVA232

UVa 232 - Crossword Answers

UVA232字符串处理

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