• [sdut] 1400 马的走法 dfs


    Problem Description

    在一个4*5的棋盘上,马的初始位置坐标(纵 横)位置由键盘输入,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走“日”字)。如果马的初始位置坐标超过棋盘的边界,则输出ERROR。例如初始位置为4 6,则输出ERROR。

    Input

    输入数据只有一行,有两个用空格分开的整数,表示马所在的初始位置坐标。首行首列位置编号为(1 1)。

    Output

    输出一行,只有一个整数,表示马能返回初始位置的所有不同走法的总数。

    如果输入的马的初始位置超出棋盘边界,则输出ERROR。

    Example Input

    2 2

    Example Output

    4596

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    bool vis[5][6];
    int dir[2][8] = {{-1,1,2,2,1,-1,-2,-2},{2,2,1,-1,-2,-2,-1,1}};
    int X, Y;
    
    int cnt = 0;
    
    void dfs(int x, int y)
    {
        int dx, dy;
        for (int i = 0; i < 8; i++)
        {
            dx = dir[0][i] + x;
            dy = dir[1][i] + y;
            if (dx >= 1 && dx <= 4 && dy >= 1 && dy <= 5
                && !vis[dx][dy]) {
                vis[dx][dy] = 1;
                dfs(dx, dy);
                vis[dx][dy] = 0;
            }
            else if (dx == X && dy == Y)
                cnt++;
        }
    
    }
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        cin >> X >> Y;
        if (X < 1 || X > 4 || Y < 1 || Y > 5) {
            printf("ERROR
    ");
            return 0;
        }
        memset(vis, 0, sizeof(vis));
        cnt =  0;
        vis[X][Y] = 1;
        dfs(X, Y);
        cout << cnt << endl;
    
        return 0;
    }
     
  • 相关阅读:
    小木虫等论坛自动签到程序发布
    第一篇随笔
    工作和兴趣哪个更重要?
    weblogic 启动时 报错
    C#动态调用webservice (转载)
    U盘加载,卸载,拔出,插入(转载)
    Simple zip archive unzipper(转载)
    Best C# Blogs(强烈推荐)
    数据无法导入ArcSDE
    Oracle10.2.0.1.0升级Oracle10.2.0.2.0补丁安装指南(转载)
  • 原文地址:https://www.cnblogs.com/whileskies/p/7168362.html
Copyright © 2020-2023  润新知