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
行,每行两个正整数x
和y
,意义是在那一秒结束后,(x,y)
将被摆上路障。
输出格式:
对于每一组数据,输出Yes
或No
,回答B君能否走到(n,n)
。
输入输出样例
说明
样例解释:
以下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 天
你可以做的事情还有很多,即使到最后一秒也不要放弃,因为不到结束的那一刻谁也不知道结果会怎样