题目链接
思路:这题不需要什么算法,就是简单的模拟,每次直走走到可以走的最末端的时候再右转,直到不能走了为止,不过这个对代码能力要求还是很高的,一些细节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");
}