BFS- roadblock (popularization/improvement-) difficulty is about NOIP improvement group Day1T1

BFS-roadblock (popularization/improvement-) The difficulty of this question is about the difficulty of Day1T1 of the NOIP improvement group.

Impressions

How does it feel to do this question? I feel that my mind is full of stars now.
Let me talk about my own situation first. The level should only be popularized. Generally speaking, I can only make the first simple question of NOlP. After a lot of practice deep search and wide search in the past few days, the popular-difficulty questions have been basically completed, and I happened to see a familiar question. Although the difficulty is popularizing/increasing-, but think about it since it is so familiar, it should not be done It would be too difficult, so I started the problem-solving state from about 8 o'clock in the evening to the present, and finally looked at the solutions of the big guys, and finally solved it at the cost of more than 3 hours. ! ! It doesn't matter, this night, we can start the greedy algorithm tomorrow.
Okay, let's see what topic can be so difficult

Topic background

This question is about NOIP increasing the difficulty of Day1T1. Roadblock

Title description

Mr. B stands on an n×n chessboard. At the beginning, Mr. B stood at the point (1,1), and he had to go to the point (n,n).

Mr. B can move up, down, left, and right by one square per second, but it is not good. Mr. C intends to prevent Mr. B’s plan.

At the end of every second, Mr. C will put a roadblock on (x,y). Mr. B cannot walk on the roadblock.

Mr. B has obtained the points where Mr. C is going to place roadblocks. So now you need to judge whether Mr. B can successfully reach (n,n).

Ensure that the data is weak enough: In other words, there is no need to consider the situation of "walking somewhere and being killed by a roadblock", because the answer will not be such a situation.

Input format

The first is a positive integer T, which represents the number of data groups.

For each set of data:

In the first line, a positive integer n.

In the next 2n-2 lines, each line contains two positive integers x and y, meaning that after that second, (x,y) will be put on a roadblock.

Output format

For each set of data, output Yes or No to answer whether Mr. B can go to (n, n).

Sample input and output

enter

2

2
1 1
2 2

5
3 3
3 2
3 1
1 2
1 3
1 4
1 5
2 2

Output

Yes
Yes

Instructions/tips

Sample explanation:

Below 0 means that you can go, x means that you cannot go, and B means the current position of Mr. B. The time is shown from left to right.

Case 1:
0 0 0 0 0 B (have come)
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 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 …(Mr. B can go to the end)
Data scale:

To prevent fraudulent points, the data is guaranteed to be all hand-made.

For 20% of the data, n<=3.

For 60% of the data, n<=500.

For 100% data, there is n<=1000.

For 100% data, there is T<=10.

Prevent fraud! ! ! I really don't give a chance to take any points

Code:

#include<bits/stdc++.h>
using namespace std;
int t,n,x,y,head,tail;
int book[1001][1001];
int next[5][2]={
    
    {
    
    0,0},{
    
    0,1},{
    
    1,0},{
    
    0,-1},{
    
    -1,0}};
struct note{
    
    
	int x,y;
}que[1000002],qb[1000002];

void bfs()
{
    
    
	int t=1;
	head=1,tail=1;
	book[head][tail]=1;
	que[tail].x=1;
	que[tail].y=1;
	tail++;
//	if(head==n&&tail==n) flag=1;
	while(head<tail){
    
    
		int tx,ty;
		for(int i=1;i<=4;i++){
    
    
			tx=que[head].x+next[i][0];
			ty=que[head].y+next[i][1];
			
			if(tx<1||tx>n||ty<1||ty>n) continue;
	//		if(tx==n&&ty==n) flag=1;
			if(book[tx][ty]==0){
    
    
				book[tx][ty]=1; //标记走过的路,你总不能回头吧??
				que[tail].x=tx;
				que[tail].y=ty;
				tail++;
			}
		}
		book[qb[t].x][qb[t].y]=1; //标记路障
		t++;
		head++;
	}
	
	for(int i=tail-1;i>=1;i--){
    
    
		if(que[i].x==n&&que[i].y==n) {
    
    
			printf("Yes\n");
			return;
		}
	}
	printf("No\n");
	return;
//	if(flag) printf("YES\n");
//	else printf("NO\n");
	
}
int main()
{
    
    
	scanf("%d",&t);
	while(t--){
    
    
		scanf("%d",&n);
		for(int i=1;i<=2*n-2;i++){
    
    
			scanf("%d%d",&x,&y);
			qb[i].x=x;  //标记路障
			qb[i].y=y;
		}
		bfs();
		memset(book,0,sizeof(book));  //清空数组或者结构体的最快方法
		memset(qb,0,sizeof(qb));
		memset(que,0,sizeof(que));
	}
	return 0;
}

Insert picture description here
Well, done! ! ! !

Guess you like

Origin blog.csdn.net/qq_45735298/article/details/108159576
Recommended