【题解】 校内测9 5.18 搜索

傻叉题

优先级:dfs不能bfs
剪枝:确定行不通的点不用再走第2遍 unable[][];
(m-xx)*2 < (n-yy) 肯定不行,因为向上移动2起码得向右移动1
路径:要是用一个傻叉path[10000][10000]就爆空间了,用一个zhan[10000]即可

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

//how to get it in BFS??


int backy[4]={2,1,-1,-2}, x[4]={1,2,2,1};
int y[4]={-2,-1,1,2}, backx[4]={-1,-2,-2,-1};
int m,n;

int path[1010][1010];//傻叉数据 小于1000 , 正解应该是 用一个栈 存数据 而不是搞一个10000*10000的傻逼数组
int zhan[10010], tot;
 

//bool vis[10010][10010];
int ans[10010],top;
bool unable[10010][10010];
int t=0;
bool dfs(int xx, int yy){
	if(t)	return 1;
	if(((m-xx)<<1) < n-yy)	return 0;//剪枝 
	int res=0;
	for(int i=0;i<4;i++){
		int tox=xx+x[i], toy=yy+y[i];
		if(tox <= m && toy >= 1 && toy <= n &&! unable[tox][toy]){		
			if(t)	return 1;//避免找到解后没用的的东西入栈 
//			path[tox][toy]=i+1;//有栈就用不到这个傻逼path了 
			zhan[++tot]=i+1;//
			if(tox==m&&toy==n){
				t=1;
				return 1;
			}	
			if(t)	return 1;

			bool pan=dfs(tox,toy);
			if(!pan){
				unable[tox][toy]=1;//剪枝 
				tot--;//
			}
			else	res=1;	
		}
	}
	if(res==0)
		return 0;
	return 1;
}

int main()
{
//	freopen("knight.in","r",stdin);
//	freopen("knight.out","w",stdout);
	
	cin >> m >> n;
	dfs(1,1);

	if(!t){
		printf("-1
");//-1搞我三个点 
		return 0;
	}

/*	int xx=m,yy=n;
	while(xx!=1||yy!=1){
		int l=path[xx][yy];
		ans[++top]=l;
		xx+=backx[l-1];yy+=backy[l-1];
	}

	
	if(top>=1)	cout<<ans[top--];
	while(top){
		cout<<" "<<ans[top--];
	}cout<<endl;
	*/
	for(int i=1;i<=tot;i++)	cout<<zhan[i]<<" ";
	cout<<endl;
	return 0;
}

原文地址:https://www.cnblogs.com/ZhengkunJia/p/12912955.html