信息学奥赛一本通 1219:马走日 (粑粑)

在这里插入图片描述

http://ybt.ssoier.cn:8088/problem_show.php?pid=1219

#include<bits/stdc++.h>
using namespace std;
int a[20][20];
int v[20][20];
int go[8][2] = {{1,2},{2,1},{-1,2},{2,-1},{-1,-2},{-2,-1},{-2,1},{1,-2}};
int cns;
int n,m,x,y,T;
int dian=1; // 开始就是1 
void print(){
	for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			cout<<v[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	cout<<endl;
}
void dfs(int x,int y){
	// 如果dian已经有了这么多次,就满足全部搞掂,cns++  
	if(dian==n*m){ 
		cns++;
//		print();
		return;
	}
	for(int i=0;i<8;i++){
		int xx = x + go[i][0];
		int yy = y + go[i][1];
		if(xx>=0 && yy>=0 && xx<n && yy<m && v[xx][yy]==0){
			v[xx][yy]++;
			dian++;
			dfs(xx,yy);
			v[xx][yy] = 0;
			dian--;
		}
	}
	return;
}
int main(){
	freopen("cpp.in","r",stdin);
	freopen("cpp.out","w",stdout);
	cin>>T;
	while(T--){
		cin>>n>>m>>x>>y;
		memset(v,0,sizeof(v));
		v[x][y] = 1; //记得要一开始就将起始点设置成 1 
		cns=0;
		dfs(x,y);
		cout<<cns<<endl;
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值