Wiki下象棋

传送门
我一开始错误的思路 把中国象棋 途径路上有棋子的点也标记为了1,一直不对,后来想了想,途径路上有棋子的点不能标记为1 ,应该标记为其他的 ,如果标记为1,则表示这个点在队列里面了,表示已经遍历过这个点了,是不可以的 因为就算这个点走过了 又有什么关系呢 我们最终的目的地也不是这个点

#include<bits/stdc++.h>
using namespace std;
struct pp
{
	int x,y;
} s[310];
struct node
{
	int x,y,step;
};
//int to[8][2]= {{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int to[8][2] = {-2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2, -2, -1};
int n,m,a,b,c,d,vis[310][310];
queue<node>q;
int check(int x,int y)
{
	if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y])
	return 1;
	else
	return 0;
}
int bfs(int flag)
{
	node h,t;
	h.x=a;
	h.y=b;
	h.step=0;
	vis[a][b]=1;
	q.push(h);
	while(!q.empty())
	{
		h=q.front();
		q.pop();
		if(h.x==c&&h.y==d)
		return h.step;
		for(int i=0;i<8;i++)
		{
			t.x=h.x+to[i][0];
			t.y=h.y+to[i][1];
			if(flag)
			{
				if(vis[to[i][0]/2+h.x][to[i][1]/2+h.y]==2)
				continue;
				if(check(t.x,t.y))
				{
					t.step=h.step+1;
					q.push(t);
					vis[t.x][t.y]=1;
				}
				else
				continue;
			}
			if(!flag)
			{
				if(check(t.x,t.y))
				{
					t.step=h.step+1;
					q.push(t);
					vis[t.x][t.y]=1;
				}
				else
				continue;
			}
		}
	}
	return -1;
}
int main()
{
	int i,k,tt;
	cin>>tt;
	while(tt--)
	{
		while(!q.empty())
		q.pop();
		memset(vis,0,sizeof(vis));
		cin>>n>>m>>k>>a>>b>>c>>d;
		for(i=1; i<=k; i++)
		{
			cin>>s[i].x>>s[i].y;
			vis[s[i].x][s[i].y]=1;
		}
		int ans1=bfs(0);
		memset(vis,0,sizeof(vis));
		while(!q.empty())
		q.pop();
		for(i=1;i<=k;i++)
		{
			vis[s[i].x][s[i].y]=2;//标记已经有棋子的点
		}
		int ans2=bfs(1);
		printf("%d %d\n",ans1,ans2);
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值