动态规划——出差问题

动态规划题目,参照原来题目

// Study.cpp: 定义控制台应用程序的入口点。
//

#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

char x;
int step;
bool canWin(vector<vector<vector<int>>> &m,int a,int b,int c)
{
	int sum = a + b + c;
	if (sum == 1)
	{
		m[a][b][c] = 0;
		return false;
	}
	else if (sum == a || sum == b || sum == c || sum == 2)
		return true;

	for (int i = 1; i <= a; i++)
		if (m[a-i][b][c] == 0 || (m[a-i][b][c] == -1 && !canWin(m,a - i, b, c)) )
		{
			x = 'A';
			step = i;
			m[a][b][c] = 1;
			return true;
		}

	for (int i = 1; i <= b; i++)
		if (m[a][b-i][c] == 0 || (m[a][b-i][c] == -1 && !canWin(m,a, b-i, c)) )
		{
			x = 'B';
			step = i;
			m[a][b][c] = 1;
			return true;
		}
	for (int i = 1; i <= c; i++)
		if (m[a][b][c-i] == 0 || (m[a][b][c-i] == -1 && !canWin(m,a , b, c-i)) )
		{
			x = 'C';
			step = i;
			m[a][b][c] = 1;
			return true;
		}

	m[a][b][c] = 0;
	return false;
}
int main()
{
	int a, b, c;
	cin >> a >> x >> b >> x >> c;
	vector<vector<vector<int>>> m(a+1, vector<vector<int>>(b+1, vector<int>(c+1, -1)));
	//cout << canWin(a, b, c) << endl;
	if (canWin(m,a, b, c))
	{
		cout << x << "," << step << endl;
	}
	else
		cout << 1 << endl;
	system("pause");
	return 0;
}

  

 

posted @ 2018-08-24 14:37  何许  阅读(249)  评论(0编辑  收藏  举报