骑士游历【codevs】【黄金题】【棋盘型动态规划】

骑士游历

历经千难万险,最终的ac程序让我给凑出来了。

关于我的不细心的点在于1、忘记了日字行走是怎么走了,一开始只想到了两种走法。2、没有去计算数据范围。

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
int n,m; 
int x1,x2,y1,y2;
ll mychess[51][51];
bool have[51][51];
queue<int>q,q1,q2;
int main(){
	cin>>n>>m;
	cin>>x1>>y1>>x2>>y2;
	memset(mychess,0,sizeof(mychess));
	memset(have,0,sizeof(have));
	mychess[x2][y2]=1;
	q.push(x2);q.push(y2);
	
	while(!q.empty()){
		while(!q.empty()){
			int x=q.front();q.pop();
			int y=q.front();q.pop();
			
			int xt,yt,xb,yb;//左上、左下
			xt=x-1;
			yt=y+2;
			xb=x-1;
			yb=y-2;
			
			if(xt>=x1&&xt>=1&&yt<=m){
				mychess[xt][yt]+=mychess[x][y];
				if(!have[xt][yt]){q1.push(xt);q1.push(yt);}
				have[xt][yt]=true;
			}
			if(xb>=x1&&xb>=1&&yb>=1){
				mychess[xb][yb]+=mychess[x][y];
				if(!have[xb][yb]){q1.push(xb);q1.push(yb);}
				have[xb][yb]=true;
			}
			
			//日字是四种
			int xt2,yt2,xb2,yb2;//左上、左下
			xt2=x-2;
			yt2=y+1;
			xb2=x-2;
			yb2=y-1;
			
			if(xt2>=x1&&xt2>=1&&yt2<=m){
				mychess[xt2][yt2]+=mychess[x][y];
				if(!have[xt2][yt2]){q2.push(xt2);q2.push(yt2);}
				have[xt2][yt2]=true;
			}
			if(xb2>=x1&&xb2>=1&&yb2>=1){
				mychess[xb2][yb2]+=mychess[x][y];
				if(!have[xb2][yb2]){q2.push(xb2);q2.push(yb2);}
				have[xb2][yb2]=true;
			} 
		}		
		q=q1;q1=q2;	while(!q2.empty())q2.pop();
	}
	cout<<mychess[x1][y1]<<endl;
	return 0;
}

 

 

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/qq_36160277/article/details/83042176
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