1033A. King Escape--Codeforce 题解

原题链接

这题就是国王一直不动(可以往八个方向走无限远),王后可以一直动,现在要使王后移动到指定位置且不会被国王的可以移动的位置相遇。这题和正常的走迷宫问题一样,只是路障需要自己建立(以国王为初始点往八个方向伸长),并且王后是可以往八个方向走。简单的BFS模板搞定

#include<cstring>
#include<math.h>
#include<iostream>
#include <climits>
#include <queue>

using namespace std;
typedef long long LL;

const int N = 1010;
bool g[N][N];
int n;
bool bfs(int x1,int y1,int x2,int y2)
{
    queue<pair<int,int>> q;
    q.push({x1,y1});
    int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    g[x1][y1]=true;
    while(q.size())
    {
        /*
        for (int i = n; i >= 1; i -- )
        {
            for (int j = 1; j <= n; j ++ )
            {
                cout<<g[j][i];
            }
            puts("");
        }
        puts("");
        */
        int x=q.front().first,y=q.front().second;
        //cout<<x<<"  "<<y<<endl;
        q.pop();
        for (int i = 0; i < 8; i ++ )
        {
            int tx=x+dx[i],ty=y+dy[i];
            //cout<<tx<<" !!  "<<ty<<endl;
            if(tx==x2&&ty==y2)return true;
            if(tx<1||tx>n||ty<1||ty>n)continue;
            if(g[tx][ty])continue;
            g[tx][ty]=true;
            q.push({tx,ty});
        }
    }
    return false;

}
int main()
{
	int x1,y1,x2,y2,x3,y3;
	scanf("%d%d%d%d%d%d%d",&n,&x1,&y1,&x2,&y2,&x3,&y3);
	g[x1][y1]=true;
	int l=x1,r=x1,u=y1,d=y1;
	while(u<=n||d>=1||l>=1||r<=n)
    {
        if(u<=n)g[x1][u]=true;
        if(d>=1)g[x1][d]=true;
        if(l>=1)g[l][y1]=true;
        if(r<=n)g[r][y1]=true;
        
        if(u<=n&&l>=1)g[l][u]=true;
        if(u<=n&&r<=n)g[r][u]=true;
        if(d>=1&&l>=1)g[l][d]=true;
        if(d>=1&&r<=n)g[r][d]=true;
        u++;
        d--;
        r++;
        l--;
    }
    if(bfs(x2,y2,x3,y3))puts("YES");
    else puts("NO");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值