如何看网站是谁做的/网页搜索引擎
- 博客主页: https://blog.csdn.net/qq_50285142
- 欢迎点赞👍收藏✨关注❤留言 📝 如有错误,敬请指正
- 🎈虽然生活很难,但我们也要一直走下去🎈
题目链接
题意:
马在中国象棋以日字形规则移动。
请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点
总的来说就是dfs
每个方向进行dfs,每个方向分支出一种情况,用st[]st[]st[]数组设置智能遍历一次,并且每次遍历之后要进行回溯,因为有多种情况分支,该种情况遍历到最终时,如果对st[]st[]st[]数组不进行回溯,会影响其他的分支
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int>vi;
typedef pair<int,int> pii;const int dx[] = {-1,-2,-2,-1,1,2,2,1};
const int dy[] = {-2,-1,1,2,2,1,-1,-2};
const int N = 1e5+5,M = 1e5+5;
int n,m,k,res;
bool st[15][15];void dfs(int x,int y,int cnt)
{if(cnt==n*m){res ++;return ;}st[x][y] = true;for(int i=0;i<8;i++){int nx = x + dx[i],ny = y + dy[i];if(nx < 0 or nx >= n or ny < 0 or ny >= m) continue;if(st[nx][ny]) continue;dfs(nx,ny,cnt+1);}st[x][y] = false;
}void solve()
{int x,y;cin>>n>>m>>x>>y;res = 0;dfs(x,y,1);cout<<res<<'\n';
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0),cout.tie(0);int _;cin>>_;
// _ = 1;while(_--){solve();} return 0;}
往期优质文章推荐
- 💛💚C++ STL详解,超全总结(快速入门STL)
- 💛🧡【期末复习】c++知识点大回顾,八篇文章让你永不破防(一)
- 💛💚区间贡献问题习题详解