题意:两个人有各有一手牌,每人同时出最上面的那张牌,比较大的那个人可以吃掉对方牌同时把自己的一起放到最后,问最后谁能(让对方空牌)或者谁都不会赢 |
思路:
很直观的一个队列题,每次双方都取出队列顶部元素,同时pop掉,然后进行判断,将两张牌push按对手牌先入队列的方式push进大的一方队尾,重复这个操作,看谁空了就好了。
因为可能会一直这样进行下去,所以就设置turn回合大于某个特定大的数就break掉即可。
#include <iostream>
#include <vector>
#include <cstdio>
#include <map>
#include <climits>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#define maxn 105
using namespace std;
typedef long long ll;
int main()
{
int n;
queue<int> a;
queue <int> b;
cin>>n;
int len;
cin>>len;
for(int i=1;i<=len;i++)
{
int tmp;
cin>>tmp;
a.push(tmp);
}
cin>>len;
for(int i=1;i<=len;i++)
{
int tmp;
cin>>tmp;
b.push(tmp);
}
int turn = 0;
while(!a.empty()&&!b.empty())
{
int one = a.front();
a.pop();
int other = b.front();
b.pop();
// cout<<one<<' '<<other<<endl;
if(one>other)
{
a.push(other);
a.push(one);
}
else
{
b.push(one);
b.push(other);
}
turn ++;
if(turn>1e6)
break;
}
if(a.empty())
printf("%d %d\n",turn,2);
else if(b.empty())
printf("%d %d\n",turn,1);
else
printf("-1\n");
return 0;
}