CCF 201609-3 爐石傳說
阿新 • • 發佈:2018-12-09
思路: 1. 兩個玩家,每個玩家用一個向量 vector 表示,如果隨從死了,刪除對應隨從,vector 自動更新;如果用陣列,判斷更新會比較麻煩。 2. 依次判斷每行第一個字串,按遊戲規則作相應處理即可。注意英雄提前掛掉的情況。 3. 遇到字串”end” 切換玩家,0表示第一玩家,1表示第二玩家,則 player = (++player)%2 即可切換玩家。文中多處用取模方法來切換玩家,比 if 判斷方便得多。當前玩家是 nowplayer,則對方就是 (nowplayer + 1)%2。 程式碼如下:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;
struct sommen
{
int position;
int attack;
int health;
sommen(){position = 0; attack = 0; health = 0;}
sommen(int p, int a, int h):position(p), attack(a), health(h){}
};
int main()
{
//freopen("game.txt", "r", stdin);
vector<sommen> game[2]; //game[0]表示玩家一,game[1]玩家二
int n;
int nowplayer = 0;
game[0].push_back(sommen(0, 0, 30));
game[1].push_back(sommen(0, 0, 30));
int winner = 0;
cin >> n;
cin.get();
for(int numline = 0; numline < n; numline++) //依次讀入指令,按規則處理即可
{
string line, action;
int arg1(0), arg2(0), arg3(0);
getline(cin, line);
stringstream s(line);
s >> action;
if(action == "summon")
{
s >> arg1 >> arg2 >> arg3;
game[nowplayer].insert(game[nowplayer].begin() + arg1, sommen(arg1, arg2, arg3));
for(int i = 1; i < game[0].size(); i++)
game[0][i].position = i;
for(int i = 1; i < game[1].size(); i++)
game[1][i].position = i;
}
if(action == "attack")
{
s >> arg1 >> arg2;
game[nowplayer][arg1].health -= game[(nowplayer+1)%2][arg2].attack;
game[(nowplayer+1)%2][arg2].health -= game[nowplayer][arg1].attack;
if(game[nowplayer][arg1].health <= 0)
{
if(arg1 == 0) //自己的英雄掛了
{
winner = nowplayer==0?-1:1;
break;
}
else //掛掉的是隨從
game[nowplayer].erase(game[nowplayer].begin() + arg1);
}
if(game[(nowplayer+1)%2][arg2].health <= 0)
{
if(arg2 == 0) //對方的英雄掛了
{
winner = nowplayer==0?1:-1;
break;
}
else
game[(nowplayer+1)%2].erase(game[(nowplayer+1)%2].begin() + arg2);
}
//更新各個隨從game[s][i]的位置
for(int i = 1; i < game[0].size(); i++)
game[0][i].position = i;
for(int i = 1; i < game[1].size(); i++)
game[1][i].position = i;
}
if(action == "end") //切換當前玩家
nowplayer = (++nowplayer)%2;
}
//以下為處理輸出
cout << winner << endl;
cout << game[0][0].health << endl;
int i;
for(i = 0; i < game[0].size(); i++);
cout << i-1 << " ";
for(i = 1; i < game[0].size(); i++)
cout << game[0][i].health << " ";
cout << endl;
cout << game[1][0].health << endl;
for(i = 0; i < game[1].size(); i++);
cout << i-1 << " ";
for(i = 1; i < game[1].size(); i++)
cout << game[1][i].health << " ";
return 0;
}