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

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

相关文章

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.

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言