2017 8th Blue Bridge Cup Java Programming Undergraduate Group B Final Alphabet Filling Game (Programming Topics)

2017 8th Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90184941

 

Question 5

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 letters form the word "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 can win
-1 means must lose
0 means can tie


For example,
enter:
4
***
L**L
L**L***L
L*****L

Then the program should output:
0
-1
1
1

Resource convention:
peak memory consumption < 256M
CPU consumption < 1000ms


Please output strictly according to the requirements, and do not superficially print superfluous 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.


Not a formal solution ah ah ah ah ah ah

Violent search method: Because both parties will take the best step, the process of filling in letters is simulated recursively according to the best step of both parties. If the two parties can still win when both parties are optimal, it means that they must win. Therefore, it has been recursively searching whether there is a must-win situation. If it does not exist, a draw is considered, and if the draw is not successful, -1 is returned to indicate a must-lose. Obviously there will be a timeout, but the line of thought can be learned.

20 minute timeout code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;


public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n;
    public static String s;

    public static void main(String[] args) {
        n = in.nextInt();
        while (n-- > 0) {
            s = in.nextLine();
            out.println(isWin(s));
            out.flush();
        }
        out.close();
    }

    static int isWin(String now) {//轮到当前人来填
        if (now.contains("LOL")) return -1;//当前人还没填,串里面就已经存在LOL了,则当前人输了
        if (now.contains("*") == false) return 0;//没有可以填的格子了且没有LOL子串,平局
        char[] tmp = now.toCharArray();
        int len = now.length(), ping = 0;
        for (int i = 0; i < len; i++) {
            if (now.charAt(i) == '*') {//当前位置可以填
                tmp[i] = 'L';//先填一个'L'
                switch (isWin(new String(tmp))) {
                    case -1:
                        return 1;//下一个人填的时候输了,则表示当前人赢了
                    case 0:
                        ping = 1;//平局,先不直接返回,继续看有没有机会可以赢
                }
                //当填'L'的情况下,下一个人会赢则先不直接返回当前人输,因为当前人是可以在该位置不填'L'而填'O'的,或者不在该位置操作(进入下一次i循环)
                tmp[i] = 'O';
                switch (isWin(new String(tmp))) {
                    case -1:
                        return 1;
                    case 0:
                        ping=1;
                }
                //不在该位置进行操作
                tmp[i] = '*';
            }
        }
        if (ping == 1) return 0;
        return -1;//上面所有可能性都不能得到赢或者平,则当前人必输
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526090&siteId=291194637