https://leetcode-cn.com/problems/swim-in-rising-water/
思路:这不是和昨天的题基本一样吗……直接二分带走。
class Solution {
public:
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
bool bfs(vector<vector<int>>& grid,int time)
{
if(grid[0][0]>time)
return 0;
int n=grid.size();
vector<vector<bool>> vis(n,vector<bool>(n));
using pr=pair<int,int>;
queue<pr> q;
q.emplace(0,0);
vis[0][0]=1;
while(!q.empty())
{
pr fon=q.front();
q.pop();
if(fon.first==n-1&&fon.second==n-1)
return 1;
for(int i=0;i<4;i++)
{
int dx=fon.first+dir[i][0];
int dy=fon.second+dir[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<n&&!vis[dx][dy]&&grid[dx][dy]<=time)
q.emplace(dx,dy),vis[dx][dy]=1;
}
}
return 0;
}
int swimInWater(vector<vector<int>>& grid) {
int n=grid.size();
int l=0,r=n*n-1,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(bfs(grid,mid))
r=mid-1;
else
l=mid+1;
}
return l;
}
};