Codeforces Round #593 (Div. 2)D- Alice and the Doll

18 篇文章 0 订阅
1 篇文章 0 订阅

D. Alice and the Doll

题意: 给定一个n*m的矩阵, 里面有k个路障, 你目前在(1, 1), 沿着矩阵的上边界往右走, 当你遇到边界或者是路障时, 你会自己右转. 现在问你能不能走过所有非路障的格子而且每个格子只能走一次;

>> face <<

  • 前置技能
    set

感觉有点像大模拟, 想了半天不敢写, 参考了别人的代码, 发现用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(!(x==1&&y==1&&d==0)) break;//处理一开始往下走的情况
			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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值