算法竞赛入门经典 习题3-6

UVa232

Crossword Answers

根据题目叙述的内容进行编号,然后固定行,遍历列,打印横向的单词。

竖向的单词好像不能通过固定列,遍历行的方式,所以为了避免重复的打印,打印竖向单词的时候我就把标号给去掉了,反正也没什么影响。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int cnt = 0;
	while (1){
		cnt++;
		vector<vector<char>> vvcSquare(10, vector<char>(10, '*'));
		vector<vector<int>> vviNumber(10, vector<int>(10, 0));
		int row = 0, col = 0;
		int iNumber = 1;
		cin >> row;
		if (row == 0) break;
		else if (cnt != 1) cout << endl;
		cin >> col;
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				cin >> vvcSquare[i][j];
			}
		}
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				if (vvcSquare[i][j] == '*') continue;
				if (i - 1 < 0 || j - 1 < 0){
					vviNumber[i][j] = iNumber++;
				}
				else if (vvcSquare[i - 1][j] == '*' || vvcSquare[i][j - 1] == '*'){
					vviNumber[i][j] = iNumber++;
				}
			}
		}
		cout << "puzzle #" << cnt << ':' << endl;
		cout << "Across" << endl;
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				if (vviNumber[i][j] != 0){
					if (vviNumber[i][j] >= 10) cout << ' ';
					else cout << "  ";
					cout << vviNumber[i][j] << '.';
					int k = j;
					for (; k < col; k++)
					{
						if (vvcSquare[i][k] != '*') cout << vvcSquare[i][k];
						else break;
					}
					cout << endl;
					j = k;
				}
			}
		}
		cout << "Down" << endl;
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				if (vviNumber[i][j] != 0){
					if (vviNumber[i][j] >= 10) cout << ' ';
					else cout << "  ";
					cout << vviNumber[i][j] << '.';
					for (int k = i; k < row; k++)
					{
						if (vvcSquare[k][j] != '*'){
							vviNumber[k][j] = 0;
							cout << vvcSquare[k][j];
						}
						else break;
					}
					cout << endl;
				}
			}
		}
	}
	return 0;
}
/*
2 2
AT
*O
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值