- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
-
1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)
# = Wall
| = No wall
- = No wall
图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成mn(m≤50,n≤50)个方块,每个方块可以有0~4面墙。 - 输入
- 程序从标准输入设备读入数据。第一行是两个整数,分别是南北向、东西向的方块数。在接下来的输入行里,每个方块用一个数字(0≤p≤50)描述。用一个数字表示方块周围的墙,1表示西墙,2表示北墙,4表示东墙,8表示南墙。每个方块用代表其周围墙的数字之和表示。城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。输入的数据保证城堡至少有两个房间。
- 输出
- 城堡的房间数、城堡中最大房间所包括的方块数。结果显示在标准输出设备上。
- 样例输入
-
4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
- 样例输出
-
5
9
package pro.proclass.tielintang;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Integer.parseInt;
//openjudge
public class 城堡问题2815 {
private static int[][][] ns;
private static int[][] ns2;
private static int w, h;
private static int ghouseNum = 0;
private static int gbigHouse = 0;
private static int bigHouse = 0;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
w = parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
h = parseInt(st.nextToken());
ns = new int[w][h][4];
ns2 = new int[w][h];
ghouseNum = 0;
gbigHouse = 0;
for (int i = 0; i < w; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < h; j++) {
int v = parseInt(st.nextToken());
ns[i][j] = getWall(v);
// ns2[i][j] = v;
}
}
boolean[][] visited = new boolean[w][h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if(!visited[i][j]) {
visited[i][j] = true;
bigHouse = 1;
dfs(i, j, visited);
ghouseNum++;
}
}
}
System.out.println(ghouseNum);
System.out.println(gbigHouse);
}
private static void dfs(int x, int y, boolean[][] nvisited) {
if(bigHouse > gbigHouse) gbigHouse = bigHouse;
if(ns[x][y][0] == 0) {
// if((ns2[x][y] & 1) == 0) {
if(y-1 >= 0 && !nvisited[x][y-1]) { // 西
nvisited[x][y - 1] = true;
bigHouse++;
dfs(x, y - 1, nvisited);
}
}
if(ns[x][y][1] == 0) {
// if((ns2[x][y] & 2) == 0) {
if(x-1 >= 0 && !nvisited[x-1][y]) { // 北
nvisited[x - 1][y] = true;
bigHouse++;
dfs(x - 1, y, nvisited);
}
}
if(ns[x][y][2] == 0) {
// if((ns2[x][y] & 4) == 0) {
if(y+1 < h && !nvisited[x][y+1]) { // 东
nvisited[x][y+1] = true;
bigHouse++;
dfs(x, y+1, nvisited);
}
}
if(ns[x][y][3] == 0) {
// if((ns2[x][y] & 8) == 0) {
if(x+1 < w && !nvisited[x+1][y]) { // 南
nvisited[x+1][y] = true;
bigHouse++;
dfs(x+1, y, nvisited);
}
}
}
private static int[] getWall(int x) {
int[] ret = new int[4]; // 西北东南
int max = 1 << 3;
int index = 3;
while (x > 0) {
while (x < max) {
max = max >> 1;
index--;
}
x -= max;
ret[index] = 1;
}
return ret;
}
}