From dc1f0c4c0096013799416664894c5194dc7e1f52 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 28 Feb 2025 23:13:39 +0800 Subject: chore(store): move everything to store. --- .../works/life/algorithm-contest-2/solution/3.cpp | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 store/works/life/algorithm-contest-2/solution/3.cpp (limited to 'store/works/life/algorithm-contest-2/solution/3.cpp') diff --git a/store/works/life/algorithm-contest-2/solution/3.cpp b/store/works/life/algorithm-contest-2/solution/3.cpp new file mode 100644 index 0000000..a97e351 --- /dev/null +++ b/store/works/life/algorithm-contest-2/solution/3.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define x first +#define y second +#define pub push_back +#define mp make_pair +#define ll long long +using namespace std; +typedef pair 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 graph; +vector> vis; + +int solve() { + queue 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(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 -- cgit v1.2.3