再解炸弹人--广度搜索和深度搜索

2024-05-02 06:48

本文主要是介绍再解炸弹人--广度搜索和深度搜索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

再解炸弹人

要求

之前炸弹人通过枚举统计每个点消灭敌人数,包括了小人不能直接到达的位置,现在要求小人从(3,3)位置开始,有敌人的地方小人不能通过,统计消灭敌人数。

思路

先使用广度优先搜索或者深度优先搜索找出小人可以通过的点,再统计这些点消灭敌人数

bfs代码

#include <stdio.h>#define N 13
#define M 13char a[13][13] = {"#############","#GG.GGG#GGG.#","###.#G#G#G#G#","#.......#..G#","#G#.###.#G#G#","#GG.GGG.#.GG#","#G#.#G#.#.#.#","##G...G.....#","#G#.#G###.#G#","#...G#GGG.GG#","#G#.#G#G#.#G#","#GG.GGG#G.GG#","#############"};int b[13][13] = {0};struct node 
{int x;int y;
};struct node que[401];  // 地图大小不超过20*20
int head = 0, tail = 0;void enqueue(struct node p) {que[tail] = p;tail++;
}struct node dequeue() {head++;return que[head-1];
}int is_empty() {return head == tail;
}void print_a() {for (int i = 0; i < N; ++i){for (int j = 0; j < M; ++j){printf("%c ", a[i][j]);}printf("\n");}printf("\n");
}void print_b() {for (int i = 0; i < N; ++i){for (int j = 0; j < M; ++j){printf("%d ", b[i][j]);}printf("\n");}printf("\n");
}int main() 
{int i, j;struct node p = {3, 3}; // 小人所在位置enqueue(p);a[p.x][p.y] = '2'; // 小人到达点标记为2while(!is_empty()) {p = dequeue();// 统计此处安放炸弹消灭敌人数i = p.x;j = p.y;while(a[i][j] != '#') {  //rightj++;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  //downi++;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  //leftj--;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  // upi--;if(a[i][j] == 'G') {b[p.x][p.y]++;}}// bfs 搜索小人可以到达的点if (a[p.x][p.y+1] == '.') {struct node temp = {p.x, p.y+1};enqueue(temp);a[p.x][p.y+1] = '2';}if (a[p.x+1][p.y] == '.') {struct node temp = {p.x+1, p.y};enqueue(temp);a[p.x+1][p.y] = '2';}if (a[p.x][p.y-1] == '.') {struct node temp = {p.x, p.y-1};enqueue(temp);a[p.x][p.y-1] = '2';}if (a[p.x-1][p.y] == '.') {struct node temp = {p.x-1, p.y};enqueue(temp);a[p.x-1][p.y] = '2';}}print_a();print_b();return 0;
}

dfs代码

#include <stdio.h>#define N 13
#define M 13char a[13][13] = {"#############","#GG.GGG#GGG.#","###.#G#G#G#G#","#.......#..G#","#G#.###.#G#G#","#GG.GGG.#.GG#","#G#.#G#.#.#.#","##G...G.....#","#G#.#G###.#G#","#...G#GGG.GG#","#G#.#G#G#.#G#","#GG.GGG#G.GG#","#############"};int b[13][13] = {0};struct node 
{int x;int y;
};struct node stack[41];
int top = 0;void push(struct node p) {stack[top] = p;top++;
}struct node pop() {top--;return stack[top];
}int is_empty() {return top == 0;
}void print_a() {for (int i = 0; i < N; ++i){for (int j = 0; j < M; ++j){printf("%c ", a[i][j]);}printf("\n");}printf("\n");
}void print_b() {for (int i = 0; i < N; ++i){for (int j = 0; j < M; ++j){printf("%d ", b[i][j]);}printf("\n");}printf("\n");
}int main() 
{int i, j;struct node p = {3, 3}; // 小人所在位置push(p);a[p.x][p.y] = '2';while(!is_empty()) {p = pop();// 统计此处安放炸弹消灭敌人数i = p.x;j = p.y;while(a[i][j] != '#') {  //rightj++;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  //downi++;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  //leftj--;if(a[i][j] == 'G') {b[p.x][p.y]++;}}i = p.x;j = p.y;while(a[i][j] != '#') {  // upi--;if(a[i][j] == 'G') {b[p.x][p.y]++;}}// bfs 搜索小人可以到达的点if (a[p.x][p.y+1] == '.') {struct node temp = {p.x, p.y+1};push(temp);a[p.x][p.y+1] = '2';}if (a[p.x+1][p.y] == '.') {struct node temp = {p.x+1, p.y};push(temp);a[p.x+1][p.y] = '2';}if (a[p.x][p.y-1] == '.') {struct node temp = {p.x, p.y-1};push(temp);a[p.x][p.y-1] = '2';}if (a[p.x-1][p.y] == '.') {struct node temp = {p.x-1, p.y};push(temp);a[p.x-1][p.y] = '2';}}print_a();print_b();return 0;
}

