1. 程式人生 > >題解報告:hdu 1564 Play a game(找規律博弈)

題解報告:hdu 1564 Play a game(找規律博弈)

正整數 lse CI character square placed eight 技術 分享

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

Problem Description New Year is Coming! ailyanlu is very happy today! and he is playing a chessboard game with 8600.The size of the chessboard is n*n. A stone is placed in a corner square. They play alternatively with 8600 having the first move. Each time, player is allowed to move the stone to an unvisited neighbor square horizontally or vertically. The one who can‘t make a move will lose the game. If both play perfectly, who will win the game?
譯文:新年快到了!ailyanlu今天很開心!他正在玩8600棋盤遊戲。棋盤的大小是n * n。一塊石頭被放置在角落廣場。8600有先移動的機會。每次玩家都可以將石頭水平或垂直移動到未訪問的鄰居廣場。無法采取行動的人將失去這場比賽。如果兩者都完美發揮,誰會贏得比賽?
Input The input is a sequence of positive integers each in a separate line.
The integers are between 1 and 10000, inclusive,(means 1 <= n <= 10000) indicating the size of the chessboard. The end of the input is indicated by a zero. 譯文:輸入是一系列正整數,每個單獨一行。整數在1到10000之間(表示1 <= n <= 10000),表示棋盤的大小。輸入的結尾用零表示。 Output Output the winner ("8600" or "ailyanlu") for each input line except the last zero. No other characters should be inserted in the output. 譯文:輸出每個輸入行的勝者(“8600”或“ailyanlu”),除了最後一個零。輸出中不應插入其他字符。
Sample Input 2 0 Sample Output 8600 解題思路:找規律博弈。題目的意思就是輪到的人只能上或下或左或右移1格到未訪問的格子,並且兩者完美發揮,最後無法移動的人將輸掉比賽。 舉3個栗子:①當n=1(奇數)時,先手不能移動,則後手必贏; ②當n=2(偶數)時,兩者移動如圖所示(都是最優策略):易得先手必贏 技術分享圖片 ③當n=3(奇數)時,兩者移動如圖所示(都是最有策略):易得後手必贏 技術分享圖片 再舉多個例子的過程中我們可以發現:當n為奇數時,後手只要按照螺旋線的走法(最優策略),最後一步必到中心方格,此時先手無法再移動,即後手必贏;當n為偶數時,同樣,按照螺旋線的走法(最優策略),最後一步必輪到先手,此時後手無法移動,即先手必贏。
因此,可得出結論:當n為奇數時,“8600”先手必贏;反之,“ailyanlu”後手必贏。 AC代碼:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n;
 6     while(cin>>n && n){
 7         if(n%2==0)cout<<"8600"<<endl;//先手必贏
 8         else cout<<"ailyanlu"<<endl;//後手必贏
 9     }
10     return 0;
11 }

題解報告:hdu 1564 Play a game(找規律博弈)