这道题觉得主要还是格式问题吧,Down的输出值还需要按顺序排列,一开始没去想后面才发现,在原基础上改了半天,还好最后AC了
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int r, c; // r行和c列
int first = 1; // 判断是否是第一个输出
int kcase = 1; // 判断是第几个输出
string strs[15];
int nums[15][15] = {0}; // 记录顺序
int needoutput[15][15] = {0}; // 记录Down中需要输出的起始格
int num = 1;
void inputNumsAcross(); // 记录Across空白格的顺序
void Across();
void Down();
int main() {
strs[0] = "************"; // 让第一行全为*
while (cin >> r) {
if (r == 0) // 如果r为0,则退出
break;
cin >> c;
while (getchar() != '\n')
continue;
int i = 1;
for (i = 1; i <= r; i++) { // 输入网格
getline(cin, strs[i]);
strs[i] = '*' + strs[i] + '*'; // 令第一个和最后一个字符为*
}
strs[i] = "************";
if (first)
first = 0;
else
putchar('\n');
printf("puzzle #%d:\n", kcase++);
inputNumsAcross();
Across();
Down();
}
return 0;
}
void inputNumsAcross() {
num = 1;
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++) {
if (strs[i][j] != '*') {
if (strs[i][j-1] == '*' || strs[i-1][j] == '*')
nums[i][j] = num++;
}
}
}
void Down() {
cout << "Down" << endl;
for (int j = 1; j <= c; j++) {
for (int i = 1; i <= r; i++) {
if (nums[i][j] != 0) { // 如果是起始格
needoutput[i][j] = nums[i][j];
while (strs[i][j] != '*')
strs[i++][j];
}
}
}
for (int i1 = 1; i1 <= r; i1++) {
for (int j1 = 1; j1 <= c; j1++) {
if (needoutput[i1][j1] != 0) {
int i2 = i1;
printf("%3d.", nums[i1][j1]);
while (strs[i2][j1] != '*')
putchar(strs[i2++][j1]);
putchar('\n');
}
}
}
memset(nums, 0, sizeof(nums));
memset(needoutput, 0, sizeof(needoutput));
}
void Across() {
cout << "Across" << endl;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (nums[i][j] != 0) { // 如果是起始格
printf("%3d.", nums[i][j]);
while (strs[i][j] != '*')
putchar(strs[i][j++]);
putchar('\n');
}
}
}
}