洛谷——P3395 路障

P3395 路障

题目背景

此题约为NOIP提高组Day1T1难度。

题目描述

B君站在一个n*n的棋盘上。最开始,B君站在(1,1)这个点,他要走到(n,n)这个点。

B君每秒可以向上下左右的某个方向移动一格,但是很不妙,C君打算阻止B君的计划。

每秒结束的时刻,C君会在(x,y)上摆一个路障。B君不能走在路障上。

B君拿到了C君准备在哪些点放置路障。所以现在你需要判断,B君能否成功走到(n,n)

保证不会走到某处,然后被一个路障砸死。

输入输出格式

输入格式:

 

首先是一个正整数T,表示数据组数。

对于每一组数据:

第一行,一个正整数n

接下来2n-2行,每行两个正整数xy,意义是在那一秒结束后,(x,y)将被摆上路障。

 

输出格式:

 

对于每一组数据,输出YesNo,回答B君能否走到(n,n)

 

输入输出样例

输入样例#1:  复制
2

2
1 1
2 2

5
3 3
3 2
3 1
1 2
1 3
1 4
1 5
2 2
输出样例#1:  复制
Yes
Yes

说明

样例解释:

以下0表示能走,x表示不能走,B表示B君现在的位置。从左往右表示时间。

Case 1:
0 0    0 0 0 B (已经走到了) B 0 x B x 0 
Case 2:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x 0 0 0 0 x 0 0 0 0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x 0 0 0 0 x 0 0 B 0 0 0 0 0 B 0 0 0 0 0 B 0 0 0 0 x B 0 ......(B君可以走到终点)

数据规模:

防止骗分,数据保证全部手造。

对于20%的数据,有n<=3

对于60%的数据,有n<=500

对于100%的数据,有n<=1000

对于100%的数据,有T<=10

 

bfs

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1010
#define maxn 999999
using namespace std;
bool flag;
bool vis[N][N];
int T,n,x,y,nx,ny,t[N][N],dis[N][N];
int xx[4]={0,0,1,-1},yy[4]={-1,1,0,0};
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct Node
{
    int x,y;
}node;
queue<Node>q;
int main()
{
    T=read();
    while(T--)
    {
        while(!q.empty()) q.pop();
        flag=false;n=read();
        for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
          t[i][j]=dis[i][j]=maxn,vis[i][j]=0;
        for(int i=1;i<=n*2-2;i++) 
         x=read(),y=read(),t[x][y]=i;
        node.x=1,node.y=1;q.push(node);
        dis[1][1]=0;vis[1][1]=0;
        while(!q.empty())
        {
            Node p=q.front();q.pop();
            if(p.x==n&&p.y==n) {flag=true;break;}
            for(int i=0;i<4;i++)
            {
                nx=p.x+xx[i],ny=p.y+yy[i];
                if(nx<1||ny<1||nx>n||ny>n) continue;
                if(t[nx][ny]<dis[p.x][p.y]||vis[nx][ny]) continue;
                dis[nx][ny]=min(dis[nx][ny],dis[p.x][p.y]+1);
                node.x=nx,node.y=ny;
                vis[nx][ny]=true;q.push(node);
            }
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

 

              

 

 

                      距 NOIp2017 还剩 16 天

 

                         你可以做的事情还有很多,即使到最后一秒也不要放弃,因为不到结束的那一刻谁也不知道结果会怎样

转载于:https://www.cnblogs.com/z360/p/7727873.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值