【模拟】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;
}