1 solutions
-
0
#G1040. 水流
Fess:一眼暴力。 考虑 BFS。
先贪心对每个点的高度从小到大排序,依次放点,广搜更新每个点是否已被抽水, 计数即可
#include<bits/stdc++.h> using namespace std; const int N=100,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; char ch[N][N]; bool vis[N][N]; int n,m,T,p_size,ans; struct Node{ int x,y; char k; bool operator <(const Node &x)const{ return k<x.k ; } }p[N*N]; void bfs(Node a){ queue<Node>q; vis[a.x ][a.y ]=1; q.push(a); while(q.size()){ Node x=q.front();q.pop(); for(int i=0;i<4;i++){ Node nx=Node{x.x +dx[i],x.y +dy[i],ch[x.x +dx[i]][x.y +dy[i]]}; if(nx.x <=0||nx.x >n||nx.y <=0||nx.y >m||vis[nx.x ][nx.y ]||nx<x)continue; vis[nx.x ][nx.y ]=1; q.push(nx); } } return ; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); //freopen("water.in","r",stdin); //freopen("water.out","w",stdout); cin>>T; while(T--){ ans=p_size=0; cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>ch[i][j]; vis[i][j]=0; p[++p_size]=Node{i,j,ch[i][j]}; } } sort(p+1,p+1+p_size); for(int i=1;i<=p_size;i++){ if(vis[p[i].x ][p[i].y ])continue; ans++; bfs(p[i]); } cout<<ans<<'\n'; } }
- 1
Information
- ID
- 44
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 34
- Accepted
- 11
- Uploaded By