标题:全球变暖
你有一张某海域NxN像素的照片,“。”表示海洋,“#”表示陆地,如下所示:
…
.##…
.##…
…##.
…####.
…###.
…
其中“上下左右”四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。
由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。
例如上图中的海域未来会变成如下样子:
…
…
…
…
…#…
…
…
请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。
【输入格式】
第一行包含一个整数N.(1 <= N <= 1000)
以下N行N列代表一张海域照片。
照片保证第1行,第1列,第N行,第N列的像素都是海洋。
【输出格式】
一个整数表示答案。
【输入样例】
7
…
.##…
.##…
…##.
…####.
…###.
…
【输出样例】
1
思路:
判断某一块陆地是否不被淹的judge一定要是全局变量,而不能是局部变量。因为如果judge是局部变量,那么有可能之后的judge变为true,但之前的judge还是false,这样可能导致结果不正确。而如果judge是全局变量,那么只要judge变为true,之前的递归中用的judge和之后递归中用的judge就全变为了true。
例如:(0代表 ‘.’,1代表 ‘#’)
0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0
0 0 1 1 1 1 1 0
0 1 1 1 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
如果是上例,那么judge是局部变量就会出现错误。因为在判断完左边的那个陆地不会被淹没后,judge就变成了true,但其实右面的那个不会被淹没的陆地,其对应的judge还是false,所以最终结果会出现错误。
以下代码更普及一点,包含了边界的情况。
import java.util.Scanner;
public class LanQiao62_test {
static int temp = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[][] nums = new char[n][n];
String[] s = new String[n];
for(int i = 0; i < n; i++){
s[i] = sc.next();
for(int j = 0; j < n; j++){
nums[i][j] = s[i].charAt(j);
}
}
/*
注:以下这种情况,如果judge是局部变量就会出错
int n = 8;
char[][] nums = {
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '#', '#', '.', '.'},
{'.', '.', '#', '#', '#', '#', '#', '.'},
{'.', '#', '#', '#', '.', '#', '.', '.'},
{'.', '.', '#', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'}
};
*/
int num = 0;
int[][] map = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(nums[i][j] == '#'){
map[i][j] = 1;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(map[i][j] == 1){
f(map, i, j, false);
num++;
}
}
}
System.out.println(num - temp);
}
public static void f(int[][] map, int i, int j, boolean judge){
if((i - 1 >= 0 && map[i - 1][j] == 0)
|| (j - 1 >= 0 && map[i][j - 1] == 0)
|| (i + 1 < map.length && map[i + 1][j] == 0)
|| (j + 1 < map.length && map[i][j + 1] == 0)){//如果某一块陆地的周围有水,那么就说明这个陆地被淹没了,用2表示
map[i][j] = 2;
}else{//如果某一块陆地的四周没有水,就把它标记成3,表示该岛屿不会被完全淹没。一定不能让它继续保持1,否则可能会出错
map[i][j] = 3;
if(!judge){
temp++;
judge = true;
}
}
if(i - 1 >= 0 && map[i - 1][j] == 1){
f(map, i - 1, j, judge);
}
if(j - 1 >= 0 && map[i][j - 1] == 1){
f(map, i, j - 1, judge);
}
if(i + 1 < map.length && map[i + 1][j] == 1){
f(map, i + 1, j, judge);
}
if(j + 1 < map.length && map[i][j + 1] == 1){
f(map, i, j + 1, judge);
}
}
}
注: judge变量一定要是全局变量。因为如果judge是局部变量,那么如果当前的judge是true,那么之后的judge都会变成true,但之前的judge还是false,这样可能导致结果不正确。而如果judge是全局变量,那么只要judge变为true,之前的递归中用的judge和之后递归中用的judge就全变为了true
import java.util.Scanner;
public class Main {
static int temp = 0;
static boolean judge;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[][] nums = new char[n][n];
String[] s = new String[n];
for(int i = 0; i < n; i++){
s[i] = sc.next();
for(int j = 0; j < n; j++){
nums[i][j] = s[i].charAt(j);
}
}
int num = 0;
int[][] map = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(nums[i][j] == '#'){
map[i][j] = 1;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(map[i][j] == 1){
judge = false;
f(map, i, j);
num++;
}
}
}
System.out.println(num - temp);
}
public static void f(int[][] map, int i, int j){
if((i - 1 >= 0 && map[i - 1][j] == 0)
|| (j - 1 >= 0 && map[i][j - 1] == 0)
|| (i + 1 < map.length && map[i + 1][j] == 0)
|| (j + 1 < map.length && map[i][j + 1] == 0)){//如果某一块陆地的周围有水,那么就说明这个陆地被淹没了,用2表示
map[i][j] = 2;
}else{//如果某一块陆地的四周没有水,就把它标记成3,表示该岛屿不会被完全淹没。一定不能让它继续保持1,否则可能会出错
map[i][j] = 3;
if(!judge){
temp++;
judge = true;
}
}
if(i - 1 >= 0 && map[i - 1][j] == 1){
f(map, i - 1, j);
}
if(j - 1 >= 0 && map[i][j - 1] == 1){
f(map, i, j - 1);
}
if(i + 1 < map.length && map[i + 1][j] == 1){
f(map, i + 1, j);
}
if(j + 1 < map.length && map[i][j + 1] == 1){
f(map, i, j + 1);
}
}
}