电子商务网站建设自服务器杭州海淀区网站建设
目录
六宫数局
示例题目
简单模式
普通模式
困难模式
六宫数局
最强大脑同款项目。
找出一条给定起点和终点的路径,每一步的方向任选,在这个方向上移动的步数是当前数的质因数分解中2、3、5的次数。

示例题目

按照六边形坐标系来建立坐标系,用BFS算法求解:
#include <iostream>
#include<string>
#include<string.h>
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)
{queue<Node>q;q.push(a);map<int, int>m;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步。
简单模式

普通模式

困难模式

