diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-02-12 15:55:21 +0800 | 
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-02-12 15:55:21 +0800 | 
| commit | 10eb95869601e145b1d8bc909424777c25752d51 (patch) | |
| tree | 49449a4076ded9bd937a51679318edbe2a532cae /works/life/algorithm-contest-2/solution/3.cpp | |
| parent | 29ba3e88b1a7425fe00af0005b8a8228103aa21c (diff) | |
| parent | f8c10dd1fc55e60f35286475356e48c4f642eb63 (diff) | |
| download | crupest-10eb95869601e145b1d8bc909424777c25752d51.tar.gz crupest-10eb95869601e145b1d8bc909424777c25752d51.tar.bz2 crupest-10eb95869601e145b1d8bc909424777c25752d51.zip | |
import(life): IMPORT crupest/life COMPLETE.
Diffstat (limited to 'works/life/algorithm-contest-2/solution/3.cpp')
| -rw-r--r-- | works/life/algorithm-contest-2/solution/3.cpp | 77 | 
1 files changed, 77 insertions, 0 deletions
| diff --git a/works/life/algorithm-contest-2/solution/3.cpp b/works/life/algorithm-contest-2/solution/3.cpp new file mode 100644 index 0000000..a97e351 --- /dev/null +++ b/works/life/algorithm-contest-2/solution/3.cpp @@ -0,0 +1,77 @@ +#include <iostream>
 +#include <cstdio>
 +#include <cstring>
 +#include <algorithm>
 +#include <vector>
 +#include <queue>
 +#include <stack>
 +#include <set>
 +#include <map>
 +#include <cmath>
 +#include <unordered_map>
 +#include <unordered_set>
 +#include <string>
 +#include <sstream>
 +#include <climits>
 +#define x first
 +#define y second
 +#define pub push_back
 +#define mp make_pair
 +#define ll long long
 +using namespace std;
 +typedef pair<int, int> PII;
 +
 +struct node{
 +    int x, y;
 +    node(){}
 +    node(int _x = 0,int _y = 0): x(_x), y(_y){}
 +};
 +
 +int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 +int n;
 +vector<string> graph;
 +vector<vector<int>> vis;
 +
 +int solve() {
 +    queue<node> Q;
 +    for(int i = 0;i < n;++i){
 +        for(int j = 0;j < n;++j){
 +            if(graph[i][j] == '#') Q.push(node(i,j));
 +            vis[i][j] = 0;
 +        }
 +    }
 +    
 +    if(Q.size() == n * n || Q.empty()) return -1;
 +
 +    int dis = 0;
 +    while(!Q.empty()){
 +        int size = Q.size();
 +        while(size-- > 0){
 +            node cur = Q.front();
 +            Q.pop();
 +            for(int i = 0;i < 4;i++){
 +                int nextX = cur.x + dir[i][0];
 +                int nextY = cur.y + dir[i][1];
 +                if(nextX > -1 && nextX < n && nextY > -1 && nextY < n && graph[nextX][nextY] == '.' && !vis[nextX][nextY]){
 +                    vis[nextX][nextY] = 1;
 +                    Q.push(node(nextX,nextY));
 +                }
 +            }
 +        }
 +        if(!Q.empty()) dis += 1;
 +    }
 +    return dis;
 +}
 +
 +int main(void) {
 +    cin >> n;
 +    string line;
 +    vis.resize(n + 1, vector<int>(n + 1));
 +    for (int i = 0; i < n; i++) {
 +        cin >> line;
 +        graph.push_back(line);
 +    }
 +
 +    cout << solve();
 +    return 0;
 +}
\ No newline at end of file | 
