蓝桥杯算法心得——扫雷(bfs+遍历)

大家好,我是晴天学长,今天讲解的是一道基础题,其实用bfs有些复杂了,用逆向枚举会简单些但是也是一个复习的过程,加油!需要的小伙伴请自取哦!💪💪💪


1 )扫雷

在这里插入图片描述


2) .算法思路

(方法一-bfs)
扫雷()
1.接收一个nm大小的二维数组,是1的直接改成9
2.遍历矩阵,不是9的直接bfs

2.bfs
压入坐标
while
取出对头,上下左右(限制条件为该点的8个方格内获取二维矩阵内)
9直接
sum++

(方法二-遍历)
扫雷()
1.接收一个nm大小的二维数组,是1的直接改成9
2.遍历矩阵,是9的直接在周围的数中+1(注意越界问题)


3).代码示例

(方法一-bfs)

package LanQiaoTest.BFS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class 扫雷 {
    //偏位移
    static int[] dx = {1, -1, 0, 0, 1, -1, 1, -1};
    static int[] dy = {0, 0, 1, -1, 1, 1, -1, -1};

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] s = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int[][] G = new int[n][m];
        for (int i = 0; i < n; i++) {
            s = in.readLine().split(" ");
            for (int j = 0; j < m; j++) {
                int a = Integer.parseInt(s[j]);
                if (a == 1) {
                    a = 9;
                }
                G[i][j] = a;
            }
        }
        //遍历二维数组
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!(G[i][j] == 9)) {
                    G[i][j] = bfs(n, m, i, j, G);
                }

            }
        }
        //输出数组
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(G[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static int bfs(int n, int m, int x, int y, int[][] G) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y});
        int ans = 0;
        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int a = curr[0], b = curr[1];
            //正方向和45度方向
            for (int i = 0; i < 8; i++) {
                int newX = a + dx[i];
                int newY = b + dy[i];
                if (newX >= 0 && newX < n && newY >= 0 && newY < m) {
                    if (G[newX][newY] == 9) {
                        ans++;
                    }
                }
            }

        }
        return ans;
    }
}

(方法二-遍历)



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	//偏位移
		static int[] dx = {1,-1,0,0,1,-1,1,-1};
		static int[] dy = {0,0,1,-1,1,1,-1,-1};

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] s = in.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int m = Integer.parseInt(s[1]);
		int[][] G  = new int[n][m];
		//接收矩阵
		for (int i = 0; i < n; i++) {
			s = in.readLine().split(" ");
			for (int j = 0; j < m; j++) {
				int a = Integer.parseInt(s[j]);
				if (a==1) {
					a=9;
				}
				G[i][j] = a;
			}
		}
		//遍历矩阵
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (G[i][j]==9) {
					for (int j2 = 0; j2 < 8; j2++) {
						int newX = i + dx[j2];
						int newY = j + dy[j2];
						if (newX>=0&&newX<n&&newY>=0&&newY<m&&!(G[newX][newY]==9)) {
							G[newX][newY]++;
						}
					}
				}
			}
		}
		//输出矩阵
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				System.out.print(G[i][j]+" ");
			}
			System.out.println();
		}
		
	}

}


4).总结

  • bfs的基础做法
  • 枚举的逆向思维

原题链接

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晴天学长

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

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

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

打赏作者

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

抵扣说明:

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

余额充值