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

题目链接
在这里插入图片描述
在这里插入图片描述
思路:这题不需要什么算法,就是简单的模拟,每次直走走到可以走的最末端的时候再右转,直到不能走了为止,不过这个对代码能力要求还是很高的,一些细节lower_bound的边界问题等等,debug了进两个小时。。。不过这里还有一点不明白的就是为什么right在每次向右走的时候为下一次更新就会wa。。。就是注释掉的那四行代码(留坑待debug)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+1;
ll n,m,k,ans=1;
vector<int>r[maxn],c[maxn];
int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	for(int i=1;i<=k;++i)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		r[x].push_back(y);
		c[y].push_back(x);
	}
	for(int i=1;i<=n;++i) sort(r[i].begin(),r[i].end());
	for(int i=1;i<=m;++i) sort(c[i].begin(),c[i].end());
	int now=0,up=0,down=n+1,right=m+1,left=0,size,x=1,y=1,first=0;
	while(1)
	{
		int tx,ty;
		if(now==0)//右 
		{
			size=lower_bound(r[x].begin(),r[x].end(),y)-r[x].begin();
			up=tx=x;
			ty=(size==r[x].size())?right-1:min(right-1,r[x][size]-1);
			//right=ty; 
		}
		if(now==1)//下 
		{
			size=lower_bound(c[y].begin(),c[y].end(),x)-c[y].begin();
			ty=right=y;
			tx=(size==c[y].size())?down-1:min(down-1,c[y][size]-1);
			//down=tx;
		 } 
		 if(now==2)//左
		 {
		 	size=lower_bound(r[x].begin(),r[x].end(),y)-r[x].begin()-1;
		 	down=tx=x;
		 	ty=(size<0)?left+1:max(left+1,r[x][size]+1);
		 	//left=ty;
		  } 
		  if(now==3)//上 
		  {
		  	size=lower_bound(c[y].begin(),c[y].end(),x)-c[y].begin()-1;
		  	left=ty=y;;
		  	tx=(size<0)?up+1:max(up+1,c[y][size]+1);
		  	//up=tx;
		  }
		  if(tx==x&&ty==y&&first) break;
		  now=(now+1)%4;
		  ans+=abs(tx-x)+abs(ty-y);
		  x=tx;y=ty;
		  first=1;
	}
	printf("%s\n",(ans==n*m-k)?"YES":"NO");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值