首页 > ACM题库 > HDU-杭电 > HDU 3827-The Killers of Two Kingdoms[解题报告]HOJ
2015
04-13

HDU 3827-The Killers of Two Kingdoms[解题报告]HOJ

The Killers of Two Kingdoms

问题描述 :

Due to the popular strategy card game “The Killers of Three Kingdom”, iSea detects the immense market of this game kind, so he decides to SHANZHAI a similar game, The Killers of Two Kingdom.

In this game, there are four kinds of card:

1.Kill: make the enemy loss one unit of life
2.Evade: evade the hurt of “Kill” from enemy, i.e. no loss of life
3.Snitch: steal one card from the enemy if he/she has
4.Defend: stay yourself away from “Snitch” of enemy, i.e. no loss of card

The game is played by only two players; all own three units of life in the beginning. At first, there are N cards in the original pile. Before the rounds begin, the first player takes four cards, then then second player take four cards, and both them will always keep the cards exactly in the order they get them, including the snitched card. There is no limit of card’s number for the players.

While playing the game, two players make an operation in turn, from the first player. As they are stupid AI written by iSea, both of them will take the same strategy.
The one who is in his round will act in this order:

1.Get min {2, the number of left cards} cards first
2.Check if he/she still has “Snitch” Card, use it until none left or can’t use, and try to snitch the first card of the enemy
3.Check if he/she has “Kill” Card, use the first “Kill” card if exists, he/she can use at most one “Kill” in each round

The other one will response in this way:

1.If the enemy play “Snitch” and he/she has “Defend”, use the first one
2.If the enemy play “Kill” and he/she has “Evade”, use the first one

The game ends when one of the played is dead, means own zero unit of life, or when no more cards in the original pile and both of the players can’t play card any more. Once a card had been played, it won’t be used again.
After inventing the rule, iSea find the game is extraordinary boring to play, so he don’t want to play it even only once, try to simulate the game and show him who is the winner or just a tie.

输入:

The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the number of cards.
Then N Cards’ description following, each of them will be one of the four kinds of cards according to the rules, short for “K”, “E”, “S” and “D”.

Technical Specification

1. 1 <= T <= 50
2. 8 <= N <= 100

输出:

The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the number of cards.
Then N Cards’ description following, each of them will be one of the four kinds of cards according to the rules, short for “K”, “E”, “S” and “D”.

Technical Specification

1. 1 <= T <= 50
2. 8 <= N <= 100

样例输入:

2
8
K K K E K K S S
10
K K S S K K D D E E

样例输出:

Case 1: The winner is the second.
Case 2: Just a tie.

#include<string.h>
#include<stdio.h>


char ch[201];
int xue[3] , sta[201] , n , flag = 1; 

void init()
{
 int i; char t;
 scanf("%d\n", &n);
 for (i=1;i<=n;i++)
 {
 scanf("%c ", &ch[i]);
 sta[i] = 0;
 } 
 for (i=1;i<=4;i++) sta[i] = 1;
 for (i=5;i<=8;i++) sta[i] = 2;
 xue[1] = 3; xue[2] = 3;
}


int find_need(char now , int x)
{
 int i;
 for (i=1;i<=n;i++)
 if (ch[i] == now && sta[i] == x) return i;
 return 0;
}

int find(int x)
{
 int i;
 for (i=1;i<=n;i++)
 if (sta[i] == x) return i;
 return 0; 
}


void deal()
{
 int i , j , t , x , tail , m , now = 0;
 flag = 1; tail = 8; m = n;
 while (1)
 {
 now++;
 if (now > m+1) break;
 if (tail + 1 <= m) sta[tail+1] = flag;
 if (tail + 2 <= m) sta[tail+2] = flag;
 if (tail+2 <= m) tail = tail + 2; else tail = m;
 
 
 for (j=1;j<=n;j++)
 if (ch[j] == 'S' && sta[j] == flag)
 {
 sta[j] = 3;
 t = find_need('D' , 3-flag);
 if (t == 0) 
 {
 x = find(3-flag); 
 if (x == 0) continue;
 sta[x] = 3;
 n++; 
 sta[n] = flag;
 ch[n] = ch[x];
 }
 else sta[t] = 3;
 }
 
 for (j=1;j<=n;j++)
 if (ch[j] == 'K' && sta[j] == flag)
 {
 sta[j] = 3;
 t = find_need('E' , 3-flag);
 if (t == 0)
 {
 xue[3-flag] --;
 if (xue[3-flag] < 1) 
 {
 if (flag == 1) printf("The winner is the first.\n"); 
 else printf("The winner is the second.\n"); 
 return;
 }
 }
 else sta[t] = 3; 
 break; 
 }
 flag = 3-flag;
 }
 printf("Just a tie.\n");
}

int main()
{
 
 int i , tot;
 scanf("%d", &tot);
 for (i = 1; i<= tot;i++)
 {
 printf("Case %d: " , i); 
 init();
 deal(); 
 }
 //while(1);
}

  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.