
|
| 1 |
|
| 5 |

洪水灌溉,思路:给该图外面包围一圈可遍历的的点,作为引流灌溉。
BFS外围一点,即可顺流而下的灌溉下去。
- #include
- #include
- #include
- #include
- #include
- #include
- #define endl '\n'
- #define YES puts("YES")
- #define NO puts("NO")
- #define mk make_pair
- #define x first
- #define y second
- #define umap unordered_map
- #define All(x) x.begin(),x.end()
- #pragma GCC optimize(3,"Ofast","inline")
- #define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
- using namespace std;
- const int N = 2e6 + 10;
- using PII = pair<int,int>;
- int n,m;
- umap<int,string>g;// 地图
-
- int dx[4] = {0,0,1,-1};
- int dy[4] = {1,-1,0,0};
-
- inline bool isRun(int x,int y)
- {
- return (x >= 0 && x < n && y >= 0 &&y < m && g[x][y] == '0');
- }
-
- inline void BFS(int tx,int ty)
- {
- queue
q; -
- q.emplace(mk(tx,ty));
-
- while(q.size())
- {
- PII now = q.front();
- q.pop();
-
- g[now.x][now.y] = '1'; // 标记被灌溉的点
-
- for(int i = 0;i < 4;++i)
- {
- int bx = now.x + dx[i];
- int by = now.y + dy[i];
- if(isRun(bx,by))
- {
- g[bx][by] = '1'; // 标记被灌溉的点
- q.emplace(mk(bx,by));
- }
- }
- }
- }
-
-
- inline void solve()
- {
- cin >> n >> m;
- m += 2; // 由于建了外圈,所以宽度 +2
-
- // 建立上下外圈
- for(int i = 0;i < m;++i)
- {
- g[0] += "0";
- g[n + 1] += "0";
- }
-
- for(int i = 1;i <= n;++i)
- {
- // 这里是输入地图后,建立两边的外圈
- string s;
- cin >> s;
- g[i] = "0" + s + "0";
- }
-
- n += 2; // 由于建立了上下外圈,所以高 + 2
-
- BFS(0,0); // 引流灌溉
-
- // 获取未被洪水淹没的重要区域
- int ans = 0;
- for(int i = 0;i < n;++i)
- {
- for(int j = 0;j < m;++j)
- {
- if(g[i][j] == '0') ++ans;
- }
- }
-
- cout << ans << endl;
- }
-
- int main()
- {
- // freopen("a.txt", "r", stdin);
- IOS;
- int _t = 1;
- // cin >> _t;
- while (_t--)
- {
- solve();
- }
-
- return 0;
- }
