DFS poj2488 A Knight's Journey

很经典的搜索+最小字典序路径打印

巧妙的利用LAST结构体,来保存上一个节点的位置,最后再递归输出

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 30;
const int INF = 0x3f3f3f3f;

int m, n;
bool vis[MX][MX];
int dist[][2] = {{ -1, -2}, {1, -2}, { -2, -1}, {2, -1}, { -2, 1}, {2, 1}, { -1, 2}, {1, 2}};

struct LAST {
    int x, y;
} La[MX][MX], End;

bool DFS(int x, int y, int cnt) {
    if(cnt == m * n) {
        End.x = x;
        End.y = y;
        return true;
    }

    vis[x][y] = 1;
    for(int k = 0; k < 8; k++) {
        int nx = x + dist[k][0], ny = y + dist[k][1];

        if(nx < 1 || nx > m || ny < 1 || ny > n) continue;
        if(vis[nx][ny]) continue;

        La[nx][ny].x = x;
        La[nx][ny].y = y;
        if(DFS(nx, ny, cnt + 1)) {
            vis[x][y] = 0;
            return true;
        }
    }
    vis[x][y] = 0;
    return false;
}

void print(int x, int y, int cnt) {
    if(cnt == m * n) {
        return;
    }

    print(La[x][y].x, La[x][y].y, cnt + 1);

    printf("%c%d", 'A' + y - 1, x);
    if(!cnt) printf("\n");
}

int main() {
    int T, ansk = 0;
    scanf("%d", &T);

    while(T--) {
        memset(vis, false, sizeof(vis));
        End.x = -1;

        scanf("%d%d", &m, &n);
        bool sign = false;
        for(int j = 1; j <= n; j++) {
            for(int i = 1; i <= m; i++) {
                if(DFS(i, j, 1)) {
                    sign = true;
                    break;
                }
            }
            if(sign) break;
        }

        printf("Scenario #%d:\n", ++ansk);
        if(End.x != -1) {
            print(End.x, End.y, 0);
        } else {
            printf("impossible\n");
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值