欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

计蒜客T1217 马走日(DFS)

程序员文章站 2022-09-21 14:42:56
计蒜客T1217 马走日思路:DFS模板题,注意马可以跳8个方向,DFS是否回溯。代码如下:#include#includeusing namespace std;int vis[20][20]={0};int xx[]={1,1,2,2,-1,-1,-2,-2};int yy[]={-2,2,1,-1,2,-2,1,-1};int sum; int n,m,x,y;void DFS(int x,int y,int coun...

计蒜客T1217 马走日

计蒜客T1217 马走日(DFS)
思路:DFS模板题,注意马可以跳8个方向,DFS是否回溯。

代码如下:

#include<cstdio>
#include<iostream>
using namespace std;
int vis[20][20]={0};
int xx[]={1,1,2,2,-1,-1,-2,-2};
int yy[]={-2,2,1,-1,2,-2,1,-1};
int sum; 
int n,m,x,y;
void DFS(int x,int y,int count){
	if(count==n*m){
		sum++;
		return ;
	}
	for(int i=0;i<8;i++){
		int tempx=x+xx[i];
		int tempy=y+yy[i];
		if(tempx>=0 && tempy>=0 && tempx<n && tempy<m && vis[tempx][tempy]==0){
			vis[tempx][tempy]=1;
			DFS(tempx,tempy,count+1);
			vis[tempx][tempy]=0;
		}
	}
	return;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n>>m>>x>>y;
		vis[x][y]=1;
		sum=0;
		DFS(x,y,1);
		cout<<sum<<endl;
	}
	return 0;
} 

本文地址:https://blog.csdn.net/Mxeron/article/details/108990149

相关标签: DFS 算法