1 / 12
May 2024
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;


class IslandUnderAttack {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());

		for (int cs = 1; cs <= T; cs++) {
			String[] wd = br.readLine().trim().split(" ");
			int n = Integer.parseInt(wd[0]);
			int m = Integer.parseInt(wd[1]);
			int[][] dist = new int[n][m];
			char[][] map = new char[n][m];
			int shelter = 0;
			int humans = 0;
			for (int i = 0; i < n; i++) {
				char[] inp = br.readLine().trim().toCharArray();
				for (int j = 0; j < inp.length; j++) {
					map[i][j] = inp[j];
					if (Character.isDigit(inp[j])) {
						dist[i][j] = (inp[j] - '0');
						shelter += dist[i][j];
					}
					if (inp[j] == 'H')
						humans += 1;
				}
			}

			int d = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (dist[i][j] > 0) {
						d += bfs(map, dist, shelter, humans, i, j);
					}
				}
			}
			int consumed = Math.max(humans - d, 0);
			if (2 * consumed > humans || d == 0){
				System.out.println("Case #" + cs + ": There is no survivor.");
			}
			else if (d == 1) {
				System.out.println("Case #" + cs + ": There is 1 survivor.");
			} else {
				System.out.println("Case #" + cs + ": There are " + d + " survivors.");
			}
		}
		br.close();
	}

	static void printInt(int[][] map) {
		for (int[] df : map) {
			System.out.println(Arrays.toString(df));
		}
	}

	static void print(char[][] map) {
		for (char[] df : map) {
			System.out.println(Arrays.toString(df));
		}
	}

	static int bfs(char[][] map, int[][] dist, int shelter, int total, int ii, int jj) {
		int r = map.length;
		int c = map[0].length;
		Queue<int[]> qu = new LinkedList<>();

		qu.add(new int[] { ii, jj, ii, jj });

		int safe = 0;

		int[][] dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

		boolean[][] seen = new boolean[r][c];

		while (!qu.isEmpty()) {

			int sz = qu.size();

			while (sz-- > 0) {

				int[] cr = qu.poll();

				int i = cr[0];
				int j = cr[1];
				int si = cr[2];
				int sj = cr[3];

				if (dist[si][sj] < 1)
					continue;

				if (map[i][j] == 'H' && dist[si][sj] > 0) {
					map[i][j] = '#';// Once shelter is reachable from shelter mark it as '#' and this can be used to readh other shelter.
					safe += 1;
					dist[si][sj] -= 1;
				}

				for (int[] d : dirs) {

					int ni = i + d[0];
					int nj = j + d[1];

					if (ni < 0 || ni > r - 1 || nj < 0 || nj > c - 1 || map[ni][nj] == '.' || seen[ni][nj]) {
						continue;
					}
					seen[ni][nj] = true;
					qu.add(new int[] { ni, nj, si, sj });
				}
			}
		}
		return safe;
	}
}

Same issue - looks like tests are broken - I got assertation error on assert(grid[i].size() == M);

Thanks I saw your comment on the problem page.
I did change my code to handle all scenario. Still getting WA.
If possible can you give me some test cases.

remove after AC
```.

Another issue with your code - each island should have its own Population vs shelters logic.

I’ve not got AC, but we get different answers for this test case:

I get 2, your code gives 4. The way I read the description, all villagers on the second island perish because there’s no shelter for over half of them.

4 7
.2HH...
....
2HHHHH.
.......

(edit: and the second line is short to test Oleg’s remark on how to fix short lines.)

Thanks for the test case, Yes my initial understanding was wrong. After
@defrager response I changed my approach. Now I am considering Individual island.
So Changed my approach. and here is my new bfs Implementation.

//Removed after AC

Here’s another one. I get 10, you give none.

6 6
HHHHH1
H....2
H....3
H....3
H.....
HHHHH1

OK, here’s another. These two should give the same answer, but the above code gives 15 and 12.

6 6
HHHHH1
H....2
H.3H.3
H.HH.3
H....2
HHHHH1
6 6
HHHHH1
H....2
H.9H.3
H.HH.3
H....2
HHHHH1

Thanks for the test case. Finally Accepted.
Small change in code.

		if (population == 0)
			return 0;
		if (shalter >= population)
			return population;
		else if (population > shalter) {
			con = population - shalter;
			if (2 * con > population)
				return 0;
			else
				return shalter;
		} else
			return shalter;
	}

Congratulations!

We now get the same answer for all my test cases, but I still don’t get AC!