C. Asia regional Palace

Single Point Test time:  1.0 seconds

Memory Limit:  512 MB

Ancient prehistoric period, Queen Alice and Bob Mars fell in love with love, some tear-jerking love story spread among the people.
According to legend, stunning beauty queen Alice six circles, as required color values rather reveal the ancient heart Qaq transaction, but the Queen had Bob betrothed to Mars, how it would be empathetic to ourselves, but he is not God of War singled Bob opponent. Thus, they reveal Qaq will design the Ares Bob introduction of prehistoric land, planted ten peach trapped Ares Bob. Queen Alice, after that, braved crossing the robbery Trick rush to the rescue ...... really bian not go on.
In short, a n × n maze, Alice standing (1,1) position, Bob trapped in ( n, n) location, in a maze there is an obstacle, a predetermined obstacle traveling together in a maze and can not be the same column . Alice can only move in a maze and can not cross an obstacle, you can only move around a grid and only four directions up and down.
Alice Bob can successfully save it? If successful rescue output  Yes, and outputs the minimum number of steps or the output used.  No.
As shown, A the representative Alice, B on behalf of Bob, # on behalf of an obstacle, four arrow represents the direction of movement:

Entry

A first line integer T, T represents the set of test data
for each set of data, the first row of two integers n and m, respectively represent the size of n × n and m maze obstacle, then m rows, each two integers x and y, represents an obstacle coordinates (x, y).
Ensure that data in different rows and different columns obstacle, not at the position (1,1), (n, n ).
(1≤T≤2000,1≤m, n≤10000,1≤x, y≤n)

Export

For each test case, the output per line.
If successful rescue output  Yes, and output the minimum number of steps. Otherwise output  No .

Sample

input

2
2 1
1 2
2 2
1 2
2 1

output

Yes 2
No
    乍看一眼或许好多人都写成bfs了,但是一直会超时,所以本题用bfs是行不通的。
注意本题的一个提示(数据保证障碍物不同行且不同列)。即不能达到终点的情况
就是矩阵的副对角线以及与副对角线平行的位置上都充满障碍物,这样才不能到达
。哪怕副对角线上有一个缺口都是可以到达滴。
    还有一个要点是这个矩阵是方阵。而能到达终点的情况都可以转化为一横一竖的路径
即总长为2*(n-1)

    而判断是否在矩阵的副对角线以及与副对角线平行的位置上,只需判断,x+y的值就行了
因为这些位置上的值都是相等的。而我们要做的就是统计这些点的个数就行了
#include<bits/stdc++.h>

using namespace std;

struct node
{
	int x,y;//保存障碍物的位置 
	int sum;
}a[10005];

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n,m,k,s1 = 0,x;
		scanf("%d %d",&n,&m);
		for (int i=0;i<m;i++)
		{
			scanf("%d %d",&a[i].x,&a[i].y);
			a[i].sum = a[i].x + a[i].y;
			if (a[i].x==1)
			{//标记第一行上障碍物的位置,因为封死的情况是从右上到左下的 
				s1 = a[i].sum;//只有第一行上出现障碍物才可能封死 
				x = a[i].y;//y坐标就是这条线上封死情况下出现障碍物的个数 
			}
		}
		k = 0; // 
		for (int i=0;i<m;i++)
		{
			if (a[i].sum==s1)
				k++; 
		}
		if (x==k)
			printf("No\n");
		else
		{
			int ans = 2 * (n - 1);
			printf("Yes %d\n",ans);
		}
	}
	return 0;
} 

 

 

 

Guess you like

Origin blog.csdn.net/qq_40912854/article/details/88958538