puzzle(0919)六宫数局
创始人
2024-05-30 15:46:49
0

目录

六宫数局

示例题目

简单模式

普通模式

困难模式


六宫数局

最强大脑同款项目。

找出一条给定起点和终点的路径,每一步的方向任选,在这个方向上移动的步数是当前数的质因数分解中2、3、5的次数。

 

示例题目

 按照六边形坐标系来建立坐标系,用BFS算法求解:

#include 
#include
#include
using namespace std;struct Node
{int x,y;
};int GetLen(Node a, Node b)
{if (a.x > b.x)return GetLen(b, a);int dx = b.x - a.x;int dy = abs(b.y - a.y);if (dx % 2 == 0) {if (dy <= dx / 2)return dx;return dx + dy - dx / 2;}int ym = dx / 2 + ((a.x + (b.y < a.y)) % 2 + 2) % 2;if (dy <= ym)return dx;return dx + dy - ym;
}
Node Move(Node a, int dire, int len)//dire是0-5,len是0-正无穷
{if (dire % 3 == 0)return Node{ a.x, a.y + (dire == 0 ? 1 : -1) * len };if (dire == 1 || dire == 5)return Node{ a.x + (dire == 1 ? 1 : -1)*len,  a.y + len / 2 + (len % 2 ? (a.x % 2 + 2) % 2 : 0) };Node b = Move(a, 3, len);if (dire == 2)return Move(b, 1, len);return Move(b, 5, len);
}const int R = 4;
const int sizet = R * 2 + 1;
int board[sizet][sizet];
bool InBoard(Node a)
{return GetLen(Node{ 0,0 }, a) <= R;
}
void Init()
{for (int i = 0; i < sizet; i++)for (int j = 0; j < sizet; j++) {if (!InBoard(Node{ i - R,j - R })) {continue;}cin >> board[i][j];}
}
void bfs(Node a)
{queueq;q.push(a);mapm;m[a.x*R * 3 + a.y] = 1;int p[] = { 5,2,3,5,3,2 };while (!q.empty()) {Node k = q.front();q.pop();int n = board[k.x + R][k.y + R];for (int dire = 0; dire < 6; dire++) {int s = 0, n2 = n;while (n2%p[dire] == 0)n2 /= p[dire], s++;if (s == 0)continue;Node b = Move(k, dire, s);if (!InBoard(b))continue;if (m[b.x*R * 3 + b.y] == 0)q.push(b);m[b.x*R * 3 + b.y] = 1;cout << n << "->" << board[b.x + R][b.y + R] << endl;}}
}int main()
{//freopen("D:/in.txt", "r",stdin);Init();bfs(Node{ 0,-R });return 0;
}

输入:

308 454 219 304 248
271 416 473 291 361 392 
286 330 875 175 367 472 266
434 432 164 621 316 269 450 484
54 332 103 328 300 494 391 115 413
244 376 370 131 356 426 495 74
215 409 235 457 401 346 290
625 317 459 321 513 265
427 112 172 364 131

输出:

54->244
54->434
244->625
244->432
434->332
434->286
625->513
432->459
332->409
332->330
286->432
286->271
513->300
459->103
330->875
330->164
330->432
330->286
330->271
330->416
300->391
300->401
300->131
300->103
300->621
300->367
875->472
164->131
164->473
416->401
621->409
621->308
472->74
308->875
74->413

求出来的最短路比示例路径短3步。

简单模式

普通模式

困难模式

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...