• 马走日 (DFS)


    马在中国象棋以日字形规则移动。

    请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点。

    Input第一行为整数T(T < 10),表示测试数据组数。 
    每一组测试数据包含一行,为四个整数,分别为棋盘的大小以及初始位置坐标n,m,x,y。(0<=x<=n-1,0<=y<=m-1, m < 10, n < 10)Output每组测试数据包含一行,为一个整数,表示马能遍历棋盘的途径总数,0为无法遍历一次。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
    ",(x))
    #define yes printf("YES
    ");
    #define no printf("NO
    ");
    #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;
    }
    所遇皆星河
  • 相关阅读:
    springmvc介绍
    mybatis中的动态sql应用
    mybatis中表的关联
    mybatis分页
    聚类评估指标系列(二):准确率和F值
    混淆矩阵,准确率,召回率,F-score,PR曲线,ROC曲线,AUC
    聚类评估指标系列(一):标准化互信息NMI计算步骤及其Python实现
    numpy.where() 用法详解
    互信息Mutual Information
    转:Prewitt算子、Sobel算子、canny算子、Lapacian算子
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11546275.html
Copyright © 2020-2023  润新知