It's All Squares【列舉計數】-2020杭電多校2
阿新 • • 發佈:2020-08-02
題意:
\(n*m\) 的棋盤,每個格子上有權值 \(w_{i,j}\),\(q\) 次詢問,每次給出起點,在邊線上走出一個簡單多邊形,問多邊形內部有多少個不同的權值。
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=6771
分析:
程式碼:
#include<bits/stdc++.h> #define pb push_back using namespace std; const int N=410; const int maxn=4e6+6; int w[N][N]; int cnt[N*N]; char ss[maxn]; vector<int>num; int main() { int t,n,m,q; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&q); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) scanf("%d",&w[i][j]); } int x,y,ans; while(q--) { scanf("%d%d",&x,&y); scanf("%s",ss+1); int len=strlen(ss+1),it=1; ans=0; num.clear(); while(it<=len) { if(ss[it]=='L')//x-1 { for(int i=1;i<=y;i++) { cnt[w[x][i]]--; if(cnt[w[x][i]]==-1) { ans++; num.pb(w[x][i]); } else if(cnt[w[x][i]]==0) ans--; } x--; } else if(ss[it]=='R')//x+1 { for(int i=1;i<=y;i++) { cnt[w[x+1][i]]++; if(cnt[w[x+1][i]]==1) { ans++; num.pb(w[x+1][i]); } else if(cnt[w[x+1][i]]==0) ans--; } x++; } else if(ss[it]=='U') y++; else y--; it++; } for(int i=0;i<num.size();i++) cnt[num[i]]=0; printf("%d\n",ans); } } return 0; }
參考部落格:
https://blog.csdn.net/qq_43202683/article/details/107548741