结果

# # # # # # # # # # # # #
# G G 2 G G G # G G G . #
# # # 2 # G # G # G # G #
# 2 2 2 2 2 2 2 # . . G #
# G # 2 # # # 2 # G # G #
# G G 2 G G G 2 # 2 G G #
# G # 2 # G # 2 # 2 # 2 #
# # G 2 2 2 G 2 2 2 2 2 #
# G # 2 # G # # # 2 # G #
# 2 2 2 G # G G G 2 G G #
# G # 2 # G # G # 2 # G #
# G G 2 G G G # G 2 G G #
# # # # # # # # # # # # #0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 0 0 0 2 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 5 0 0 0 6 0 5 0 0 0
0 0 0 0 0 0 0 1 0 3 0 8 0
0 0 0 2 2 5 0 3 2 5 2 10 0
0 0 0 0 0 0 0 0 0 3 0 0 0
0 4 1 1 0 0 0 0 0 8 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0
0 0 0 5 0 0 0 0 0 6 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
python 代码
N = 13
M = 13a  =   ["#############","#GG.GGG#GGG.#","###.#G#G#G#G#","#.......#..G#","#G#.###.#G#G#","#GG.GGG.#.GG#","#G#.#G#.#.#.#","##G...G.....#","#G#.#G###.#G#","#...G#GGG.GG#","#G#.#G#G#.#G#","#GG.GGG#G.GG#","#############"]b = [([0] * N) for i in range(N)]q = []
s = []def mprint(x):for i in range(N):print(x[i])def marking(a, i, j):text = a[i]new = text[:j] + "2" + text[j+1:]a[i] = newdef bfs(i, j):q.append((i, j))marking(a, i, j)while len(q) != 0:i, j = q.pop(0)x = iy = jwhile a[x][y] != '#':  # righty = y + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # downx = x + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # lefty = y - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # upx = x - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1if a[i][j+1] == '.':q.append((i, j+1))marking(a, i, j+1)if a[i+1][j] == '.':q.append((i+1, j))marking(a, i+1, j)if a[i][j-1] == '.':q.append((i, j-1))marking(a, i, j-1)if a[i-1][j] == '.':q.append((i-1, j))marking(a, i-1, j)mprint(a)mprint(b)def dfs(i, j):s.append((i, j))marking(a, i, j)while len(s):i, j = s.pop()x = iy = jwhile a[x][y] != '#':  # righty = y + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # downx = x + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # lefty = y - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#':  # upx = x - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1if a[i][j + 1] == '.':s.append((i, j + 1))marking(a, i, j + 1)if a[i + 1][j] == '.':s.append((i + 1, j))marking(a, i + 1, j)if a[i][j - 1] == '.':s.append((i, j - 1))marking(a, i, j - 1)if a[i - 1][j] == '.':s.append((i - 1, j))marking(a, i - 1, j)mprint(a)mprint(b)# bfs(3, 3)
dfs(3, 3)

总结

bfs使用队列实现,dfs使用栈实现,将bfs中的队列改为栈就很容易写出dfs。

这篇关于再解炸弹人--广度搜索和深度搜索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/953627

相关文章

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

MyBatis分页插件PageHelper深度解析与实践指南

《MyBatis分页插件PageHelper深度解析与实践指南》在数据库操作中,分页查询是最常见的需求之一,传统的分页方式通常有两种内存分页和SQL分页,MyBatis作为优秀的ORM框架,本身并未提... 目录1. 为什么需要分页插件?2. PageHelper简介3. PageHelper集成与配置3.