From 58e4c77b1d5f8241af91c55e1a8efaba3115dc6b Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 25 Oct 2020 17:16:31 +0800 Subject: import(life): Add algorithm contest 2. --- works/life/algorithm-contest-2/solution/3.cpp | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 works/life/algorithm-contest-2/solution/3.cpp (limited to 'works/life/algorithm-contest-2/solution/3.cpp') 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 +#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