POJ 4123:马走日

POJ 4123:马走日

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>

#define ll long long
#define ull unsigned long long
#define dd double
#define mes(x, y) memset(x,y,sizeof(x))
using namespace std;
ll hang, lie, sum;
map<pair<ll, ll>, ll> mm;
ll moving[10][10] = {{-1, 2},
                     {1, 2},
                     {2, 1},
                     {2,  -1},
                     {1,  -2},
                     {-1,  -2},
                     {-2,  -1},
                     {-2, 1}};

bool Around(ll x, ll y) {
    return (x >= 0 && x < hang && y >= 0 && y < lie);
}

void DFS(ll comingx, ll comingy, ll step) {
    if (step == hang * lie) {
        sum++;
        return;
    }
    for (ll i = 0; i <= 7; i++) {
        pair<ll, ll> st;
        st.first = comingx + moving[i][0];
        st.second = comingy + moving[i][1];
        if (Around(st.first, st.second) && !mm[st]) {
            mm[st] = 1;
            DFS(st.first, st.second, step + 1);
            mm[st] = 0;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, k;
    while (cin >> n) {
        while (n--) {
            pair<ll, ll> st;
            mm.clear();
            cin >> hang >> lie >> st.first >> st.second;
            mm[st] = 1;
            sum = 0;
            DFS(st.first, st.second, 1);
            cout << sum << endl;
        }
    }
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/97328617