本文主要是介绍1249:Lake Counting,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【题目描述】
题意:有一块N×M的土地,雨后积起了水,有水标记为‘W’,干燥为‘.’。八连通的积水被认为是连接在一起的。请求出院子里共有多少水洼?
【输入】
第一行为N,M(1≤N,M≤110)。
下面为N*M的土地示意图。
【输出】
一行,共有的水洼数。
【输入样例】
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
【输出样例】
3
【AC代码】
#include <bits/stdc++.h>
using namespace std;
int n,m;
char a[1001][1001];
int dx[9]= {-1,1,0,0,-1,1,-1,1};
int dy[9]= {0,0,-1,1,-1,1,1,-1};
struct node
{int x,y;
};
void bfs(int x1,int y1)
{queue<node> q;q.push({x1,y1});a[x1][y1]='0';while(!q.empty()){node xy;xy=q.front();q.pop();for(int i=0; i<8; i++){int xx=xy.x+dx[i],yy=xy.y+dy[i];if(xx>=1 &&xx<=n &&yy>=1 &&yy<=m &&a[xx][yy]=='W'){q.push({xx,yy});a[xx][yy]='.';}}}
}
int main()
{int cnt=0;cin>>n>>m;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){cin>>a[i][j];}}for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(a[i][j]=='W'){bfs(i,j);cnt++;}}}cout<<cnt<<endl;return 0;
}
这篇关于1249:Lake Counting的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!