POJ 2488 A Knight's Journey

题意:给一个n×m的棋盘,如果一个骑士可以从任意一个位置出发不重复的走遍棋盘的每个格子就输出字典序最短的路径。

解法:dfs。暴搜n×m次,只是被字典序输出坑了……而且字母是列序号数字是行序号……这两个总弄反……搜索的时候会只要按字典序搜那8个方向就可以了,搜到第一条满足条件的路径就结束。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long

using namespace std;

int dir[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -2, 1, 2, 1, -1, 2, 1, 2};
struct node
{
    int x, y;
    node(int x, int y) : x(x), y(y) {}
    node() {}
};
int n, m;
bool vis[30][30];
node path[30][30];
bool dfs(node tmp, int step)
{
    if(step == n * m)
        return true;
    for(int i = 0; i < 8; i++)
    {
        int tx = tmp.x + dir[i][0], ty = tmp.y + dir[i][1];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty])
        {
            path[tmp.x][tmp.y] = node(tx, ty);
            vis[tx][ty] = 1;
            if(dfs(node(tx, ty), step + 1))
                return true;
            vis[tx][ty] = 0;
        }
    }
    return false;
}
int main()
{
    int T;
    while(~scanf("%d", &T))
    {
        int cse = 1;
        while(T--)
        {
            scanf("%d%d", &n, &m);
            int ans = 1;
            int x, y;
            for(int i = 0; i < n && ans; i++)
            {
                for(int j = 0; j < m && ans; j++)
                {
                    memset(vis, 0, sizeof vis);
                    x = i, y = j;
                    vis[i][j] = 1;
                    if(dfs(node(i, j), 1))
                        ans = 0;
                }
            }
            printf("Scenario #%d:
", cse++);
            if(!ans)
            {
                for(int i = 0; i < n * m; i++)
                {
                    printf("%c%d", 'A' + y, x + 1);
                    node tmp = path[x][y];
                    x = tmp.x, y = tmp.y;
                }
                puts("");
            }
            else
                puts("impossible");
            puts("");
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Apro/p/4664748.html