【模拟】Crossword Answers

【模拟】Crossword Answers

原题地址
题目思路比较简单,难点是输出内容格式的处理。
C++ AC代码:

#include <iostream>
using namespace std;
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstdio>
#define N 10000
char a[N][N];
int nu[N][N] = {0};
int r, c, caseNo = 0;
map<int, string> m;
bool last = false,first = true;
void init() {
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            cin >> a[i][j];
    //初始化编号
    int n = 0;
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            if (j == 0 || i == 0 || a[i][j - 1] == '*' || a[i - 1][j] == '*')
                if (a[i][j] != '*') nu[i][j] = ++n;
}
int main() {
    while (true) {
        cin >> r; if (!r)   last = true;
        if (!last) {
            cin >> c;
            if (!first)    cout << endl;
        } else return 0;
        init();
        //Across
        cout << "puzzle #" << ++caseNo << ":" << endl;
        cout << "Across" << endl;
        bool y = true;
        int n = 0;
        for (int i = 0; i < r; i++) {
            string across;
            for (int j = 0; j < c; j++) {
                if (a[i][j] == '*' || j == c - 1) {
                    if (a[i][j] != '*') {
                        across += a[i][j];
                        if (y) {
                            n = nu[i][j];
                            y = false;
                        }
                    }
                    if (!across.empty()) {
                        //cout << "  " << n << "." << across << endl;
                        printf("%3d.%s\n", n, across.c_str());
                        y = true;
                    }
                    across.clear();
                } else if (a[i][j] != '*') {
                    across += a[i][j];
                    if (y) {
                        n = nu[i][j];
                        y = false;
                    }
                }
            }
        }
        //down
        n = 0;y = true;
        for (int j = 0; j < c; j++) {
            string down;
            for (int i = 0; i < r; i++) {
                if (a[i][j] == '*' || i == r - 1) {
                    if (a[i][j] != '*') {
                        down += a[i][j];
                        if (y) {
                            n = nu[i][j];
                            y = false;
                        }
                    }
                    if (!down.empty()) {
                        char t[N];
                        sprintf(t, "%3d.%s", n, down.c_str());
                        m.insert(make_pair(n, t));
                        y = true;
                    }
                    down.clear();
                } else if (a[i][j] != '*') {
                    down += a[i][j];
                    if (y) {
                        n = nu[i][j];
                        y = false;
                    }
                }
            }
        }
        cout << "Down" << endl;
        for (auto i : m)  cout << i.second << endl;
        first = false; m.clear();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值