이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<char>>grid;
vector<vector<bool>>visitado;
void bfs(int n, int m, char c) {
queue<pair<int,int>>colaBFS;
pair<int,int>aux;
vector<vector<bool>>vact(n, vector<bool>(m, false));
aux.first=0; aux.second=0;
colaBFS.push(aux);
visitado[0][0]=true;
vact[0][0]=true;
while(!colaBFS.empty()) {
int i=colaBFS.front().first;
int j=colaBFS.front().second;
colaBFS.pop();
if (i+1<n&&((!visitado[i+1][j]&&grid[i+1][j]==c)||(visitado[i+1][j]&&vact[i+1][j]==false))){
aux.first=i+1;
aux.second=j;
colaBFS.push(aux);
visitado[i+1][j]=true;
vact[i+1][j]=true;
}if (i>0&&((!visitado[i-1][j]&&grid[i-1][j]==c)||(visitado[i-1][j]&&vact[i-1][j]==false))){
aux.first=i-1;
aux.second=j;
colaBFS.push(aux);
visitado[i-1][j]=true;
vact[i-1][j]=true;
}if (j+1<m&&((!visitado[i][j+1]&&grid[i][j+1]==c)||(visitado[i][j+1]&&vact[i][j+1]==false))){
aux.first=i;
aux.second=j+1;
colaBFS.push(aux);
vact[i][j+1]=true;
visitado[i][j+1]=true;
}if (j>0&&((!visitado[i][j-1]&&grid[i][j-1]==c)||(visitado[i][j-1]&&vact[i][j-1]==false))){
aux.first=i;
aux.second=j-1;
colaBFS.push(aux);
visitado[i][j-1]=true;
vact[i][j-1]=true;
}
}
}
int main(){
int n, m;
cin>>n>>m;
grid.resize(n, vector<char>(m));
visitado.resize(n, vector<bool>(m, false));
for (int i=0; i<n; i++){
for (int j=0; j<m; j++) cin>>grid[i][j];
}int animals=0;
for (int k=0; k<n; k++){
for (int l=0; l<m; l++){
if (grid[k][l]!='.' and visitado[k][l]==false){
animals++;
bfs(n, m, grid[k][l]);
}
}
}cout<<animals<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |