【编程马拉松】【014-红与黑】

【编程马拉松算法目录】


【014-红与黑】【工程下载>>>】


1 题目描述


  有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的(上下左右四个方向)黑色瓷砖移动。
  请写一个程序,计算你总共能够到达多少块黑色的瓷砖。

1.1 输入描述:


  输入包含多组数据。
  每组数据第一行是两个整数 m 和 n(1≤m, n≤20)。紧接着 m 行,每行包括 n 个字符。每个字符表示一块瓷砖的颜色,规则如下:
  1. “.”:黑色的瓷砖;
  2. “#”:白色的瓷砖;
  3. “@”:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。

1.2 输出描述:


  对应每组数据,输出总共能够到达多少块黑色的瓷砖。

1.3 输入例子:


9 6
....#.
.....#
......
......
......
......
......
#@...#
.#..#.

1.4 输出例子:


45

2 解题思路


  可以将红色地板看作是障碍,从@地板出发,尝试不同的走法,直到找出步的黑地板数最多的解决方案。因为走过的地板可以重复走,所以只要从起始点开始做广度优先遍历,记录可以访问的黑地板数就可以实现。

3 算法实现


import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

/**
 * 改进方案
 * <p>
 * Author: 王俊超
 * Time: 2016-05-11 09:34
 * CSDN: http://blog.csdn.net/derrantcm
 * Github: https://github.com/Wang-Jun-Chao
 * Declaration: All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            int row = scanner.nextInt();
            int col = scanner.nextInt();
            int[][] floor = new int[row][col];

            for (int i = 0; i < row; i++) {
                floor[i] = new int[col];
                String line = scanner.next();
                for (int j = 0; j < col; j++) {
                    floor[i][j] = line.charAt(j);
                }
            }

            System.out.println(maxStep(floor));
        }

        scanner.close();
    }

    /**
     * 求可以走的地板的最大步数
     *
     * @param floor 地板
     * @return 步数
     */
    private static int maxStep(int[][] floor) {

        int x = 0;
        int y = 0;
        int row = floor.length;
        int col = floor[0].length;

        // 找起始位置
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (floor[i][j] == '@') {
                    x = i;
                    y = j;
                }
            }
        }

        // 输出地板信息
//        for (int[] line : floor) {
//            for (int e : line) {
//                System.out.print((char) e);
//            }
//            System.out.println();
//        }


        return findPath(floor, x, y);
    }

    /**
     * 求可以走的黑地板的最大步数
     *
     * @param floor 地板
     * @param x     起始坐标
     * @param y     起始坐标
     */
    private static int findPath(int[][] floor, int x, int y) {


        int row = floor.length;
        int col = floor[0].length;

        if (x < 0 || x >= row || y < 0 || y >= col || floor[x][y] == '#') {
            return 0;
        }

        // 记录待访问的位置,两个一组
        Queue<Integer> queue = new ArrayDeque<>(row * col * 2);
        // 可以移动的四个方向,两个一组
        int[] d = {1, 0, 0, 1, -1, 0, 0, -1};

        queue.add(x);
        queue.add(y);

        // 最多可以走的黑地板数目
        int result = 1;

        while (!queue.isEmpty()) {
            x = queue.remove();
            y = queue.remove();

            for (int i = 0; i < d.length; i += 2) {
                int t = x + d[i];
                int v = y + d[i + 1];
                if (t >= 0 && t < row && v >= 0 && v < col && floor[t][v] == '.') {
                    // 标记已经访问过
                    floor[t][v] = '#';
                    // 计数器增加
                    result++;

                    // 访问的位置添加到队列中
                    queue.add(t);
                    queue.add(v);
                }
            }
        }


        return result;
    }
}

4 测试结果


5 其它信息


因为markddow不好编辑,因此将文档的图片上传以供阅读。Pdf和Word文档可以在Github上进行【下载>>>】

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值