把第一个样例下载下来看了,结果是对的,判题机给了WA 。。。
code
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 500;
typedef struct MyNode{
int x;
int y;
}MyNode;
int n,m;
int data[N][N];
int x,y;
int ans;
queue<MyNode> que;
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
char ch;
bool check(int x,int y){
if(x < 1 || x > n || y < 1 || y > m) return false;
if(data[x][y] == 0) return false;
return true;
}
void bfs(int x,int y){
while(!que.empty()) que.pop();
MyNode node;
node.x = x;
node.y = y;
que.push(node);
while(!que.empty()){
MyNode node1 = que.front();
que.pop();
for(int i = 0;i<4;i++){
int nx = node1.x+dx[i];
int ny = node1.y+dy[i];
if(check(nx,ny) == true){
data[nx][ny] = 0;
MyNode node2;
node2.x = nx;
node2.y = ny;
que.push(node2);
}
}
}
}
char read(){
char ch = getchar();
if(ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
return ch;
}
signed main(){
cin>>n>>m;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
ch = read();
data[i][j] = ch - '0';
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
if(data[i][j] != 0){
++ans;
bfs(i,j);
}
}
}
cout<<ans<<endl;
return 0;
}
悬赏一个关注