HDU4387 Stone Game

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4387

                                                        Stone Game


Problem Description

  Alice and Bob are playing a game. It is played in 1*N grids. Each grid can be occupied by one stone. Alice has K white stones on the left (numbered 1 to K from left to right), and Bob has K black stones on the right (numbered 1 to K from right to left). They take turns to move their own stones, and Alice moves first. In each move, the player must choose one stone to move to the nearest empty grid forward (Alice moves to the right, Bob moves to the left). If one player cannot find any stone to move, he wins.
  Now Alice asks you to find a winning strategy of the game. Can you help him?


 

Input

  There are multiple test cases. In each case, there is one line containing two integers N, K.

Technical Specification
  3 <= N <= 1,000,000, 1 < K*2 < N
 

Output

  For each case, print in one line containing the case number (starting with 1) and the winning information. If Alice loses, just print “Bob”, otherwise print “Alice” and the first stone he chooses. If there are multiple stones he can choose, he will choose the rightmost one.
 

Sample Input
3 1 4 1
 

Sample Output
Case 1: Bob Case 2: Alice 1
 
題意:
  AB兩個人玩遊戲,給出n,d,n代表格子數,d代表AB各擁有的棋子數,A只能往右移動,B只能往左移動,而且AB只能移動到最靠近自己的第一個空上,A先移動,若誰先移動到不能移動爲止誰就獲勝,如果A贏了,輸出他第一顆移動的棋子;

題解:
   移動到不能移動爲止,即最終狀態,A都在右邊,B都在左邊。分類討論:
  1、d=1,即AB都只有一顆棋子,若n爲偶數,A贏,若n爲奇數,B贏;
  2、d>1,此時的原則是誰在中間n-2d的空中佔的棋子數多誰就贏,因爲A先走,所以不管奇數還是偶數個空,都是A贏
    只要討論A走的第一顆棋子就行了:
                           (n-2d)=1,A移動d位子上的棋子爲最優方案;
                           否則,A移動第一顆棋子爲最優方案;

代碼如下:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,d,k=1;
    while(~scanf("%d%d",&n,&d))
    {
    	if(d==1 && n%2!=0) printf("Case %d: Bob\n",k++);
    	else
	    {
	                int kk=n-2*d;
			if(kk==1)printf("Case %d: Alice %d\n",k++,d);
			else printf("Case %d: Alice 1\n",k++);	
	    }
    }
    return 0;
}
























發佈了48 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章