zoukankan      html  css  js  c++  java
  • 动态规划——出差问题

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

    // 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;
    }
    

      

  • 相关阅读:
    [HAOI2015]树上操作
    虚树入门笔记
    多重背包问题(三阶段)
    树链剖分(轻重链)
    安卓开发中Theme.AppCompat.Light的解决方法
    dumpsys命令用法
    vim实现全选功能
    java中fail-fast 和 fail-safe的区别
    基本数据类型的装箱和拆箱()优先使用基本数据类型
    hexo github pages 搭建博客
  • 原文地址:https://www.cnblogs.com/Oscar67/p/9529758.html
Copyright © 2011-2022 走看看