Gym 100095 Problem - Black Square
题目类型:思维、条件分支判断
题意
有一个m x n 大小的网格矩阵,其中有一个 s x s 大小的正方形区域中的字符为 ‘*’,其余区域为符号 ‘.’。 现在只给出其中的第 k 行,判断是否可以推出原来矩阵可能的状态,如果不行则输出 impossible,如果只有一种可能则输出 unique,如果有多种可能则输出 Ambiguous。
分析
从给定的哪一行入手判断,分为两种情况:
1、全为 '.'
则表示黑色矩阵只可能在第 k 行的上半部分或下半部分,如果两个部分都放不下黑色矩阵则不存在解。然后判断是唯一解还是多解,多解的情况就是黑色矩阵在两个部分中可以左右或者上下移动,然后对 4 种情况进行考虑和判断。
2、存在 '*'
需要注意连续的 ‘*’ 的个数一定要等于 ‘*’ 的总数和 s,否则一定不存在解。当 s 为一的时候或 k 取 1 或 m 的时候一定是唯一解。因为 ‘*’ 的位置是确定的,所以不存在左右移动的情况,所以考虑是否可以上下移动,如果可以则有多解,否则唯一解。
具体细节参考代码。
代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static final int MAX_N = 100010;
static final int INF = 0x3f3f3f3f;
static final int mod = 1000000007;
public static void main(String[] args) throws IOException {
initReader(System.in);
solve();
printWriter.flush();
}
/*******************************************************************************************************************************/
public static void solve() throws IOException {
int m = nextInt();
int n = nextInt();
int s = nextInt();
int k = nextInt();
String ss = next();
if (ss.length() != n) {
printWriter.println("Impossible");
return;
}
// 点的个数
int cnt1 = 0;
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) == '.') {
cnt1++;
}
}
// 全是点
if (cnt1 == n) {
if (s == m) {
printWriter.println("Impossible");
return;
}
int top = k - 1, down = m - k;
int cnt = 0;
if (top == s) {
cnt++;
}
if (top > s) {
cnt += 2;
}
if (down == s) {
cnt++;
}
if (down > s) {
cnt += 2;
}
if (cnt != 0 && n > s) {
cnt += 2;
}
if (cnt > 1) {
printWriter.println("Ambiguous");
} else if (cnt == 1) {
printWriter.println("Unique");
} else {
printWriter.println("Impossible");
}
return;
}
int cnt2 = 0;
int cnt3 = 0;
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) == '*') {
cnt2 = 1;
while (i + 1 < ss.length() && ss.charAt(i + 1) == '*') {
i++;
cnt2++;
}
break;
}
}
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) == '*') {
cnt3++;
}
}
if (cnt2 != cnt3 || cnt2 != s) {
printWriter.println("Impossible");
return;
}
if (s == 1 || k == 1 || k == m) {
printWriter.println("Unique");
return;
}
if (m > s) {
printWriter.println("Ambiguous");
} else if (m == s) {
printWriter.println("Unique");
} else {
printWriter.println("Impossible");
}
}
/*******************************************************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter printWriter;
public static void initReader(InputStream input) throws IOException {
// reader = new BufferedReader(new InputStreamReader(input));
// tokenizer = new StringTokenizer("");
// printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
reader = new BufferedReader(new FileReader("black.in"));
tokenizer = new StringTokenizer("");
printWriter = new PrintWriter(new BufferedWriter(new FileWriter("black.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}