atcoder ABC 354-E题

atcoder ABC 354-E题

Problem Statement

Takahashi and Aoki are playing a game using Ncards. The front side of the i-th card has Ai​written on it, and the back side has Bi​ written on it. Initially, the N cards are laid out on the table. With Takahashi going first, the two players take turns performing the following operation:

Choose a pair of cards from the table such that either the numbers on their front sides are the same or the numbers on their back sides are the same, and remove these two cards from the table. If no such pair of cards exists, the player cannot perform the operation.
The player who is first to be unable to perform the operation loses, and the other player wins. Determine who wins if both players play optimally.

Constraints

1≤N≤18
1≤Ai​,Bi​≤109
All input values are integers.

Input

The input is given from Standard Input in the following
format:

N
A1​ B1​
A2​ B2​

AN​ BN​

Output

Print Takahashi if Takahashi wins when both players play optimally, and Aoki otherwise.

Sample Input 1

5
1 9
2 5
4 9
1 4
2 5

Sample Output 1

Aoki
If Takahashi first removes

the first and third cards: Aoki can win by removing the second and fifth cards.

the first and fourth cards: Aoki can win by removing the second and fifth cards.

the second and fifth cards: Aoki can win by removing the first and third cards.

These are the only three pairs of cards Takahashi can remove in his first move, and Aoki can win in all cases. Therefore, the answer is Aoki.

Sample Input 2

9
3 2
1 7
4 1
1 8
5 2
9 8
2 1
6 8
5 2

Sample Output 2

Takahashi

思路分析:

使用一个struct的vector来存储卡牌的正反面,再用一个vector来标记已经删除的卡牌,再用一个bool的常量来决定输出的是“Takahashi”还是“Aoki”。
此代码提交后过了11个测试点,样例都过了,如果有佬看见在评论区或者私信我帮我改一下,十分感谢。

code:

#include 
#include 
using namespace std;
struct node{
    int a,c;
};
int n;
bool b=false;
int main(int argc, const char * argv[]) {
    cin>>n;
    vector<node>v(n);
    vector<bool>vi(n,false);
    for(int i=0;i<n;i++){
        cin>>v[i].a>>v[i].c;
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            if(i!=j&&(v[i].a==v[j].a)||(v[i].c==v[j].c)&&vi[i]==false&&vi[j]==false){
                vi[i]=true;
                vi[j]=true;
                b=true;
                if(i!=j&&(v[i].a==v[j].a)||(v[i].c==v[j].c)&&vi[i]==false&&vi[j]==false){
                    vi[i]=true;
                    vi[j]=true;
                    b=false;
                }
                else break;
            }
            else break;
        }
    if(!b){
        cout<<"Aoki"<<endl;
    }
    else if(b){
        cout<<"Takahashi"<<endl;
    }
    return 0;
}

你可能感兴趣的:(算法,c++)