AcWing 1098. java 城堡问题
原题链接
简单
作者:
mkuiwu
,
2021-02-10 12:47:43
,
所有人可见
,
阅读 400
import java.lang.*;
import java.io.*;
import java.util.*;
class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer stz = new StringTokenizer("");
static String nextLine() throws Exception {// 读取下一行字符串
return br.readLine();
}
static String next() throws Exception {// 读取下一个字符串
while (!stz.hasMoreTokens()) {
stz = new StringTokenizer(br.readLine());
}
return stz.nextToken();
}
static int nI() throws Exception {// 读取下一个int型数值
return Integer.parseInt(next());
}
static double nD() throws Exception {// 读取下一个double型数值
return Double.parseDouble(next());
}
static long nL() throws Exception {// 读取下一个double型数值
return Long.parseLong(next());
}
static void write(String str) throws Exception{
bw.write(str);
}
static String itoS(int i){
return Integer.toString(i);
}
static void wI(int i) throws Exception{
write(Integer.toString(i));
}
static void flush() throws Exception{
bw.flush();
}
public static void main(String[] args) throws Exception{
new Main().run();
}
final int N =55;
class Point{
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
int n, m;
int[][] g = new int[N][N];
boolean[][] st = new boolean[N][N];
Queue<Point> q = new LinkedList<>();
int[] dx = {0, -1, 0, 1}, dy = {-1, 0, 1, 0};
int bfs(int x, int y){
q.add(new Point(x, y));
st[x][y] = true;
int area = 0;
while (!q.isEmpty()){
Point t = q.poll();
area++;
for (int i = 0; i < 4; i++) {
int a = t.x + dx[i], b = t.y + dy[i];
if (a >= n || a < 0 || b < 0 || b >= m || st[a][b]) continue;
if ((g[t.x][t.y] >> i & 1) == 1) continue;
st[a][b] = true;
q.add(new Point(a, b));
}
}
return area;
}
public void run() throws Exception{
n = nI(); m = nI();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
g[i][j] = nI();
}
}
int cnt = 0, area = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!st[i][j]){
cnt++;
area = Math.max(area, bfs(i, j));
}
}
}
wI(cnt);
write("\n");
wI(area);
write("\n");
flush();
}
}