P3395 路障
题目背景
此题约为NOIP提高组Day1T1难度。
题目描述
B君站在一个n\times nn×n的棋盘上。最开始,B君站在(1,1)这个点,他要走到(n,n)这个点。
B君每秒可以向上下左右的某个方向移动一格,但是很不妙,C君打算阻止B君的计划。
每秒结束的时刻,C君会在(x,y)上摆一个路障。B君不能走在路障上。
B君拿到了C君准备在哪些点放置路障。所以现在你需要判断,B君能否成功走到(n,n)。
保证数据足够弱:也就是说,无需考虑“走到某处然后被一个路障砸死”的情况,因为答案不会出现此类情况。
输入格式
首先是一个正整数T,表示数据组数。
对于每一组数据:
第一行,一个正整数n。
接下来2n-2行,每行两个正整数x和y,意义是在那一秒结束后,(x,y)将被摆上路障。
输出格式
对于每一组数据,输出Yes或No,回答B君能否走到(n,n)。
输入输出样例
输入
2
2
1 1
2 2
5
3 3
3 2
3 1
1 2
1 3
1 4
1 5
2 2
输出
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。
**题目思路:这是一道很水的题,我一秒钟就想出来了 **
开个玩笑活跃一下气氛。
进入正题…
我们其实只需要考虑在这一秒时,你要走的位置是否是路障
,换言之你是否能在下个点设置路障之前到达那个点,就可以了(即dis[tx][ty]<lz[nx][ny])
我们设lz[i][j]表示设置路障(i,j)是在第几秒,如果没有,那就最大值喽
#include<cstring>
#include<cstdio>
#include<iostream>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int dx[5]={0,-1,1,0,0};
const int dy[5]={0,0,0,-1,1};
int lz[1010][1010],n,dis[1010][1010],linex[1000010],liney[1000010];
bool v[1010][1010];
void input()
{
memset(lz,0x4f,sizeof(lz));
memset(dis,0x4f,sizeof(dis));
memset(v,0,sizeof(v));
scanf("%d",&n);
for(int i=1;i<=2*n-2;i++)
{
int x,y;
scanf("%d%d",&x,&y);
lz[x][y]=i;
}
}
bool check(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=n) return true;
return false;
}
void bfs()
{
linex[1]=1,liney[1]=1;
dis[1][1]=0;
v[1][1]=1;
int h=0,t=1;
while(h<=t)
{
h++;
int tx=linex[h],ty=liney[h];
for(int i=1;i<=4;i++)
{
int nx=tx+dx[i],ny=ty+dy[i];
if(check(nx,ny)&&dis[tx][ty]<lz[nx][ny]&&!v[nx][ny])
{
dis[nx][ny]=dis[tx][ty]+1;
v[nx][ny]=1;
t++;
linex[t]=nx,liney[t]=ny;
}
}
}
}
int main()
{
//fre();
int t;
scanf("%d",&t);
while(t--)
{
input();
bfs();
if(dis[n][n]==1330597711) printf("No\n");
else printf("Yes\n");
}
return 0;
}