Blue Bridge Cup the previous questions about the number of multiple selected card (Game)

Title Description
spare time, Holmes and Watson to play a game:

On the N cards that read N integers. They turn away a card. The next person to take the required number must be a divisor or multiplier A man holding numbers. For example, written on a particular digital Holmes took the card "6", the next Watson can get the figures include:

1,2,3, 6,12,18,24 …

When it came either take the card, the card does not meet the requirements Alternatively, the input side to side.

Please you do the math, at which all known digital numbers and optional on the card, how to choose in order to ensure victory to take advantage of the computer!

When selecting a plurality of numbers are win, wherein the output of the lowest number. If the input anyway, -1 is output.

2 input data row. The first line is a number of space-separated integers (each an integer ranging from 1 to 100), indicating that all current remaining cards.
The second row is a number of space-separated integers, represents a selected digital. Of course, the numbers in the second row must be entirely contained in a first row numbers.

Program the output winning tactics! !

For example:
a user input:
2. 6 3
3. 6
the application should output:
3

Another example:
a user input:
. 1. 5 2 2 4. 3. 3
. 3 4. 5
the application should Output:
4

Resources for:
peak memory consumption <64M
the CPU consumption <1000ms

Thinking: n up to 100, first pre-processing all the cards can be selected with a vector array, then all the cards of the first step can be sorted out traversal search, If a player can win, and direct output break.
Note: When the input is used to handle the stringstream strings (header file is ssstream), concrete can Baidu.

#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
#define N 100005
int num[N];
int n;
vector<int> b;
vector<int> a[105];
int dfs(int x)    //在上一张选x的情况下
{
	for(int i=a[x].size()-1;i>=0;i--) //遍历选了x之后能选的牌
	{
		if(num[a[x][i]])
		{
			num[a[x][i]]--;
			int t=dfs(a[x][i]);   //若我选了这张牌以后对方会输,返回1(自己可以赢)
			num[a[x][i]]++;
			if(t==-1)
				return 1;
		}
	}
	return -1;  //若无牌可选或者选任一能选的牌之后对方都会赢,返回-1(自己会输)
}
int main()
{
    string s;
    int x;
    getline(cin,s);
    stringstream in(s);
    while(in>>x)
    {
        num[x]++;
    }
    int ans=-1;
    getline(cin,s);
    stringstream iin(s);
    while(iin>>x)
    {
        b.push_back(x);
    }  
    for(int i=1;i<=100;i++)
    {
        if(num[i])
        {
            num[i]--;
            for(int j=1;j<=100;j++)
            {
                if(num[j] && (i%j==0 || j%i==0))
                    a[i].push_back(j);
            }
            num[i]++;
        }
    } 
    sort(b.begin(),b.end());  //先排序再遍历,因为如果有两张牌都能赢,需输出小的那个
    for(int i=0;i<b.size();i++)
    {
        num[b[i]]--;
        int t=dfs(b[i]);
        if(t==-1)
        {
            cout<<b[i]<<endl;
            return 0;
        }
        num[b[i]]++;
    }
    cout<<"-1"<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43693379/article/details/90415981