Gym 100095 Problem - Black Square

6 篇文章 0 订阅

Gym 100095 Problem - Black Square

原题链接

题目类型:思维、条件分支判断
题意

有一个m x n 大小的网格矩阵,其中有一个 s x s 大小的正方形区域中的字符为 ‘*’,其余区域为符号 ‘.’。 现在只给出其中的第 k 行,判断是否可以推出原来矩阵可能的状态,如果不行则输出 impossible,如果只有一种可能则输出 unique,如果有多种可能则输出 Ambiguous

分析

从给定的哪一行入手判断,分为两种情况:
1、全为 '.'
则表示黑色矩阵只可能在第 k 行的上半部分或下半部分,如果两个部分都放不下黑色矩阵则不存在解。然后判断是唯一解还是多解,多解的情况就是黑色矩阵在两个部分中可以左右或者上下移动,然后对 4 种情况进行考虑和判断。

2、存在 '*'
需要注意连续的 ‘*’ 的个数一定要等于 ‘*’ 的总数和 s,否则一定不存在解。当 s 为一的时候或 k1m 的时候一定是唯一解。因为 ‘*’ 的位置是确定的,所以不存在左右移动的情况,所以考虑是否可以上下移动,如果可以则有多解,否则唯一解。

具体细节参考代码。

代码
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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值