蓝桥杯之全球变暖

标题:全球变暖

你有一张某海域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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静波波呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值