其他分享
首页 > 其他分享> > POJ 4123:马走日

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;
        }
    }
}

标签:4123,ll,st,mm,second,POJ,马走,include,first
来源: https://blog.csdn.net/weixin_44417851/article/details/97328486