Title: Alphabet Game
Xiao Ming is often addicted to playing LOL games. Once he wanted to challenge Master K, but Master K said:
"Let's play a game of filling in letters in the blanks first. If you can't beat me, don't play LOL again."
Master K drew a line of n grids on the paper, and asked Xiaoming and him to alternately fill in letters in them.
And:
1. When it's someone's turn to fill in, they can only fill in L or O in a certain space
2. Whoever makes the letter "LOL" first wins.
3. If all the squares are filled and still can't form LOL, then it will be a draw.
Xiao Ming has failed several times in the experiment. He is very ashamed. I hope you can use the computer to help him solve this mystery.
The input format of this question is:
the first line, the number n (n<10), indicating that there are n initial situations below.
Next, n lines, each with a string, represent the starting position.
For example: "******", which means there are 6 spaces.
"L****", which means there is a letter L on the left and 4 spaces on the right.
It is required to output n numbers, indicating that for each position, if Xiao Ming fills in first, when Master K always uses the strongest move, Xiao Ming's best result. 1 means
win -1 means must
lose 0 means
draw _ _
1
1
Resource convention:
Peak memory consumption < 256M
CPU consumption < 1000ms
Please output strictly according to the requirements, and do not superfluous to print redundant content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Note: Do not use the package statement. Do not use features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.
package Main; /** Tie Game Problem: f (position x) { t = lose for(all possible moves){ try x --> y if f(y)= ? { lose ==> return win flat ==> t = flat } backtracking } return t } */ import java.util.Scanner; public class Alphabet Game { static int f(char [] c) { String s=new String(c); if(s.contains("LOL")) return -1; if(s.contains("*")==false) return 0;//Do not leak the exit boolean ping=false;//Assume that it cannot be tied for(int i=0;i<c.length;i++) { if(c[i]=='*') { try { c[i]='L';//Try it out if(f(c)==-1)//If f(y) loses, return to win return 1; else if(f(c)==0) ping=true; //Can't return 0 directly, otherwise no further testing can be done c[i]='O'; if(f(c)==-1)//If f(y) loses, return to win return 1; else if(f(c)==0) ping=true; } finally { c[i]='*';//Backtrack } } } if(ping) return 0; return -1; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- != 0) { String s = in.next(); System.out.println(f(s.toCharArray())); } } }