Crossword Answers UVA - 232

题目: 给你一个二维的字符表,里面有一些字母和和边界都当做是分隔符, 按照图示方式编号连续位置(从上到下,从左到右)生成单词,输出所有横向和竖向单词。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long

using namespace std;
int m,n,a[20][20];
string s[20];

typedef struct _node
{
    int  id;
    char str[12];
}node;

node data[101];

int cmp(node a, node b)
{
    return a.id < b.id;
}



void init()
{
     int c = 0;
     memset(a,0,sizeof(a));
     for(int i = 0; i < n; i++)
        if(s[0][i]!='*') a[0][i] = (++c);
     for(int i = 1; i < m; i++)
        for(int j = 0; j < n; j++) {
            if(s[i][j]!='*') {
                if(j == 0) a[i][j] = (++c);
                else if(s[i-1][j] == '*'||s[i][j-1] == '*') a[i][j] = (++c);
            }
        }
}

void solve() {
        printf("Across\n");
        for (int i = 0; i < m; i++) {
            int c = 0;
            while (c < n) {
                if (a[i][c]) {
                    printf("%3d.",a[i][c]);
                    int save = 0;
                    while (c < n && s[i][c] != '*')
                        printf("%c",s[i][c ++]);
                    printf("\n");
                }else c++;
            }
        }

        printf("Down\n");
        int cut = 0;
        for (int i = 0; i < n; ++ i) {
            int c = 0;
            while (c < m) {
                if (a[c][i]) {
                    data[cut].id = a[c][i];
                    int save = 0;
                    while (c < m && s[c][i] != '*')
                        data[cut].str[save ++] = s[c ++][i];
                    data[cut ++].str[save ++] = 0;
                }else c ++;
            }
        }
        sort(data, data+cut, cmp);
        for (int i = 0; i < cut; i++)
            printf("%3d.%s\n",data[i].id, data[i].str);
}

int main()
{
    int caset = 0;
    while(1){
        scanf("%d",&m); if(m == 0) break;
        if(caset != 0) cout << endl;
        scanf("%d",&n);
        for(int i = 0; i < m; i++) cin >> s[i];
        init();
        printf("puzzle #%d:\n",++caset);
        solve();
        for(int i = 0; i < m; i++)
        s[i].erase();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值