codeforces723D Lakes in Berland 搜索
来源:互联网 发布:nginx检查配置文件 编辑:程序博客网 时间:2024/06/06 14:04
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <map>#include <queue>#include <cmath>#include <vector>#include <string>using namespace std;struct node {int x, y;int area;node(int x, int y, int area) : x(x), y(y), area(area) {}node(int x, int y) : x(x), y(y) {}node() {}bool operator < (const node& on) const {return this->area < on.area;}};int n, m, k;char mapp[55][55];int vis[55][55];int dir[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };vector<node> v;bool check(int x, int y) {if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y]) {return false;}return true;}bool checkf(int x, int y) {if (x < 0 || x >= n || y < 0 || y >= m || mapp[x][y] == '*' || vis[x][y] == 2) {return false;}return true;}//排除靠海水域void bfso(int x, int y) {queue<node> Q;Q.push(node(x, y));vis[x][y] = true;while (!Q.empty()) {node tn = Q.front(); Q.pop();for (int i = 0; i < 4; i++) {int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];if (check(tx, ty)) {Q.push(node(tx, ty));vis[tx][ty] = true;}}}}//算面积,保存湖的位置void bfs(int x, int y) {queue<node> Q;int maxa = 0;Q.push(node(x, y));vis[x][y] = true;while (!Q.empty()) {node tn = Q.front(); Q.pop();maxa++;for (int i = 0; i < 4; i++) {int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];if (check(tx, ty)) {Q.push(node(tx, ty));vis[tx][ty] = true;}}}//将搜素的湖的位置和面积保存下来,用于后续排序和填充v.push_back(node(x, y, maxa));}//用陆地填充湖void Fill(int x, int y) {queue<node> Q;Q.push(node(x, y));mapp[x][y] = '*';vis[x][y] = 2;while (!Q.empty()) {node tn = Q.front(); Q.pop();for (int i = 0; i < 4; i++) {int tx = tn.x + dir[i][0], ty = tn.y + dir[i][1];if (checkf(tx, ty)) {Q.push(node(tx, ty));mapp[tx][ty] = '*';vis[tx][ty] = 2;}}}}int main(){cin >> n >> m >> k;memset(vis, 0, sizeof(vis));for (int i = 0; i < n; i++) {scanf("%s", mapp[i]);for (int j = 0; j < m; j++) {if (mapp[i][j] == '*') {vis[i][j] = true;}}}//把边缘靠海的水域排除掉for (int i = 0; i < n; i++) {if (!vis[i][0]) {bfso(i, 0);}if (!vis[i][m - 1]) {bfso(i, m - 1);}}for (int i = 0; i < m; i++) {if (!vis[0][i]) {bfso(0, i);}if (!vis[n - 1][i]) {bfso(n - 1, i);}}v.clear();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!vis[i][j]) {bfs(i, j);}}}//从小到大排序,尽可能先填充面积小的sort(v.begin(), v.end());int ans = 0, len = v.size();for (int i = 0; i < len - k; i++) {ans += v[i].area;Fill(v[i].x, v[i].y);}cout << ans << endl;for (int i = 0; i < n; i++) {puts(mapp[i]);}return 0;}
0 0
- codeforces723D Lakes in Berland 搜索
- codeforces723D-Lakes in Berland-DFS
- codeforces723D.Lakes in Berland(dfs)
- D. Lakes in Berland
- Lakes in Berland
- D. Lakes in Berland
- D.Lakes in Berland(DFS)
- CodeFroces723D Lakes in Berland BFS
- Codeforces723 D. Lakes in Berland (BFS)
- 【29.70%】【codeforces 723D】Lakes in Berland
- Codeforces373D-Lakes in Berland(dfs)
- codeforces 723D. Lakes in Berland [DFS]【】
- codeforce#375D. Lakes in Berland
- codeforces 723D Lakes in Berland
- Codeforces 723D - Lakes in Berland
- codeforces 723 D. Lakes in Berland
- CodeForces 723D Lakes in Berland DFS
- Codeforeces - 723D -Lakes in Berland
- JAVA中对象的序列化
- 【URAL 1183】Brackets Sequence(区间DP+路径记录)
- N的阶乘
- iOS开发之CLPlacemark属性一览(定位编码与反编码相关)
- 初品符号常量
- codeforces723D Lakes in Berland 搜索
- nmap系列之telnet
- As Easy As Possible
- mysql sql_mode 解决数据库非空无默认值依然可以插入的问题
- SpringMVC的各种参数绑定方式
- 排序总结系列一:冒泡排序
- A water problem
- Android SQLite Memento1 —— Basic SQL
- PHP 语句demo