【NC235814】马踏棋盘

题目

马踏棋盘

DFS、回溯

思路

回溯模板题,这道题不仅仅是深度优先搜索,而关键在于回溯的思想,具体见代码。注意这道题中的 n , m n,m n,m 的含义。

代码

#include <stdio.h>
#define N 20

const int TO[4][2] = {{1, 2}, {1, -2}, {2, 1}, {2, -1}};

char vis[N][N];
int n, m, ans;

void dfs(int x, int y) {
    if (x == n - 1 && y == m - 1) {
    	// 如果到了终点,则将答案加一
        ans++;
        return;
    }
    for (int i = 0; i < 4; i++) {
        int tx = x + TO[i][0];
        int ty = y + TO[i][1];
        if (tx < 0 || tx >= n || ty < 0 || ty >= m || vis[tx][ty]) {
            continue;
        }
        vis[tx][ty] = 1;
        dfs(tx, ty);
        // 回溯
        vis[tx][ty] = 0;
    }
}

int main(void) {
    scanf("%d%d", &m, &n);
    dfs(0, 0);
    printf("%d\n", ans);
    return 0;
}

相关推荐

  1. NC235814棋盘

    2024-04-13 19:40:04       32 阅读
  2. 2733: 【搜索】【广度优先】 遍历棋盘

    2024-04-13 19:40:04       43 阅读
  3. Python【走出棋盘

    2024-04-13 19:40:04       50 阅读
  4. KY98 棋盘游戏

    2024-04-13 19:40:04       56 阅读
  5. 【算法】棋盘(模板题)

    2024-04-13 19:40:04       41 阅读
  6. 【noip普及组】棋盘

    2024-04-13 19:40:04       41 阅读
  7. Opencv_棋盘格标定相机

    2024-04-13 19:40:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 19:40:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 19:40:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 19:40:04       87 阅读
  4. Python语言-面向对象

    2024-04-13 19:40:04       97 阅读

热门阅读

  1. Python将数据写入Mysql

    2024-04-13 19:40:04       46 阅读
  2. C++ list

    2024-04-13 19:40:04       33 阅读
  3. location.href和 window.location的区别有这些!

    2024-04-13 19:40:04       34 阅读
  4. 无重复字符的最长子串

    2024-04-13 19:40:04       34 阅读
  5. 蓝桥杯2021年第十二届省赛 C&C++ 研究生组2.0

    2024-04-13 19:40:04       38 阅读
  6. 安卓Android.nfc读卡

    2024-04-13 19:40:04       35 阅读
  7. 执行 npm run serve 时发生了什么?

    2024-04-13 19:40:04       38 阅读