一道普及-的题目写了50分钟,,,
思路就是输入,然后把初始格子标记一下,然后分别处理行和列,行的输出比较简单,但是列的情况有点不一样,需要把题意看懂。
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, c;
int t = 1;
while (cin >> r >> c) {
if (t != 1) cout << endl;
cout << "puzzle #" << t++ << ":\n";
string s[20];
for (int i = 0; i < r; i++) {
cin >> s[i];
}
int num = 1;
int hash[20][20] = {};
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (isalpha(s[i][j])) {
if (i == 0 || j == 0) hash[i][j] = num++;
else if (s[i - 1][j] == '*' || s[i][j - 1] == '*') hash[i][j] = num++;
}
}
}
cout << "Across\n";
int ok = 1;
for (int i = 0; i < r; i++) {
int k = 0;
while (k < c) {
if (isalpha(s[i][k])) {
if (ok == 1) {
printf("%3d.", hash[i][k]);
ok = 0;
}
cout << s[i][k];
if (k == c - 1) {
cout << endl;
ok = 1;
} else if (s[i][k + 1] == '*') {
cout << endl;
ok = 1;
}
}
k++;
}
}
cout << "Down\n";
int visit[20][20] = {};
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (isalpha(s[i][j]) && visit[i][j] == 0) {
int k = i;
printf("%3d.", hash[i][j]);
while (k < r && isalpha(s[k][j])) {
cout << s[k][j];
visit[k][j] = 1;
k++;
}
cout << endl;
}
}
}
}
return 0;
}