hdu2147 kiki's game(巴什博弈java)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40693171/article/details/82874148

题目链接
kiki’s game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others)
Total Submission(s): 13497 Accepted Submission(s): 8238

Problem Description
Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes the checkerboard game.The size of the chesserboard is n*m.First of all, a coin is placed in the top right corner(1,m). Each time one people can move the coin into the left, the underneath or the left-underneath blank space.The person who can’t make a move will lose the game. kiki plays it with ZZ.The game always starts with kiki. If both play perfectly, who will win the game?

Input
Input contains multiple test cases. Each line contains two integer n, m (0<n,m<=2000). The input is terminated when n=0 and m=0.

Output
If kiki wins the game printf “Wonderful!”, else “What a pity!”.

Sample Input
5 3
5 4
6 6
0 0

Sample Output
What a pity!
Wonderful!
Wonderful!
意思是一个棋子只能往左面,下面,或者左下走。不能走的那个输。kiki先走。
分析一下走法:

O O O O O O O O 
O O O O O O O O 
O O O O O O O O 
O O O O O O O O 
O O O O O O O O 
O O O O O O O O 
O O O O O O O O 
O O O O O O O O  

NNNNNNNN
PNPNPNPN
NNNNNNNN
PNPNPNPN
NNNNNNNN
PNPNPNPN
NNNNNNNN
PNPNPNPN

先考虑走直线,不难发现:如果走直线,向左,向下。从左下角开始找P/N点。画出图。

  • 必败点(P点) :前一个选手(Previous player)将取胜的位置称为必败点。
  • 必胜点(N点) :下一个选手(Next player)将取胜的位置称为必胜点。
    最后一个点(左下角必败),因为他的前一个人走到该点他就输了。
    相反,他的附近三个点都是必胜点,因为这个人走一步就赢了。
    所以分析pn图可以发现(n%2==0)或者(m%2= =0)都是 必胜的。
    附上代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class hdu2147 {

	public static void main(String[] args) throws IOException {
		StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 		
		 while(in.nextToken()!=StreamTokenizer.TT_EOF)
		 {
			 int n=(int)in.nval;
			 in.nextToken();
			 int m=(int)in.nval;
			 if(m==0&&n==0) {break;}
			 if(m%2==0||n%2==0)
			 {
				 out.println("Wonderful!");
			 }
			 else
				 {out.println("What a pity!");}
			 out.flush();			 
		 }
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40693171/article/details/82874148