[Codeforces 1236D] Alice and the Doll

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_33831360/article/details/102625065

 玩偶只能右转或直行,每个点只可以进行一次动作,看能不能遍历所有点一次除了障碍点

显然只有沿着边走是最好的决策(走到障碍点或边界后才右转),如果错过了,没法回头去遍历,因为只能直走右转两个操作,还无法走走过的点(每个点只可以进行一次动作所以无法重复走同一个点)。

 n,m太大了,不能模拟一步一步走,但是可以一行一行走,一列一列走,比方说向右走,我要找到这一行最左边的障碍点,走到它旁边。注意在 玩偶现在位置的左边的点是不能挡住它的,找的是它右边最靠左的。所以用vector把这行的y值都存起来就行。

还有不能走走过的点(用四个变量记下之前走过的L,U,D,R的坐标),之后右转向下走就行......

当下个点就是障碍时(dx==tpx && dy==tpy) 结束。判断走过的点是不是n*m-k个。

注意模拟时起点可以直行,也可以右转。有类似下面的情况

0111

0111

1111

#include <cstdio>
#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long LL;
#define int LL
void read(int &x) {
    char c;bool flag = 0;
    while((c=getchar())<'0'||c>'9') flag |= (c=='-');
    x=c-'0';while((c=getchar())>='0'&&c<='9') x = (x<<3)+(x<<1)+c-'0';
    flag?x=-x:x;
}

#define N 100020
int n,m,k;
vector<int> u[N],d[N],l[N],r[N];
int uu[N],dd[N],ll[N],rr[N]; 
 
signed main() {
    read(n); read(m); read(k);
    for (int i = 1; i <= n; i++) {
    //	l[i] = 0; r[i] = m+1;
        l[i].push_back(0);
        r[i].push_back(m+1);
	}
	for (int i = 1; i <= m; i++) {
		//u[i] = 0; d[i] = n+1;
		u[i].push_back(0);
		d[i].push_back(n+1);
	}
	for (int i = 1; i <= k; i++) {
		int x,y; read(x); read(y);
		l[x].push_back(y);
		r[x].push_back(y);
		u[y].push_back(x);
		d[y].push_back(x);
	} 
	for (int i = 1; i <= n; i++) {
		sort(r[i].begin(),r[i].end());
	    sort(l[i].begin(),l[i].end(),greater<int>());
	}
	for (int i = 1; i<= m; i++) {
	    sort(d[i].begin(),d[i].end());
	    sort(u[i].begin(),u[i].end(),greater<int>());
	}
	int L = 0,R = m+1,U = 0,D = n+1;
	int dir = 1,dx = 1,dy = 1;
	LL sum = 1;
	bool turned = 0;
	while(1) {
		int tpx = dx,tpy = dy;
		if (dir == 1) {
		   while (r[dx][rr[dx]] < dy) rr[dx]++;	
		   dy = min(r[dx][rr[dx]]-1,R-1);
		   U = dx;
		}
		if (dir == 2) {
		   while(d[dy][dd[dy]] < dx) dd[dy]++;
		   dx = min(d[dy][dd[dy]]-1,D-1);
		   R = dy; 
		}
		if (dir == 3) {
		   while(l[dx][ll[dx]] > dy) ll[dx]++;
		   dy = max(l[dx][ll[dx]]+1,L+1);
		   D = dx;
		}
		if (dir == 4) {
		   while(u[dy][uu[dy]] > dx) uu[dy]++;
		   dx = max(u[dy][uu[dy]]+1,U+1);
		   L = dy;
		}
		dir++;
		if (dir == 5) dir = 1;
		if (dx==tpx && dy==tpy) {
		   if (dx==1 && dy==1 && !turned) {
		   	 turned = 1;
		   	 continue;
		   } 
		   break;
		}
		sum += abs(dx-tpx)+abs(dy-tpy); 
//cout<<dx<<" "<<dy<<" "<<sum<<"\n";
	} 
//cout<<sum<<" ";
	puts(sum+k==1LL*n*m?"Yes":"No");
    return 0;
}

Alice got a new doll these days. It can even walk!

Alice has built a maze for the doll and wants to test it. The maze is a grid with nn rows and mm columns. There are kk obstacles, the ii-th of them is on the cell (xi,yi)(xi,yi), which means the cell in the intersection of the xixi-th row and the yiyi-th column.

However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze.

More formally, there exist 44 directions, in which the doll can look:

  1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x,y)(x,y) into the cell (x,y+1)(x,y+1);
  2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x,y)(x,y) into the cell (x+1,y)(x+1,y);
  3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x,y)(x,y) into the cell (x,y−1)(x,y−1);
  4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x,y)(x,y) into the cell (x−1,y)(x−1,y).

.

Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1→21→2, 2→32→3, 3→43→4, 4→14→1. Standing in one cell, the doll can make at most one turn right.

Now Alice is controlling the doll's moves. She puts the doll in of the cell (1,1)(1,1) (the upper-left cell of the maze). Initially, the doll looks to the direction 11, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved?

Input

The first line contains three integers nn, mm and kk, separated by spaces (1≤n,m≤105,0≤k≤1051≤n,m≤105,0≤k≤105) — the size of the maze and the number of obstacles.

Next kk lines describes the obstacles, the ii-th line contains two integer numbers xixi and yiyi, separated by spaces (1≤xi≤n,1≤yi≤m1≤xi≤n,1≤yi≤m), which describes the position of the ii-th obstacle.

It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1,1)(1,1).

Output

Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement.

If it is impossible to walk across the maze by these rules print 'No' (without quotes).

Examples

input

Copy

3 3 2
2 2
2 1

output

Copy

Yes

input

Copy

3 3 2
3 1
2 2

output

Copy

No

input

Copy

3 3 8
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

output

Copy

Yes

Note

Here is the picture of maze described in the first example:

In the first example, the doll can walk in this way:

  • The doll is in the cell (1,1)(1,1), looks to the direction 11. Move straight;
  • The doll is in the cell (1,2)(1,2), looks to the direction 11. Move straight;
  • The doll is in the cell (1,3)(1,3), looks to the direction 11. Turn right;
  • The doll is in the cell (1,3)(1,3), looks to the direction 22. Move straight;
  • The doll is in the cell (2,3)(2,3), looks to the direction 22. Move straight;
  • The doll is in the cell (3,3)(3,3), looks to the direction 22. Turn right;
  • The doll is in the cell (3,3)(3,3), looks to the direction 33. Move straight;
  • The doll is in the cell (3,2)(3,2), looks to the direction 33. Move straight;
  • The doll is in the cell (3,1)(3,1), looks to the direction 33. The goal is achieved, all cells of the maze without obstacles passed exactly once.

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/102625065
今日推荐