D. Alice and the Doll
题意: 给定一个n*m的矩阵, 里面有k个路障, 你目前在(1, 1), 沿着矩阵的上边界往右走, 当你遇到边界或者是路障时, 你会自己右转. 现在问你能不能走过所有非路障的格子而且每个格子只能走一次;
感觉有点像大模拟, 想了半天不敢写, 参考了别人的代码, 发现用set 维护路障然后二分真的优雅
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db double
#define eps 1e-8
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
const ll maxn = 1e5 + 10 ;
const ll mod = 1e9 + 7 ;
ll n, m, k;
set< ll> y[ maxn] , x[ maxn] ;
signed main ( )
{
cin >> n >> m >> k;
_rep ( i, 1 , k)
{
ll xi, yi;
cin >> xi >> yi;
y[ yi] . insert ( xi) ;
x[ xi] . insert ( yi) ;
}
_rep ( i, 1 , n)
{
x[ i] . insert ( 0 ) ;
x[ i] . insert ( m + 1 ) ;
}
_rep ( i, 1 , m)
{
y[ i] . insert ( 0 ) ;
y[ i] . insert ( n + 1 ) ;
}
ll mx_x = n, mn_x = 1 , mx_y = m, mn_y = 1 , cur_x = 1 , cur_y = 1 , d = 0 ;
ll cnt = 1 ;
while ( 1 )
{
ll nx = 0 , ny = 0 ;
if ( d == 0 )
{
nx = cur_x;
auto it = x[ cur_x] . lower_bound ( cur_y) ;
ny = min ( mx_y, * it - 1 ) ;
mn_x = nx + 1 ;
}
else if ( d == 1 )
{
ny = cur_y;
auto it = y[ cur_y] . lower_bound ( cur_x) ;
nx = min ( mx_x, * it - 1 ) ;
mx_y = ny - 1 ;
}
else if ( d == 2 )
{
nx = cur_x;
auto it = x[ cur_x] . lower_bound ( cur_y) ;
it-- ;
ny = max ( * it + 1 , mn_y) ;
mx_x = nx - 1 ;
}
else if ( d == 3 )
{
ny = cur_y;
auto it = y[ cur_y] . lower_bound ( cur_x) ;
it-- ;
nx = max ( * it + 1 , mn_x) ;
mn_y = ny + 1 ;
}
cnt + = abs ( nx - cur_x) + abs ( ny - cur_y) ;
if ( nx == cur_x && ny == cur_y)
{
if ( ! ( cur_x == 1 && cur_y == 1 && d == 0 ) ) break ;
}
d = ( d + 1 ) % 4 ;
cur_x = nx, cur_y = ny;
}
cnt == n * m - k ? cout << "Yes" << endl : cout << "No" << endl;
}