马的遍历问题java_数据结构 马的遍历问题 (暴力回溯)

这是一个关于在n*n棋盘上,使用马按照象棋规则不重复遍历每个位置的编程问题。通过暴力回溯法解决,代码中包括优化技巧,例如对n的奇偶性和起点坐标奇偶性的判断。当n小于等于4或n为奇数且起点横纵坐标奇偶性不同时,无解。示例代码用C++编写,包含回溯搜索的实现细节。
摘要由CSDN通过智能技术生成

Description

在n*n棋盘上,对任一位置上放置的一个马,均能选择一个合适的路线,使得该棋子能按象棋的规则不重复地走过棋盘上的每一位置。

Input

输入第一行为测试数据组数。从第二行开始每行3个整数n(3

Output

输出字典序最小的可行解,无解输出“No solution.”。格式见样例。

Sample Input

1

6 6 6

Sample Output

Case #1:

7 4 9 12 15 36

10 21 6 3 30 13

5 8 11 14 35 16

22 25 20 31 2 29

19 32 27 24 17 34

26 23 18 33 28 1

HINT

Append Code

析:暴力,回溯,从马开始的位置进行模拟,马只有8个方向,并且由于要字典序最小,那么就从左上角,然后是右上角,左下角,右下角,这个顺序来遍历。

但是这样还是过不了,TLE,我们还要进行优化,找找规律你会发现,首先 n <= 4无解,因为根本跳不开,再就是 n 为奇数,并且马开始的横纵坐标,奇偶性不同,

这样都是无解,然后经过这个优化就能过了,并且后台数据应该没有 n 为9的情况,要不然还得T。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//#include

#define freopenr freopen("in.txt", "r", stdin)

#define freopenw freopen("out.txt", "w", stdout)

using namespace std;

//using namespace std :: tr1;

typedef long long LL;

typedef pair P;

const int INF = 0x3f3f3f3f;

const double inf = 0x3f3f3f3f3f3f;

const LL LNF = 0x3f3f3f3f3f3f;

const double PI = acos(-1.0);

const double eps = 1e-8;

const int maxn = 5e4 + 5;

const LL mod = 10000000000007;

const int N = 1e6 + 5;

const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};

const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};

const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};

const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};

const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }

int n, m;

const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline int Min(int a, int b){ return a < b ? a : b; }

inline int Max(int a, int b){ return a > b ? a : b; }

inline LL Min(LL a, LL b){ return a < b ? a : b; }

inline LL Max(LL a, LL b){ return a > b ? a : b; }

inline bool is_in(int r, int c){

return r >= 0 && r < n && c >= 0 && c < m;

}

int a[15][15];

void print(){

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j)

printf("%3d", a[i][j]);

printf("\n");

}

}

bool dfs(int r, int c, int cnt){

for(int i = 0; i < 8; ++i){

int x = r + hr[i];

int y = c + hc[i];

if(!is_in(x, y) || a[x][y]) continue;

a[x][y] = cnt;

if(cnt == n * m){ print(); return true; }

if(dfs(x, y, cnt+1)) return true;

a[x][y] = 0;

}

return false;

}

int main(){

int T; cin >> T;

for(int kase = 1; kase <= T; ++kase){

int x, y;

scanf("%d %d %d", &n, &x, &y);

m = n;

for(int i = 0; i < n; ++i)

for(int j = 0; j < m; ++j)

a[i][j] = 0;

a[x-1][y-1] = 1;

printf("Case #%d:\n", kase);

if(n <= 4 || ((n&1) && ((x+y)&1))){ puts("No solution."); continue; }

if(!dfs(x-1, y-1, 2)) puts("No solution.");

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值