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


    Description

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

    Input

    输入第一行为测试数据组数。从第二行开始每行3个整数n(3<n<10)、x、y,代表棋盘的大小,和初始坐标。

    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 <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    //#include <tr1/unordered_map>
    #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<int, int> 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("
    ");
        }
    }
     
    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:
    ", 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;
    }
    
  • 相关阅读:
    排列组合算法
    C++内存管理——堆&&栈
    编程之美——1.2 中国象棋将帅问题
    Gentoo: fcitx的安装
    Gentoo NTFS USB盘有写权限
    Gentoo U盘无法自动挂载,打开报告Not Authorized,xfce只有logout,suspend/shutdown灰化等问题解决方法
    Kernel: 打开CONFIG_EMBEDDED从而使更多的kernel option可以更改
    Gentoo Enable framebuffer console (没有安装X,KDE的时候)
    转载:Gentoo和Ubuntu包管理命令对比集
    Gentoo Rebuild virtualboxmodules when kernel is updated
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5990715.html
Copyright © 2020-2023  润新知