zoukankan      html  css  js  c++  java
  • [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;
    }
     
  • 相关阅读:
    输出国际象棋&&输出余弦曲线
    打鱼晒网问题
    ATM模拟程序
    getline()函数
    AC小笔记
    ural 1208 Legendary Teams Contest
    汇编贪吃蛇
    供给与需求的市场力量
    垄断竞争
    相互依存性和贸易的好处
  • 原文地址:https://www.cnblogs.com/whileskies/p/7168362.html
Copyright © 2011-2022 走看看