【代码随想录】【算法训练营】【第30天】 [322]重新安排行程 [51]N皇后 [37]解数独

本文主要是介绍【代码随想录】【算法训练营】【第30天】 [322]重新安排行程 [51]N皇后 [37]解数独,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

思路及算法思维,指路 代码随想录。
题目来自 LeetCode。

day 30,周四,好难,会不了一点~

题目详情

[322] 重新安排行程

题目描述

322 重新安排行程
322 重新安排行程

解题思路

前提:……
思路:回溯。
重点:……。

代码实现

C语言
回溯 + 链表自实现

超出时间限制!!

/*** Note: The returned array must be malloced, assume caller calls free().*/#define NAME_LEN 4
#define INVALID_NUM 1000typedef struct airlist {char *start;char **end;int endSize;bool *used;
} airList;struct airlist *list;
int listSize;
char **path;
int pathSize;int findListLoc(char *start, int ticketsSize)
{int j = INVALID_NUM;for (j = 0; j < ticketsSize; j++) {if ((list[j].start == NULL) || (0 == strcmp(start, list[j].start))) {return j;}}return j;
}void insertListLoc(char *end, int loc)
{int endS = list[loc].endSize;int serLoc = endS;if (list[loc].endSize > 0) {}for (int k = endS - 1; k >= 0; k--) {if (0 > strcmp(end, list[loc].end[k])) {strncpy(list[loc].end[k + 1], list[loc].end[k], NAME_LEN);serLoc = k;}}strncpy(list[loc].end[serLoc], end, NAME_LEN);(list[loc].endSize)++;return ;
}void init(char*** tickets, int ticketsSize)
{// 开辟空间// 初始化listlist = (struct airlist *)malloc(sizeof(struct airlist) * ticketsSize);memset(list, 0, sizeof(struct airlist) * ticketsSize);listSize = 0;for (int i = 0; i < ticketsSize; i++) {// 初始化startint loc = findListLoc(tickets[i][0], ticketsSize);if (list[loc].start == NULL) {list[loc].start = (char *)malloc(sizeof(char) * NAME_LEN);strncpy(list[loc].start, tickets[i][0], NAME_LEN);}// 初始化end,按字典序排列if (list[loc].end == NULL) {list[loc].end = (char **)malloc(sizeof(char *) * ticketsSize);for (int v= 0; v < ticketsSize; v++) {list[loc].end[v] = (char *)malloc(sizeof(char) * NAME_LEN);memset(list[loc].end[v], 0, sizeof(char) * NAME_LEN);}}insertListLoc(tickets[i][1], loc);// 初始化used数组if (list[loc].used == NULL) {list[loc].used = (bool *)malloc(sizeof(bool) * ticketsSize);memset(list[loc].used, 0, sizeof(bool) * ticketsSize);}listSize = (listSize < (loc + 1)) ? (loc + 1) : listSize;}// 初始化pathpath = (char **)malloc(sizeof(char *) * (ticketsSize + 1));for (int l = 0; l < (ticketsSize + 1); l++) {path[l] = (char *)malloc(sizeof(char) * NAME_LEN);memset(path[l], 0, sizeof(char) * NAME_LEN);}pathSize = 0;return ;
}bool backtracking(char *start, int  ticketsSize)
{// 退出条件if (pathSize == (ticketsSize + 1)) {return true;}// 递归int loca = findListLoc(start, ticketsSize);if (loca >= listSize) {return false;}bool result = false;for (int m = 0; (m < list[loca].endSize); m++) {// 去重if (list[loca].used[m] == true) {continue;}// 保存该路径strncpy(path[pathSize], list[loca].end[m], NAME_LEN);pathSize++;list[loca].used[m] = true;bool res = backtracking(list[loca].end[m], ticketsSize);if (res == false) {// 回溯pathSize--;list[loca].used[m] = false;result = false;}else{return true;}}return result;
}char** findItinerary(char*** tickets, int ticketsSize, int* ticketsColSize, int* returnSize) {if (*ticketsColSize != 2) {return NULL;}// 初始化init(tickets, ticketsSize);strncpy(path[pathSize], "JFK", strlen("JFK"));pathSize++;(void)backtracking("JFK", ticketsSize);*returnSize = pathSize;return path;
}
回溯 + 哈希

