what?

P2771 [USACO16JAN] Build Gates S

@[kkk123123123](/user/507534) 给下代码呗,可能有UB
by bamboo1030 @ 2022-08-18 08:42:47


```cpp #include<bits/stdc++.h> using namespace std; const int maxn=2010; int n,a[maxn][maxn],sum,ans,cnt; int MAXX,MAXY; bool vis[maxn][maxn]; struct E{ int lx,ly,rx,ry; }; vector<E>point; int main(){ for(int i=1;i<=2001;i++){ for(int j=1;j<=2001;j++){ a[i][j]=2; } } scanf("%d",&n);getchar(); int X=1001,Y=1001; a[X][Y]=1; for(int i=1;i<=n;i++){ char c;c=getchar(); if(c=='N')a[X][++Y]=1,a[X][++Y]; else if(c=='E')a[++X][Y]=1,a[++X][Y]=1; else if(c=='S')a[X][--Y]=1,a[X][--Y]=1; else if(c=='W')a[--X][Y]=1,a[--X][Y]=1; a[X][Y]=1; } for(int i=1;i<=2001;i++){ for(int j=1;j<=2001;j++){ if(a[i][j]==2&&!vis[i][j]){ MAXX=0;MAXY=0; sum++; queue<pair<int,int> >q;q.push({i,j}); while(!q.empty()){ int x=q.front().first,y=q.front().second;q.pop(); if(vis[x][y])continue; vis[x][y]=1; if(a[x+1][y]==2&&!vis[x+1][y])q.push({x+1,y}); if(a[x-1][y]==2&&!vis[x-1][y])q.push({x-1,y}); if(a[x][y+1]==2&&!vis[x][y+1])q.push({x,y+1}); if(a[x][y-1]==2&&!vis[x][y-1])q.push({x,y-1}); } } } } if(sum==0)cout<<0; else cout<<sum-1<<endl; return 0; } ```
by YBaggio @ 2022-08-18 13:54:28


|