161行的华容道程序

news/2025/10/22 15:16:58/文章来源:https://www.cnblogs.com/funwithwords/p/19158097

比吕震宇的慢多了,我的Intel N100上0.624s,他的兆芯KX-6640MA上0.314秒。

Clipboard01

写都写了,贴出来吧:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_set>
using std::unordered_set;
typedef unsigned char byte;const char* NM[] = { "", "", "", "", "", "", "", "", "", "" };
int W[] = { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1 };
int H[] = { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
const int D[4][2] = { 0, -1, 0, 1, -1, 0, 1, 0 };enum { QMAX = 36 * 1000 * 1000 };
struct State {uint64_t  xys; // xy坐标们byte b[5][4];int p; // 局面路径的previous// int next; // Hash表里的nextvoid clear () { xys = 0; memset(b, 0, sizeof(b)); }State& operator= (int a[10][2]);void toary(int a[10][2]);void operator= (const char* s);void print(const char* s = "");bool _can_move (int a[10][2], int i, int j);bool can_move (int a[10][2], int i, int j);
} states[QMAX + 40];
int qh, qt = 1; // queue head, tail
unordered_set<uint64_t> seen; // 改Hash表,和states二合一

State& State::operator= (int a[10][2]) {for (int i = 0; i < 10; i++) {const uint64_t  x = a[i][0], y = a[i][1];xys |= ((x << 3) | y) << (i * 5); // gcc不-O都会换成<<2再+1for (int yy = y; yy < y + H[i]; yy++)for (int xx = x; xx < x + W[i]; xx++)b[yy][xx] = 1;}return *this;
}void State::toary (int a[10][2]) {for (int i = 0; i < 10; i++) {int xy = xys >> (i * 5);a[i][0] = (xy >> 3) & 3; // xa[i][1] = xy & 7; // y
  }
}void State::operator= (const char* s) {int a[10][2] = {}, g = 1, p = 6;for (int x = 3; x >= 0; x--)for (int y = 4; y >= 0; y--) {#define CASE(c, i) case c: a[i][0] = x; a[i][1] = y; break;switch (s[x * 5 + y]) {CASE('c', 0) CASE('g', 1) // 曹关CASE('z', 2) CASE('h', 3) // 张黄CASE('l', 4) CASE('m', 5) // 子龙(l) 马case 'p': a[p][0] = x; a[p++][1] = y;}}static const char*  S[] = { "gg", "zz", "hh", "ll", "mm" };for (int i = 0; i < 5; i++)if (strstr(s, S[i])) W[i+1] = 1, H[i+1] = 2;*this = a;
}void State::print (const char* s) {const char* b[5][4] = {};int a[10][2]; toary(a);for (int i = 0; i < 10; i++) {int x = a[i][0], y = a[i][1];for (int yy = y; yy < y + H[i]; yy++)for (int xx = x; xx < x + W[i]; xx++)b[yy][xx] = NM[i];}for (int y = 0; y < 5; y++) {for (int x = 0; x < 4; x++) printf("%s", b[y][x] ? : " ");puts("");}printf("%s\n", s);
}bool State::can_move (int a[10][2], int i, int j) {int  ox = a[i][0], oy = a[i][1];bool ok = _can_move(a, i, j);char s[256];sprintf(s, "%s: %d,%d -> %d,%d %s", NM[i], ox, oy, a[i][0], a[i][1], ok ? "OK" : "");print(s); getchar();return ok;
}bool State::_can_move (int a[10][2], int i, int j) {const int ox = a[i][0], oy = a[i][1];int x = (a[i][0] += D[j][0]), y = (a[i][1] += D[j][1]);if (x < 0 || x + W[i] > 4 || y < 0 || y + H[i] > 5) return false;//D[4][2] = { 0,-1, 0,1, -1,0, 1,0 };if (j == 0) {for (int w = 0; w < W[i]; w++) if (b[y][x + w]) return false;return true;}else if (j == 1) {y = oy + H[i];for (int w = 0; w < W[i]; w++) if (b[y][x + w]) return false;return true;}else if (j == 2) {for (int h = 0; h < H[i]; h++) if (b[y + h][x]) return false;return true;}else {x = ox + W[i];for (int h = 0; h < H[i]; h++) if (b[y + h][x]) return false;return true;}
}void print_path () {// 用数组存放的单链表就地翻转int prev = -1, next = -1;for (int cur = qh; cur != -1;) {next = states[cur].p;states[cur].p = prev;prev = cur; cur = next;}int n = 0;for (int p = 0; p != -1; p = states[p].p) {// states[p].print();++n;}printf("%d\n", n);
}int main () {//states[0] = "pp zz""ccghh""ccgll""pp mm";states[0] = "pzzpg""cc  g""ccllm""phhpm";states[0].print();states[0].p = -1;seen.insert(states[0].xys);for (; qh < qt; qh++) {int a[10][2]; states[qh].toary(a);if (a[0][1] == 3) { print_path(); break; }for (int i = 0; i < 10; i++)for (int j = 0; j < 4; j++) {if (states[qh]._can_move(a, i, j)) {states[qt] = a;if (seen.find(states[qt].xys) == seen.end()) {seen.insert(states[qt].xys);states[qt++].p = qh;}else states[qt].clear();}a[i][0] -= D[j][0]; a[i][1] -= D[j][1];}}printf("%d\n", qt);return 0;
}
View Code

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/943400.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

调用ack集群 api 接口删除Terminating状态的资源

调用ack集群 api 接口删除Terminating状态的资源背景:在特殊情况,会出现删除不掉资源的情况 1、导出json 文件资源 kubectl get namespace arms-prom -o json > temp.json 2、去掉finallizers 3、开启代理到api服务…

二十三、K8s企业级架构设计及落地

二十三、K8s企业级架构设计及落地 目录二十三、K8s企业级架构设计及落地1、K8s企业级架构设计1.1 Kubernetes集群架构--高可用1.2 K8s生产环境资源配置1.3 K8s生产环境磁盘划分1.4 k8s集群网段划分2、基本环境配置2.1 …

题解:P9464 [EGOI 2023] Padel Prize Pursuit / 追梦笼式网球

目前暂无修正。选手无法观察到树形结构,于是选手写了比较没脑子的可持久化线段树做法。 我们考虑第 \(i\) 个奖牌会到哪里去,发现每个关系 \((X,Y)\) 意思是此时 \(Y\) 的奖牌会被 \(X\) 赢走,即奖牌此时到 \(Y\) 后…

Spring Boot 中@RestController注解的详解和运用

Spring Boot 中@RestController注解的详解和运用2025-10-22 15:09 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display…

软件工程课程第二次团队作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/202501SoftwareEngineering/这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/202501SoftwareEngineering/homework/13559这个作业的目标 构建一个能…

AGC 板刷记录2

AT_agc073_a [AGC073A] Chords and Checkered 题解 自己手画几组,没添加一条线其实就是把穿过的区域复制添加一份颜色反转的,一块区域如果是黑的,一定是被奇数条线覆盖。我们将其拆成两部分,第一部分是只有一条线围…

2025 年涿州装修公司最新推荐榜,深度解析企业服务能力与市场口碑优势

涿州作为环京核心区域,装修市场已聚集超 1500 家注册企业,但行业内资质参差、报价混乱、工艺缩水等问题频发,不少业主因选错服务商陷入工期延误、增项加价的困境。为破解这一难题,本榜单基于企业综合实力、施工标准…

结对编程项目总结

项目 GitHub 地址:https://github.com/LoadStar822/Elevator我们把结对开发的里程碑、算法设计心得以及协作复盘一起整理在这份文档里,方便后续直接发布到博客或项目页。全文以“先数据、再故事”的顺序铺陈,读者可…

刘强东带火数字人直播?商业化逐步成熟,逐渐取代真人带货!zhibo175

4月16日晚6点18分,刘强东准时出现在京东家电家居采销直播间和京东超市采销直播间。 不过,此次出镜带货的并非刘强东本人,而是其数字虚拟人分身“采销东哥”。开播不足半小时,两大直播间就吸引了超1000万次观看。 相…

Hive事务管理详解:从ACID原理到UPDATE/DELETE实战 - 实践

Hive事务管理详解:从ACID原理到UPDATE/DELETE实战 - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consola…

TabControl控件

TabControl控件,页面集合 用于管理一个TabPages集合,每个TabPage都是一个容器控件 常用属性: MultiLine,TabPages,AlignMent,Appearance,ItemSize,ImagesList 知识点1: MultiLine,是否允许多行选项卡 AlignM…

权威调研榜单:硬质合金挤压模具厂家TOP3综合实力深度解析

权威调研榜单:硬质合金挤压模具厂家TOP3综合实力深度解析 随着制造业向高端化、精密化方向发展,硬质合金挤压模具作为精密加工领域的核心工具,其性能直接影响产品质量和生产效率。根据行业调研数据显示,2024年我国…

详细介绍:【Linux指南】gdb进阶技巧:断点高级玩法与变量跟踪实战

详细介绍:【Linux指南】gdb进阶技巧:断点高级玩法与变量跟踪实战pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "…

Nacos 3.1.0 正式发布,支持 A2A 注册中心与 MCP 注册协议增强

Nacos 社区正式发布 3.1.0 版本!作为全新的里程碑版本,3.1.0 在 A2A(Agent-to-Agent)注册中心和 MCP(Model-Context-Protocol)注册中心两大核心能力上实现重大突破,同时修复多项历史问题并升级关键依赖。作者:…

2025 年点火器厂家最新推荐排行榜:综合评估高能 / 自动 / 防爆等多类型产品,精选优质品牌

在工业生产、民生应用等领域,点火器作为核心设备,其性能好坏直接关系到生产效率提升、作业安全保障以及能源消耗控制。当前点火器市场呈现品牌数量多、产品质量差异大的特点,部分品牌因技术滞后,生产的点火器存在点…

VS2026 使用 WebDeploy 发布到 IIS - Jeff

这里有B站的一位up发的视频 - 博文只是为了记录一下大体步骤,主要是记录最后的问题以及解决方案,因为遇到的问题在网上搜不到。通过使用Visual Studio将你的程序WebDeploy一键发布到windows的IIS_哔哩哔哩_bilibili …

2025 激光灯厂家最新推荐榜:全方位测评核心实力与潜力,甄选优质供应商实用指南

引言 2025 年激光灯行业迎来技术迭代与新品牌爆发的双重浪潮,市场呈现 “老品牌深耕、新势力突围” 的格局,但选型难题愈发突出。部分厂商偷工减料导致产品性能不稳定,中小品牌技术滞后难以适配文旅亮化、商业演艺等…

SpringBoot3 集成Junit4 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

详细介绍:Spark Shuffle:分布式计算的数据重分布艺术

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025 年火焰检测器生产厂家最新推荐权威排名:涵盖防爆 / 一体化 / 紫外线 / 离子 / 红外线 / 红紫外复合 / 智能型,多维度解析助力企业精准选型

引言 当前工业领域对火焰检测器的需求日益严苛,不同场景下需匹配防爆、一体化、紫外线等多种类型产品,而市场中厂家技术水平悬殊,部分产品存在检测精度不足、适应复杂工况能力弱等问题,导致企业选型时易陷入 “选贵…