Crossword Answers -------行与列按序输出

题目链接:https://vjudge.net/problem/UVA-232#author=0

题意:关键句:The de nitions correspond to the rectangular grid bymeans of sequential integers on \eligible" white squares.White squares with black squares immediately to the left orabove them are \eligible." White squares with no squares ei-ther immediately to the left or above are also \eligible." No other squares are numbered. All of thesquares on the rst row are numbered白色方框的左方或上方有黑色方框或者无方框,进行编号

题解:1.编号——无方框咋弄-------弄一个笼子,周围是0,0为边界标识符,注意此处应该是字符所以应写成‘0’,否则当空字符处理

    2.按行输出容易,如何按列输出,又按着编号——还是像行一样先循环行,再循环列(保证编号从小到大),然后因为按列输出,所以再按行循环(从循环时的i开始,到x结束,注意不能改变i的值),并每次输出都将序号设为0,这样什么时候输出就看序号是否为0即可。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<iomanip>
using namespace std;
char s[20][20];
int x,y,cou=0,a[20][20];
void init()
{
    cou=0;//注意初始化
    memset(s,0,sizeof(s));
    memset(a,0,sizeof(a));
    for(int i=0; i<=x+1; i++)//安装边界
    {
        s[i][0]='0';
        s[i][y+1]='0';
    }
    for(int i=0; i<=y+1; i++)
    {
        s[0][i]='0';
        s[x+1][i]='0';
    }
}
void Num_s()//编号
{
    for(int i=1; i<=x; i++)
        for(int j=1; j<=y; j++)
            if(s[i][j]!='*')
                if(s[i][j-1]=='0'||s[i][j-1]=='*'||s[i-1][j]=='0'||s[i-1][j]=='*')
                {
                    cou++;
                    a[i][j]=cou;
                }
}
int main()
{
    int c=0;
    while(cin>>x)
    {
        if(x==0) break;
        cin>>y;
        c++;
        getchar();//吸收空行
        init();
        for(int i=1; i<=x; i++)
        {
            for(int j=1; j<=y; j++)
                s[i][j]=getchar();
            getchar();
        }
        Num_s();
        if(c!=1)//注意空行
            cout<<endl;
        cout<<"puzzle #"<<c<<":"<<endl;
        cout<<"Across"<<endl;
        for(int i=1; i<=x; i++)
        {
            int flag=0;
            for(int j=1; j<=y; j++)
            {
                if(s[i][j]!='*')
                {
                    if(flag==0)
                        cout<<setw(3)<<a[i][j]<<'.';
                    flag=1;
                    cout<<s[i][j];
                }
                else
                {
                    if(flag==1)
                        cout<<endl;
                    flag=0;
                }
                if(j==y&&flag==1)
                    cout<<endl;
            }
        }
        cout<<"Down"<<endl;
        for(int i=1; i<=x; i++)
        {
            int flag=0;
            for(int j=1; j<=y; j++)
            {
                if (a[i][j] != 0)
                {
                    printf("%3d.", a[i][j]);
                    for (int k=i; k <= x; k++)
                    {
                        if (s[k][j] == '*')
                        {
                            flag=0;
                            cout << endl;
                            break;
                        }
                        else
                        {
                            flag=1;
                            cout << s[k][j];
                        }
                        a[k][j]=0;
                    }
                    if(flag==1)
                        cout << endl;
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Joe2019/p/12678141.html