X*Y
的方格组成,例如下图为6*4
的大小。每一个放个以坐标(x,y)
描述。(0,0)
出发,只能向东或者向北前进,(5,3)
。(4,1)
,机器人不能经过那儿。B
的方格,称之为陷阱方格。A
的方格,称之为不可达方格,不可达方格不包括墙壁所在的位置2
个,不可达方格有3
个。x
和y
(0 < x,y <= 1000
)N
(0 <= N < X*Y
)N
行墙壁的坐标6 4
5
0 2
1 2
2 2
4 1
5 1
2 3
6 4
4
2 0
2 1
3 0
3 1
0 4
说明不可达方格有4
个 (4,0)
(4,1)
(5,0)
(5,1)
import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2022/5/16
* Time: 10:02
* Description:
*/
public class Main0121 {
private static int xLen;
private static int yLen;
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
xLen = scanner.nextInt();
yLen = scanner.nextInt();
int n = scanner.nextInt();
int[][] walls = new int[n][2];
for (int i = 0; i < n; i++) {
walls[i][0] = scanner.nextInt();
walls[i][1] = scanner.nextInt();
}
solution(walls);
}
}
static class Check {
int x;
int y;
public Check(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Check check = (Check) o;
return x == check.x && y == check.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
private static void solution(int[][] walls) {
int trapCount = 0;
int invalidCount = 0;
Set<Check> wallSet = new HashSet<>();
for (int[] wall : walls) {
wallSet.add(new Check(wall[0], wall[1]));
}
Set<Check> checks = new HashSet<>();
Set<Check> finish = new HashSet<>();
// x, y
findOut(0, 0, wallSet, checks, finish);
// System.out.println(checks);
invalidCount = xLen * yLen - checks.size() - wallSet.size();
// System.out.println(finish);
// trapTest
for (Check check : finish) {
Set<Check> checksT = new HashSet<>();
Set<Check> finishT = new HashSet<>();
findOut(check.x, check.y, wallSet, checksT, finishT);
if (!finishT.contains(new Check(xLen - 1, yLen - 1))) {
trapCount++;
}
}
System.out.print(trapCount + " " + invalidCount);
}
private static void findOut(int x, int y, Set<Check> wallSet, Set<Check> checks, Set<Check> finish) {
if (x == xLen - 1 && y == yLen - 1) {
finish.add(new Check(x, y));
}
if (x >= xLen || y >= yLen) {
return;
}
checks.add(new Check(x, y));
// 北
if (!wallSet.contains(new Check(x, y + 1))) {
findOut(x, y + 1, wallSet, checks, finish);
} else {
finish.add(new Check(x, y));
}
// 东
if (!wallSet.contains(new Check(x + 1, y))) {
findOut(x + 1, y, wallSet, checks, finish);
} else {
finish.add(new Check(x, y));
}
}
}