1. 程式人生 > >Crossword Answers UVA - 232

Crossword Answers UVA - 232

這道題覺得主要還是格式問題吧,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');
			}
		}
	}
}