52 lines
1.0 KiB
C++
52 lines
1.0 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
using namespace std;
|
|
int m = 5, n = 5;
|
|
vector<vector<char>> s;
|
|
vector<vector<bool>> visited;
|
|
vector<vector<int>> ids;
|
|
|
|
|
|
void dfs(int x, int y, int id){
|
|
if(x < 0 || x >= m || y < 0 || y >=n) return;
|
|
if(s[x][y] != '@') return;
|
|
if(visited[x][y] == true) return;
|
|
visited[x][y] = true;
|
|
ids[x][y] = id;
|
|
dfs(x - 1, y - 1, id + 1);
|
|
dfs(x - 1, y, id + 1);
|
|
dfs(x - 1, y + 1, id + 1);
|
|
dfs(x, y - 1, id + 1);
|
|
dfs(x, y + 1, id + 1);
|
|
dfs(x + 1, y - 1, id + 1);
|
|
dfs(x + 1, y, id + 1);
|
|
dfs(x + 1, y + 1, id + 1);
|
|
cout << id << " ";
|
|
}
|
|
|
|
int main(){
|
|
s.resize(m);
|
|
visited.assign(m, vector<bool>(n, false));
|
|
ids.assign(m, vector<int>(n, 0));
|
|
char tmp;
|
|
for(int i = 0; i < m; i++){
|
|
for(int j = 0; j < n; j++){
|
|
cin >> tmp;
|
|
s[i].push_back(tmp);
|
|
}
|
|
}
|
|
for(int i = 0; i < m; i++){
|
|
for(int j = 0; j < n; j++){
|
|
if(!visited[i][j] && s[i][j] == '@') dfs(i, j, 1);
|
|
}
|
|
}
|
|
cout << endl;
|
|
int max = -1;
|
|
for(int i = 0; i < m; i++){
|
|
for(int j = 0; j < n; j++){
|
|
if(ids[i][j] > max) max = ids[i][j];
|
|
}
|
|
}
|
|
cout << max << endl;
|
|
}
|