【战场索敌】Java 实现

题目

在这里插入图片描述

思路

1.dfs深度遍历
2、若某个位置未被搜索过并且不是城墙“#”,那么可以开始DFS遍历,单个源头的搜索区域,该区域的‘E’(敌军)数量+1.遍历完成后,对总敌军数量<K,总结过+1.

代码

import java.util.Scanner;

public class Main {
	public static int[][] direction = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
	// 创建二维数组,记录map是否被访问
	public static boolean[][] visited_info = new boolean[100][100];

	public static void main(String[] args) {

		// 处理输入
		Scanner input = new Scanner(System.in);
		int N = input.nextInt();
		int M = input.nextInt();
		int K = input.nextInt();
		input.nextLine();// 接收“\n”换行符号

		String[][] arr_map = new String[N][M];
		for (int i = 0; i < N; i++) {
			String strs = input.nextLine();
			for (int j = 0; j < M; j++) {
				arr_map[i][j] = strs.substring(j, j + 1);
			}
		}

		// for(int i = 0;i < arr_map.length;i++){
		// for(int j = 0;j < arr_map[i].length;j++){
		// System.out.print(arr_map[i][j] + " ");
		// }
		// System.out.println();
		// }

		System.out.println(dfs(arr_map, K));
	}

	public static int dfs(String[][] arr_map, int target) {
		int sumCount = 0;
		for (int i = 0; i < arr_map.length; i++) {
			for (int j = 0; j < arr_map[i].length; j++) {
				int count = dfs(arr_map, visited_info, i, j);
				//判断区域内敌人数量是否
				if (count > 0 && count < target) {
					sumCount++;
					System.out.println("sumCount = " + sumCount);
				}
			}
		}
		return sumCount;
	}

	public static int dfs(String[][] arr_map, boolean[][] visited_info, int i, int j) {
		int count = 0;
		if (arr_map[i][j].equals("#")) {// 注意使用equals()
			return count;
		}
		if (isVisited(i, j)) {
			return count;
		}
		visited_info[i][j] = true;// 置为true
		if (arr_map[i][j].equals("E")) {
			count++;
		}
		for (int d = 0; d < 4; d++) {
			int new_x = i + direction[d][0];
			int new_y = j + direction[d][1];

			// 如果新位置在区域内并且不是城墙,而且没有被访问过,进行DFS,返回count
			if (new_x >= 0 && new_x < arr_map.length && new_y >= 0 && new_y < arr_map[new_x].length
					&& (!arr_map[new_x][new_y].equals("#")) && (!isVisited(new_x, new_y))) {
				count += dfs(arr_map, visited_info, new_x, new_y);
			}
		}
		return count;
	}

	// 判断是否被访问过
	public static boolean isVisited(int i, int j) {
		if (visited_info[i][j]) {
			return true;
		}
		return false;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了计算战场上总共有多少区域里的敌人数小于K,可以使用深度优先搜索算法(DFS)来遍历地图上的每个区域。首先,我们需要定义一个二维visited矩阵来记录已经访问过的位置。然后,我们可以使用一个双重循环来遍历地图上的每个位置。 在DFS函数中,我们可以从给定的位置开始,判断该位置上是否有敌人('E'),如果有,将敌人数量count初始化为1。然后,将该位置标记为已访问,并遍历该位置的上下左右四个方向。对于每个可以继续搜索的位置,我们递归调用DFS函数,并更新count的值。最终,DFS函数返回的count就是该区域内的敌人数量。 在getResult函数中,我们可以初始化ans为0,然后遍历地图上的每个位置。如果该位置已经访问过或者是墙壁('#'),则跳过。否则,我们调用DFS函数,并将返回的count与k进行比较。如果count小于k,则ans加1。最后,getResult函数返回的ans就是总共有多少区域里的敌人数小于K。 因此,通过调用getResult函数,可以得到战场上总共有多少区域里的敌人数小于K。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [华为OD机试 - 战场索敌Java & JS & Python)](https://blog.csdn.net/qfc_128220/article/details/130774007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值