洛谷—— P3395 路障

https://www.luogu.org/problem/show?pid=3395

题目背景

此题约为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

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <queue>
 4 
 5 #define min(a,b) (a<b?a:b)
 6 
 7 inline void read(int &x)
 8 {
 9     x=0; register char ch=getchar();
10     for(; ch>'9'||ch<'0'; ) ch=getchar();
11     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
12 }
13 const int N(1005);
14 int n,tim[N][N];
15 struct Node {
16     int x,y,t;
17 }u,v;
18 std::queue<Node>que;
19 int fx[4]={0,1,0,-1};
20 int fy[4]={1,0,-1,0};
21 bool vis[N][N];
22 
23 int Presist()
24 {
25     int t; read(t);
26     for(bool flag; t--; )
27     {
28         read(n); flag=0;
29         for(int i=1; i<=n; ++i)
30           for(int j=1; j<=n; ++j)
31             tim[i][j]=0x3f3f3f3f;
32         for(int x,y,i=1; i<=n-1<<1; ++i)
33             read(x),read(y),tim[x][y]=min(i,tim[x][y]);
34         for(; !que.empty(); ) que.pop();
35         memset(vis,0,sizeof(vis));
36         u.x=u.y=1; u.t=0;
37         vis[1][1]=1; que.push(u);
38         for(; !que.empty(); )
39         {
40             u=que.front(); que.pop();
41             if(u.x==n&&u.y==n) {flag=1;break;}
42             for(int i=0; i<4; ++i)
43             {
44                 v.x=u.x+fx[i],v.y=u.y+fy[i],v.t=u.t+1;
45                 if(v.x<1||v.y<1||v.x>n||v.y>n) continue;
46                 if(v.t>tim[v.x][v.y]||vis[v.x][v.y]) continue;
47                 vis[v.x][v.y]=1; que.push(v);
48             }
49         }
50         if(flag) puts("Yes"); else puts("No");
51     }
52     return 0;
53 }
54 
55 int Aptal=Presist();
56 int main(int argc,char**argv){;}

 

posted @ 2017-10-25 15:08  Aptal丶  阅读(273)  评论(0编辑  收藏  举报