CodeForces - 546C Soldier and Cards(模拟)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 546C Soldier and Cards(模拟)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:兩個人在玩游戲,初始時兩個人分別有一定數量的牌,牌面的大小一定是互不相同的,游戲規則如下:
每次兩個人同時從自己牌堆的最頂端取出一張牌,我們可以記做a和b,比較一下其大小,牌面更大的一方獲勝,勝者可以先將對面的牌a放到自己牌堆的下面,然后再將自己b放到牌a下面,如此往復,牌堆先為空的人即輸掉了游戲
還有一種情況就是兩個人在游戲過程中會出現循環的情況,此時游戲視為平局,輸出-1
題目分析:簡單模擬,只是單純的覺得題面挺有意思的,就是英文題面不太友好。。今晚是小師弟給我講的題意我才實現了的,開兩個隊列分別模擬兩個人的牌,然后一直循環進行游戲即可,因為會有循環的情況出現,這里給出兩種方法都可以實現:
我是為了追求完美起見,用了vis進行判重,并且配合了一個mp函數簡化了調用make_pair()時的代碼量,也算是比較好看的了。。
我看有些老哥是判斷游戲步數大于1000時直接跳出循環,這樣做可以大大減少空間和時間(map的logn的時間開銷),就是有時候可能會先WA上幾發(隨緣過題法)
廢話不多說了,直接上代碼吧:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=25;map<pair<queue<int>,queue<int> >,bool>vis;queue<int>q1,q2;pair<queue<int>,queue<int>> mp(queue<int>a,queue<int>b) {return make_pair(a,b); } int main() {int n;scanf("%d",&n);int cnt;scanf("%d",&cnt);while(cnt--){int num;scanf("%d",&num);q1.push(num);}scanf("%d",&cnt);while(cnt--){int num;scanf("%d",&num);q2.push(num);}bool flag=false;int ans=0;vis[mp(q1,q2)]=true;while(!q1.empty()&&!q2.empty()){ans++;int a=q1.front();int b=q2.front();q1.pop();q2.pop();if(a>b){q1.push(b);q1.push(a);}else{q2.push(a);q2.push(b);}if(vis[mp(q1,q2)])break;vis[mp(q1,q2)]=true;}if(q1.size()&&q2.size())printf("-1\n");else if(!q1.empty())printf("%d %d\n",ans,1);elseprintf("%d %d\n",ans,2);return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 546C Soldier and Cards(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU - 2176 取(m堆)石子游戏
- 下一篇: PAT (Basic Level) 10