过得了样例和自己写的例子,但一提交就RE(用java写的)
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int N, area = 0, cir = Integer.MAX_VALUE;
static int[] xx = { 0, 0, 1, -1 };
static int[] yy = { 1, -1, 0, 0 };
static char[][] arr;
static boolean[][] vis;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(bf);
st.nextToken();
N = (int) st.nval;
arr = new char[N][N];
vis = new boolean[N][N];
bf.readLine();
for (int i = 0; i < N; i++) {
String str = bf.readLine();
arr[i] = str.toCharArray();
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!vis[i][j] && arr[i][j] == '#') {
bfs(i, j, 1, 0);
}
}
}
System.out.println(area + " " + cir);
}
public static void bfs(int x, int y, int nowArea, int nowCir) {
Queue<Point> q = new LinkedList<Point>();
q.add(new Point(x, y));
vis[x][y] = true;
while (!q.isEmpty()) {
Point p = q.poll();
for (int i = 0; i < 4; i++) {
int row = p.x + xx[i];
int col = p.y + yy[i];
if (row >= 0 && row < N && col >= 0 && col < N) {// 前提是不能越界
if (!vis[row][col] && arr[row][col] == '#') {
q.add(new Point(row, col));
vis[row][col] = true;
nowArea++;
} else if (!vis[row][col] && arr[row][col] == '.') {
nowCir++;
}
} else {
nowCir++;
}
}
}
if (nowArea > area) {// 先要找到最大的
area = nowArea;
cir = nowCir;
} else if (nowArea == area) {// 如果碰到相等的
if (nowCir < cir) {// 就取周长最小的
cir = nowCir;
}
}
}
}