这种思路对么
查看原帖
这种思路对么
1076727
lvjuan楼主2024/7/20 19:43
#include <bits/stdc++.h>
using namespace std;
int x1,y1,x2,y2;

//生成马可以跳到的坐标
int xx1[10] = {x1-2,x1-1,x1-2,x1+1,x1-1,x1+2,x1+1,x1+2};	
int yy1[10] = {y1+1,y1+2,y1-1,y1+2,y1-2,y1+1,y1-2,y1-1};

int ans=0;


void dfs(int x,int y,int temp){//temp为1向下运动一格,为0向右一格
    //cout<<x<<" "<<y<<endl;//测试 
     
	//在边界范围内才可以运动
	if(temp==1&&y<y2)y--;
    if(temp==0&&x<x2)x++;
    //cout<<x<<" "<<y<<endl;



	//遇到马可以跳到以及马不跳时的所在位置就return
	if(x==x1&&y==y1){
//		if(temp==1&&y<y2)y++;
//        if(temp==0&&x<x2)x--;
		return;
	}
	for(int i=0;i<8;i++){
		//cout<<xx1[i]<<" "<<yy1[i]<<endl;
		if(x==xx1[i]&&y==yy1[i]){
//		if(temp==1&&y<y2)y++;
//        if(temp==0&&x<x2)x--;
        //cout<<x<<" "<<y<<endl;
		return;
		
		}
	}
	
	
	// 碰到目标就ans++,然后回溯
	if(x==x2&&y==y2){
		//cout<<ans<<" ";
//		if(temp==1&&y<y2)y++;
//        if(temp==0&&x<x2)x--;
		ans++;
		return;
	}
	//没碰到目标就继续
	dfs(x,y,1);
	dfs(x,y,0);
	//如果往右走和往下走都走过了,就回溯

	return;

}
int main(){
cin>>x2>>y2>>x1>>y1;
y2=-y2;//在y轴负半轴 
y1=-y1;
dfs(0,0,0);//从(0,0)向右一格 
cout<<ans;
return 0;
}



2024/7/20 19:43
加载中...