文章

1

粉丝

130

获赞

0

访问

2.2k

头像
DFS回溯,通过50%
P1628 上海交通大学2018年机试题
发布于2023年3月20日 15:58
阅读数 2.2k

#include <queue>
#include <cstdio>
#include <vector>

using namespace std;

struct State {
    int x,y;
    int count;
    State() {
        count = 0;
        x = 0;
        y = 0;
    }
};

int moves[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2,1}, {-2, -1}, {-1, -2}, {1, -2}};


int main () {
    int n,m;
    while (scanf("%d%d", &n, &m) != EOF) { // n : y 轴 m : x轴

        queue<State> sQueue;
        vector<vector<bool>> visited(m, vector<bool>(n, false));

        State s;
        sQueue.push(s);

        bool flag = false;
        while (!sQueue.empty()) {
            s  = sQueue.front();
            sQueue.pop();
            visited[s.x][s.y] = true;

            if (s.count == n * m) {
                flag = true;
                break;
            }


            for (int i = 0; i < 8; ++i) {
                int nx = moves[i][0] + s.x, ny = moves[i][1] + s.y;
                if (nx &...
登录查看完整内容


登录后发布评论

1 条评论
admin SVIP
2023年3月20日 21:43

你这个程序1*1的时候有问题

赞(0)