Problem A. Dynamic Grid Google APAC 2016 University Test Round D
2017-02-24 20:33
447 查看
判断连通块个数。每次用BFS+vis数组标记访问即可。因为从点(i,j)开始bfs可以标记其所在连通块内所有的点。
直接暴力水过了,o(╯□╰)o。
直接暴力水过了,o(╯□╰)o。
#include<iostream> #include<stdio.h> #include<cstdio> #include<string> #include<cmath> #include<stdlib.h> #include<algorithm> #include<string.h> #include<cstring> #include<vector> #include<queue> #include<map> #include<set> using namespace std; //2016 Round D Problem A. Dynamic Grid int T; const int maxn=110; int N; int R; int C; int mp[maxn][maxn]; bool vis[maxn][maxn]; int x; int y; int z; char op; int ans; int dx[]={0,-1,0,1}; int dy[]={-1,0,1,0}; void bfs(int stx,int sty) { queue<pair<int,int> >que; while(!que.empty()) que.pop(); que.push(make_pair(stx,sty)); while(!que.empty()) { pair<int,int>now; now=que.front(); que.pop(); vis[now.first][now.second]=true; for(int d=0;d<4;d++) { int nextx=now.first+dx[d]; int nexty=now.second+dy[d]; if(nextx<0||nextx>=R||nexty<0||nexty>=C) { continue; } if(mp[nextx][nexty]==0) continue; if(vis[nextx][nexty]==true) continue; que.push(make_pair(nextx,nexty)); } } ans++; } int main() { freopen("A-large-practice.in","r",stdin); // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d",&T); for(int ca=1;ca<=T;ca++) { printf("Case #%d:\n",ca); memset(mp,0,sizeof(mp)); memset(vis,false,sizeof(vis)); scanf("%d %d",&R,&C); for(int i=0;i<R;i++) { char tmpc[maxn]; scanf("%s",&tmpc); for(int j=0;j<C;j++) { mp[i][j]=tmpc[j]-'0'; // if(tmpc=='0') // { // mp[i][j]=0; // } // else if(tmpc=='1') // { // mp[i][j]=1; // } //scanf("%d",&mp[i][j]); //cout<<mp[i][j]<<" "; } //cout<<endl; } scanf("%d",&N); //cout<<N<<endl; //return 0; for(int i=0;i<N;i++) { cin>>op;//scanf("%c",&op); //cout<<"st"<<op<<"ed"<<endl; if(op=='Q') { ans=0; memset(vis,false,sizeof(vis)); for(int i=0;i<R;i++) { for(int j=0;j<C;j++) { if(mp[i][j]==0||vis[i][j]==true) { continue; } bfs(i,j); } } printf("%d\n",ans); } else if(op=='M') { scanf("%d %d %d",&x,&y,&z); //cout<<x<<" "<<y<<" "<<z<<endl; mp[x][y]=z; } } //printf("Case #%d: %d\n",ca,dp[N-1][Q]); } return 0; }
相关文章推荐
- Problem A. Googol String Google APAC 2016 University Test Round A
- Problem B. gWheels Google APAC 2016 University Test Round B
- Problem C. gCampus Google APAC 2016 University Test Round A
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem D. gMatrix Google APAC 2016 University Test Round C
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem B. gCube Google APAC 2016 University Test Round A
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Problem B. Safe Squares Google APAC 2017 University Test Round C
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Problem C. Not So Random Google APAC 2017 University Test Practice Round
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- Problem B. Robot Rock Band Google APAC 2017 University Test Practice Round