ZOJ 1011 NTA (DFS)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011

简单题,DFS

#include <iostream>
#include <string.h>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;

vector< pair<int, int> > matrix[15][10];
int n, m, k;
int NTA;

bool DFS(char root[], int current, int signal)
{
	int left = current * 2, right = left + 1;
	vector< pair<int, int> > tranList = matrix[signal][root[current] - 'a'];
	if (root[left] != '*' && root[right] != '*')
	{
		for (int i = 0; i < tranList.size(); i++)
			if (DFS(root, left, tranList[i].first) && DFS(root, right, tranList[i].second))
				return true;
		return false;
	}
	else if (root[left] != '*' && root[right] == '*')
	{
		for (int i = 0; i < tranList.size(); i++)
			if (DFS(root, left, tranList[i].first))
				return true;
		return false;
	}
	else if (root[left] == '*' && root[right] != '*')
	{
		for (int i = 0; i < tranList.size(); i++)
			if (DFS(root, right, tranList[i].second))
				return true;
		return false;
	}
	else // leaf node
	{
		for (int i = 0; i < tranList.size(); i++)
			if (tranList[i].first >= (n - m) && tranList[i].second >= (n - m))
				return true;
		return false;
	}
}

int main()
{
	char c;
	int NTAIndex = 1;

	while (cin >> n >> m >> k)
	{
		if (n == 0 && m == 0 && k == 0)
			break;
		getchar(); // read newline character
		for (int i = 0; i < n; i++)
			for (int j = 0; j < k; j++)
			{
				int num = 0;
				vector<int> v;
				while (true)
				{
					c = getchar();
					if (c == ' ')
					{
						v.push_back(num);
						num = 0;
					}
					else if (c == '\n')
					{
						v.push_back(num);
						matrix[i][j].clear();
						for(int p = 0; p < v.size(); p += 2)
							matrix[i][j].push_back(make_pair<int, int>(v[p], v[p+1]));
						break;
					}
					else
					{
						num = 10 * num + c - '0';
					}
				}
			}

		if (NTAIndex > 1)
			cout << endl;
		printf("NTA%d:\n", NTAIndex++);
		while (cin >> NTA)
		{
			if (NTA == -1)
				break;
			getchar(); // read newline character
			char tree[2048]; // 10 level, taotal 2^11=2048 elements
			memset(tree, '*', sizeof(tree));
			for (int i = 1; i < pow(2.0, NTA + 1); i++)
				cin >> tree[i];
			if(DFS(tree, 1, 0))
				cout << "Valid" << endl;
			else
				cout << "Invalid" << endl;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值