【代码随想录】【算法训练营】【第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

相关文章

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计