欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

纵横字谜的答案 Crossword Answers, ACM/ICPC World Finals 1994, UVa232

程序员文章站 2024-01-03 18:46:10
#include #include #include #include #include using namespace std;int main(){int col, row;int puznum = 0;while (cin >> row && row != 0){int num = 0;....

纵横字谜的答案 Crossword Answers, ACM/ICPC World Finals 1994, UVa232

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
	int col, row;
	int puznum = 0;
	while (cin >> row && row != 0)
	{
		int num = 0;
		cin >> col;
		if (puznum != 0)
			cout << endl;
		cout << "puzzle #" << ++puznum << ":" << endl;
		vector<string> solution;
		int** a;//存储各方格数值,0代表为非eligible square的白方块,-1表示黑方块
		a = new int* [row];
		for (int i = 0; i < row; ++i)
		{
			a[i] = new int[col] {0};
		}
		string s;
		for (int i = 0; i < row; ++i)
		{
			cin >> s;
			solution.push_back(s);
		}
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < col; ++j)
			{
				if ((i == 0 || j == 0 || solution[i - 1][j] == '*' || solution[i][j - 1] == '*') && solution[i][j] != '*')
				{
					a[i][j] = ++num;
				}
				else if (solution[i][j] == '*')
				{
					a[i][j] = -1;
				}
				else
				{
					a[i][j] = 0;
				}
			}
		}
		cout << "Across\n";
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < col; ++j)
			{
				if (a[i][j] > 0)
				{
					cout << setw(3) << a[i][j] << '.';
					while (j < col && solution[i][j] != '*')
					{
						cout << solution[i][j++];
					}
					cout << endl;
				}
			}
		}
		cout << "Down\n";
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < col; ++j)
			{
				if (a[i][j] > 0 && (i == 0 || a[i - 1][j] == -1))
				{
					cout << setw(3) << a[i][j] << '.';
					int temp = i;
					while (temp < row && solution[temp][j] != '*')
					{
						cout << solution[temp++][j];
					}
					cout << endl;
				}
			}
		}
		//释放动态数组指向的内存
		for (int i = 0; i < row; ++i)
		{
			delete[] a[i];
		}
		delete[] a;
	}
}

本文地址:https://blog.csdn.net/weixin_46273288/article/details/107617484

相关标签: 算法入门紫

上一篇:

下一篇: