Horses go day (DFS)

Ma moved to B shape the rules of chess in China.

Please write a program, a given board size n * m, and the initial position of the horse (x, y), a request can not be repeated through the same point on the board, the horse can calculate how many routes through all points on the board.

Input first line integer T (T <10), indicates the number of test data sets. 
Each set of test data contains a line of four integers, respectively, the board size and an initial position coordinate n, m, x, y. (0 <= x <= n -1,0 <= y <= m-1, m <10, n <10) Output test data comprising each line, is an integer, the total number of horses can traverse the route board representation, 0 can not be traversed once. Sample Input

1
5 4 0 0

Sample Output

32



#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define ull unsigned long long
#define lli long long
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl;
//二叉树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1
//int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int t,n,m,k,x,y,col,ex,ey,ans,cnt;
int a[202][202],b[202][202];
int vis[11][11];
struct node {    int x,y,t;};
void DFS(int r,int c)
{
    if(ans>=n*m) {cnt++;return ;}
    for(int i=0;i<8;i++)
    {
        int nx = r + dir[i][0];
        int ny = c + dir[i][1];
        if(nx<0 || ny<0 ||nx >=n ||ny >=m) continue;
        if(!vis[nx][ny])
        {
            vis[nx][ny] = 1;
            ans++;
            DFS(nx,ny);
            ans--;
            vis[nx][ny] = 0;
        }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                vis[i][j]=0;
        vis[x][y] = 1;
        ans=1;cnt=0;
        DFS(x,y);
        cout<<cnt<<endl;
    }
    ok;
}

 

Guess you like

Origin www.cnblogs.com/Shallow-dream/p/11546275.html
dfs