求解字谜游戏问题-数据结构与算法分析-C语言描述 Mark Allen Weiss-第一章练习题


/*
 编写一个程序求解字谜游戏问题。
 */

#include 
#include 
#include 
using namespace std;
int m, n, p;       //p代表需要解谜的单词数
string s, s0;      //定义的临时变量,用来输入
vector<string> v2;
vector<vector<string>> v;     //创建容器,宏定义替换代码
#define DAITI s0 = s0 + v[x][j];\
h(s0);
#define DAITI2 s0 = s0 + v[i][y];\
h(s0);
#define DAITI3 s0 = s0 + v[i][j];\
h(s0);


void h(string s)            //h函数,用来进行比较每个单词组合和给定单词
{
	for (int i = 0; i<p; i++)
	{
		string s3 = v2[i];
		if (s.compare(s3) == 0)
		{
			cout << "单词: "<<s3<<"  匹配成功" << endl;
		}
	}
}
void f(int x, int y)                 //f函数,用来实现对每个元素实行单词组合的操作
{
	s0 = "";
	for (int j = y; j < n; j++)
	{
		DAITI
	}

	s0 = "";
	for (int j = y; j >= 0; j--)
	{
		DAITI
	}

	s0 = "";
	for (int i = x; i < m; i++)
	{
		DAITI2
	}

	s0 = "";
	for (int i = x; i >= 0; i--)
	{
		DAITI2
	}

	s0 = "";
	int j = y;
	for (int i=x; i<m && j<n; i++)
	{
		DAITI3
		j++;
	}

	s0 = "";
	j = y;
	for (int i=x; i>=0 && j>=0; i--)
	{
		DAITI3
		j--;
	}

	s0 = "";
	j = y;
	for (int i=x; i>=0 && j<n; i--)
	{
		DAITI3
		j++;
	}

	s0 = "";
	j = y;
	for (int i=x; i<m && j>=0; i++)
	{
		DAITI3
		j--;
	}
}

int main()
{
	cout << "请输入字谜的行数m和列数n:" << endl;
	cout << "行数m为:";
	cin >> m;
	cout << "列数n为:";
	cin >> n;
	v.resize(m);
	for (int i = 0; i < m; i++)
	{
		v[i].resize(n);
	}
	cout << "请依次输入字谜字母:" << endl;
	string s;      //定义的临时变量,用来输入;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> s;
			v[i][j] = s;
		}
	}

	string s2;
	cout << "请输入需要解谜的单词数p:" << endl;
	cin >> p;
	cout << "请依次输入需要解谜的单词:" << endl;
	v2.resize(p);
	for (int i = 0; i < p; i++)
	{
		cin >> s2;
		v2[i] = s2;
	}

	for (int i = 0; i < m; i++)      //对每一个元素调用f函数
	{
		for (int j = 0; j < n; )
		{
			f(i, j);
			j++;
		}
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(Algorithms)