信息学奥赛一本通T1250-The Castle - C语言网 (dotcpp.com)
四个数组,存放上下左右能走的方向。
手动赋值上下左右判断数组
列出墙的所有情况。
import java.util.*;
public class Main {
static class jieDian {
int x;
int y;
public jieDian(int x, int y) {
super();
this.x = x;
this.y = y;
}
public jieDian() {
}
}
static int[] shang = new int[17];
static int[] xia = new int[17];
static int[] zuo = new int[17];
static int[] you = new int[17];
static int n;
static int m;
static int[][] arr;
static int[][] chek;
static int cont = 0;
static int max = 0;
public static void main(String[] args) {
inio();// 初始化判断方向的数字组,将墙的方向全部列出来,找出可以走的方向,赋值为1
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
arr = new int[n][m];
chek = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
}
// bfs
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (chek[i][j] != 1) {
bfs(i, j);
cont++;
// return;
}
}
}
System.out.println(cont);
System.out.println(max+1);
}
private static void bfs(int a1, int a2) {
Queue<jieDian> dui = new LinkedList<Main.jieDian>();
dui.offer(new jieDian(a1, a2));
int sum = 0;
chek[a1][a2] = 1;//进来第一次,脚踩的地方,下次就不能再次到达了!!!
while (!dui.isEmpty()) {
int size = dui.size();
for (int i = 0; i < size; i++) {
Main.jieDian p = dui.poll();
int x = p.x;
int y = p.y;
// if (arr[x][y] == 0) {
// System.out.println(shang[arr[x][y]] != 0 && x - 1 >= 0 && chek[x - 1][y] != 1);
// System.out.println(you[arr[x][y]] != 0 && y + 1 < m && chek[x][y + 1] != 1);
// System.out.println(xia[arr[x][y]] != 0 && x + 1 < n && chek[x + 1][y] != 1);
// System.out.println(zuo[arr[x][y]] != 0 && y - 1 >= 0 && chek[x][y - 1] != 1);
//
// }
// 上
if (shang[arr[x][y]] != 0 && x - 1 >= 0 && chek[x - 1][y] != 1) {
sum++;
chek[x - 1][y] = 1;
dui.offer(new jieDian(x - 1, y));
// System.out.println("上" + 1);
}
// 右
if (you[arr[x][y]] != 0 && y + 1 < m && chek[x][y + 1] != 1) {
sum++;
chek[x][y + 1] = 1;
dui.offer(new jieDian(x, y + 1));
// System.out.println("右" + 1);
}
// 下
if (xia[arr[x][y]] != 0 && x + 1 < n && chek[x + 1][y] != 1) {
sum++;
chek[x + 1][y] = 1;
dui.offer(new jieDian(x + 1, y));
// System.out.println("下" + 1);
}
// 左
if (zuo[arr[x][y]] != 0 && y - 1 >= 0 && chek[x][y - 1] != 1) {
sum++;
chek[x][y - 1] = 1;
dui.offer(new jieDian(x, y - 1));
// System.out.println("左" + 1);
} else {
f4 = true;
}
}
}
max = Math.max(max, sum);
}
private static void inio() {
int[][] ditu = new int[][] { { 13, 9, 12, 4, 8, 1, 5, 0 }, { 7, 3, 6, 2, 4, 1, 5, 0 },
{ 14, 6, 12, 2, 4, 8, 10, 0 }, { 11, 9, 3, 2, 8, 1, 10, 0 } };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
if (i == 0) {
shang[ditu[i][j]] = 1;
} else if (i == 1) {
xia[ditu[i][j]] = 1;
} else if (i == 2) {
zuo[ditu[i][j]] = 1;
} else {
you[ditu[i][j]] = 1;
}
}
}
// ok
// System.out.println(Arrays.toString(shang));
// System.out.println(Arrays.toString(xia));
// System.out.println(Arrays.toString(zuo));
// System.out.println(Arrays.toString(you));
}
}
注意当前点,一定是走过了的,我已经在这个地方栽了两次了呜呜呜