赞
踩
import java.util.Scanner; public class 填字母游戏 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while (n-- != 0) { char[] chs = sc.nextLine().toCharArray(); int ans = dfs(chs); System.out.println(ans); } } private static int dfs(char[] chs) { String s = new String(chs); if (s.contains("LOL")) return -1; if (!s.contains("*")) return 0; int ans = -1; int ping = -1; for (int i = 0; i < s.length(); i++) {// 选择落子的位置 if (chs[i] == '*') { // 试填L chs[i] = 'L'; int temp = dfs(chs); if (temp == -1) { chs[i] = '*'; return 1; } else if (temp == 0) ping = 1; else ans = -1; // 试填O chs[i] = 'O'; temp = dfs(chs); if (temp == -1) { chs[i] = '*'; return 1; } else if (temp == 0) ping = 1; else ans = -1; chs[i] = '*'; } } if (ping == 1) return 0; return ans; } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class 填字母游戏 { static Map<String, Integer> map = new HashMap<String, Integer>(); public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while (n-- != 0) { char[] chs = sc.nextLine().toCharArray(); int ans = dfs(chs); System.out.println(ans); } } private static int dfs(char[] chs) { String s = toS(chs); if (map.containsKey(s)) return map.get(s); if (s.contains("LOL")) return -1; if (!s.contains("*")) return 0; int ans = -1; int ping = -1; for (int i = 0; i < s.length(); i++) {// 选择落子的位置 if (chs[i] == '*') { // 试填L chs[i] = 'L'; int temp = dfs(chs); map.put(toS(chs), temp); if (temp == -1) { chs[i] = '*'; return 1; } else if (temp == 0) ping = 1; else ans = -1; // 试填O chs[i] = 'O'; temp = dfs(chs); map.put(toS(chs), temp); if (temp == -1) { chs[i] = '*'; return 1; } else if (temp == 0) ping = 1; else ans = -1; chs[i] = '*'; } } if (ping == 1) { map.put(s, 0); return 0; } map.put(s, ans); return ans; } private static String toS(char[] chs) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < chs.length; i++) { sb.append(chs[i]); } return sb.toString(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。