C的哈希函数好难~

/*** Note: The returned array must be malloced, assume caller calls free().*/typedef struct {char *name;        /* key */int cnt;           /* 记录到达机场是否飞过了 */UT_hash_handle hh; /* makes this structure hashable */
} to_airport_t;typedef struct {char *name; /* key */to_airport_t *to_airports;UT_hash_handle hh; /* makes this structure hashable */
} from_airport_t;void to_airport_destroy(to_airport_t *airports) {to_airport_t *airport, *tmp;HASH_ITER(hh, airports, airport, tmp) {HASH_DEL(airports, airport);free(airport);}
}void from_airport_destroy(from_airport_t *airports) {from_airport_t *airport, *tmp;HASH_ITER(hh, airports, airport, tmp) {to_airport_destroy(airport->to_airports);HASH_DEL(airports, airport);free(airport);}
}int name_sort(to_airport_t *a, to_airport_t *b) {return strcmp(a->name, b->name);
}bool backtracking(from_airport_t *airports, int target_path_len, char **path,int path_len) {if (path_len == target_path_len) return true;from_airport_t *from_airport = NULL;HASH_FIND_STR(airports, path[path_len - 1], from_airport);if (!from_airport) return false;for (to_airport_t *to_airport = from_airport->to_airports;to_airport != NULL; to_airport = to_airport->hh.next) {if (to_airport->cnt == 0) continue;to_airport->cnt--;path[path_len] = to_airport->name;if (backtracking(airports, target_path_len, path, path_len + 1))return true;to_airport->cnt++;}return false;
}char **findItinerary(char ***tickets, int ticketsSize, int *ticketsColSize,int *returnSize) {from_airport_t *airports = NULL;// 记录映射关系for (int i = 0; i < ticketsSize; i++) {from_airport_t *from_airport = NULL;to_airport_t *to_airport = NULL;HASH_FIND_STR(airports, tickets[i][0], from_airport);if (!from_airport) {from_airport = malloc(sizeof(from_airport_t));from_airport->name = tickets[i][0];from_airport->to_airports = NULL;HASH_ADD_KEYPTR(hh, airports, from_airport->name,strlen(from_airport->name), from_airport);}HASH_FIND_STR(from_airport->to_airports, tickets[i][1], to_airport);if (!to_airport) {to_airport = malloc(sizeof(to_airport_t));to_airport->name = tickets[i][1];to_airport->cnt = 0;HASH_ADD_KEYPTR(hh, from_airport->to_airports, to_airport->name,strlen(to_airport->name), to_airport);}to_airport->cnt++;}// 机场排序for (from_airport_t *from_airport = airports; from_airport != NULL;from_airport = from_airport->hh.next) {HASH_SRT(hh, from_airport->to_airports, name_sort);}char **path = malloc(sizeof(char *) * (ticketsSize + 1));path[0] = "JFK";  // 起始机场backtracking(airports, ticketsSize + 1, path, 1);from_airport_destroy(airports);*returnSize = ticketsSize + 1;return path;
}

[51] N皇后

题目描述

51 N皇后
51 N皇后

解题思路

前提:……
思路:回溯
重点:……

代码实现

C语言
//待补充

[37] 解数独

题目描述

37 解数独
37 解数独

解题思路

前提:……
思路:回溯
重点:……。

代码实现

C语言
// 待补充

今日收获

  1. 收获不了一点,已晕菜。

这篇关于【代码随想录】【算法训练营】【第30天】 [322]重新安排行程 [51]N皇后 [37]解数独的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义