亚特兰蒂斯
答案为 661 (搜索是蓝桥常考算法)
从任意一个角出发搜索整张图 , 我们需要维护两个变量 cnt 代表从 (0,0) 出发是否能到达其他三个角(0,29),(29,0),(29,29) ;还有ans表示答案个数
如果存在被彻底淹没的土地,那么四个角一定是相连通的(任意两点之间都存在路径可以相互到达)
假设(x,y)点会被彻底淹没那么从(x,y)这点原路返回一定可以回归四个角,那么从任意一个角出发一定可以找到路径 去往其他三个角 ( 通过(x,y)作为中间点 )。
那么我们就可以逮着其中一个点(0,0)点开始搜索并记录一共可以到达哪些为 '0' 的点
BFS做法
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 35 ; char g[50][50] = { "001000000001000010001001000000", "001000001101011010010000000111", "100000110000100101100000100000", "000001000100100000100000000001", "010001000010001100100010000010", "000000101101000010100100010001", "010000100000010000001011000000", "010001010011101100100001100100", "100000100110000000000010001001", "000000010011100011010101000000", "100101100011000000001001101000", "100000010010000100000100000000", "010000100000001100000011000001", "010000100010000001000001001100", "000001101010110001000000100000", "000000000000101000000100100001", "001010100111010100000010000010", "000000100100000000000100000000", "110011001000000000010000110011", "000001010010000010011100001000", "001011001011101010001000000001", "011000000100000100000100101100", "000100011100010111000100001011", "000010000000011001000000001010", "001000100100000000000100000000", "110000000101010101110110011110", "100001010001000000101100100011", "011001000100001100000000000000", "000000000000000000000000000000", "000000000000000000000000000000"} ; int n , m , cnt , ans ; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; bool st[N][N] ; struct node { int x , y ; }q[N * N ] ; void bfs() { int hh = 0 , tt = - 1 ; q[ ++ tt ] = {0 , 0 } ; st[0][0] = true ; while(hh <= tt ) { auto t = q[hh ++ ] ; // 判断是否能到达其余三个角 if( (t.x == 0 and t.y == m - 1 ) or (t.x == n - 1 and t.y == 0 ) or (t.x == n - 1 and t.y == m - 1 ) ) cnt ++ ; ans ++ ; for(int i = 0 ; i < 4 ; i ++ ) { int x = dx[i] + t.x , y = dy[i] + t.y ; if(x < 0 or x >= n or y < 0 or y >= m or g[x][y] == '1' )continue ; if(!st[x][y]) { st[x][y] = true ; q[++ tt ] = {x , y} ; } } } } int main() { n = 30 , m = 30 ; bfs() ; int c = 0 ; //如果 从 (0,0) 出发可以到达 其他三个 点说明 这张地图一定是能够汇聚四方洪水的 if(cnt == 3)cout << ans << endl ; // 此时答案就是从任意一个角搜索 所能蔓延到的 所有格子数量 else cout << 0 << endl ; return 0 ; }