【CF1236D】Alice and the Doll(set)

题意:给定一个n*m的网格,其中k格有障碍

周驿东从(1,1)出发面朝右,每次行动前他可以选择顺时针旋转90度或不旋转,然后向自己朝向的位置走1格

问他能否不重复不遗漏的走过所有非障碍格

n,m,k<=1e5

思路:第一次看到E的一血比D早……

事实上就是在模拟朝右、下、左、上这样循环走,每次遇到障碍物就停下,这样一个过程

发现这个过程需要维护小于某个值的最大值和大于某个值的最小值,所以可以用set维护障碍物

参考了某红名大佬的写法,果然他们的暴力都是优雅的……

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef pair<int,int> PII;
  7 typedef pair<ll,ll> Pll;
  8 typedef vector<int> VI;
  9 typedef vector<PII> VII;
 10 //typedef pair<ll,ll>P;
 11 #define N  200010
 12 #define M  200010
 13 #define fi first
 14 #define se second
 15 #define MP make_pair
 16 #define pb push_back
 17 #define pi acos(-1)
 18 #define mem(a,b) memset(a,b,sizeof(a))
 19 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 20 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 21 #define lowbit(x) x&(-x)
 22 #define Rand (rand()*(1<<16)+rand())
 23 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 24 #define ls p<<1
 25 #define rs p<<1|1
 26 
 27 const ll MOD=1e9+7,inv2=(MOD+1)/2;
 28       double eps=1e-6;
 29       ll INF=1e15;
 30       int dx[4]={-1,1,0,0};
 31       int dy[4]={0,0,-1,1};
 32 
 33 set<int>c1[N],c2[N];
 34 
 35 int read()
 36 {
 37    int v=0,f=1;
 38    char c=getchar();
 39    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 40    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 41    return v*f;
 42 }
 43 
 44 int main()
 45 {
 46     int n=read(),m=read(),k=read();
 47     rep(i,1,k)
 48     {
 49         int x=read(),y=read();
 50         c1[x].insert(y);
 51         c2[y].insert(x);
 52     }
 53     rep(i,1,n)
 54     {
 55         c1[i].insert(0);
 56         c1[i].insert(m+1);
 57     }
 58     rep(i,1,m)
 59     {
 60         c2[i].insert(0);
 61         c2[i].insert(n+1);
 62     }
 63     int mx_x=n,mn_x=1;
 64     int mx_y=m,mn_y=1;
 65     ll s=1;
 66     int x=1,y=1,d=0;
 67     while(1)
 68     {
 69         int nx,ny;
 70         if(d==0)
 71         {
 72             nx=x;
 73             auto it=c1[x].lower_bound(y);
 74             ny=min(*it-1,mx_y);
 75             mn_x=nx+1;
 76         }
 77         if(d==1)
 78         {
 79             ny=y;
 80             auto it=c2[y].lower_bound(x);
 81             nx=min(*it-1,mx_x);
 82             mx_y=ny-1;
 83         }
 84         if(d==2)
 85         {
 86             nx=x;
 87             auto it=c1[x].lower_bound(y);
 88             --it;
 89             ny=max(*it+1,mn_y);
 90             mx_x=nx-1;
 91         }
 92         if(d==3)
 93         {
 94             ny=y;
 95             auto it=c2[y].lower_bound(x);
 96             --it;
 97             nx=max(*it+1,mn_x);
 98             mn_y=ny+1;
 99         }
100         s+=abs(nx-x)+abs(ny-y);
101         if(nx==x&&ny==y)
102         {
103             if(!(x==1&&y==1&&d==0)) break;
104         }
105         d=(d+1)%4;
106         x=nx,y=ny;
107     }
108     if(s==1ll*n*m-k) printf("Yes\n");
109      else printf("No\n");
110     return 0;
111 }

猜你喜欢

转载自www.cnblogs.com/myx12345/p/11700292.html