当前位置: 首页 > news >正文

有特效的网站做网站需要多少钱

有特效的网站,做网站需要多少钱,做app的模板下载网站,网站建设日程表范文这篇博客主要记录了使用BFS解决FloodFill算法的几道题目&#xff0c;包括图像渲染、岛屿数量、岛屿的最大面积、被包围的区域。 class Solution {using PII pair<int, int>; public:vector<vector<int>> floodFill(vector<vector<int>>& im…

这篇博客主要记录了使用BFS解决FloodFill算法的几道题目,包括图像渲染、岛屿数量、岛屿的最大面积、被包围的区域。

class Solution {using PII = pair<int, int>;
public:vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {int prev = image[sr][sc];if(prev == color) return image;int m = image.size(), n = image[0].size();queue<PII> q;q.push({sr, sc});int dx[4] = {1, -1, 0, 0};int dy[4] = {0, 0, 1, -1};while(q.size()){auto [x, y] = q.front();q.pop();image[x][y] = color;for(int i = 0 ; i < 4 ; i++){int a = x + dx[i], b = y + dy[i];if(a >= 0 && a < m && b >= 0 && b < n && image[a][b] == prev){q.push({a, b});}}} return image;}
};

题目分析:对于这道题目,我们使用FloodFill算法去解决。首先,将image[sr][sc]上色,创建一个队列,队列元素为pair<int,int>,表示待上色元素的坐标,依次去遍历这个位置周围4个位置,看是不是需要上色,如果需要,那么把它放到队列中去,直到队列为空才停止。在遍历某一个位置时,设置int dx[4]={0,0,1,-1}和int dy[4]={1,-1,0,0}两个数组,依次去取对应位置组成当前位置坐标的偏移量。

class Solution {using PII = pair<int, int>;
public:int numIslands(vector<vector<char>>& grid) {int ret = 0;int m = grid.size(), n = grid[0].size();vector<vector<bool>> state(m, vector<bool>(n, false));queue<PII> q;int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};for(int i = 0 ; i < m ; i++){for(int j = 0 ; j < n ; j++){if(grid[i][j] == '0' || state[i][j] == true) continue;q.push({i, j});while(q.size()){PII pos = q.front();q.pop();int x = pos.first, y = pos.second;state[x][y] = true;for(int k = 0 ; k < 4 ; k++){int a = x + dx[k], b = y + dy[k];if(a >= 0 && a < m && b >= 0 && b < n && state[a][b] == false && grid[a][b] == '1'){q.push({a, b});state[a][b] = true;}}}ret++;}}return ret;}
};

题目分析:这道题我们需要创建一个大小为m*n的数组state,表示当前位置有没有被遍历过。依次去遍历每一个位置,当遍历到一块陆地后,把与它相连的陆地找到,然后ret++,直到遍历完整个数组。

class Solution 
{using PII = pair<int, int>;bool state[51][51] = {false};int m;int n;int ret = 0;int dx[4] = {0, 0, 1, -1};int dy[4] = {1,-1, 0, 0};
public:int maxAreaOfIsland(vector<vector<int>>& grid) {m = grid.size(); n = grid[0].size();for(int i = 0 ; i < m ; i++){for(int j = 0 ; j < n ; j++){int count = bfs(grid, i, j);ret = max(ret, count);}}return ret;}int bfs(vector<vector<int>>& grid, int i, int j){if(grid[i][j] == 0 || state[i][j]) return 0;queue<PII> q;int count = 0;q.push({i,j});state[i][j] = true;while(q.size()){auto [x, y] = q.front();q.pop();count++;for(int i = 0 ; i < 4 ; i++){int a = x + dx[i];int b = y + dy[i];if(a >= 0 && a < m && b >= 0 && b < n && grid[a][b] == 1 && !state[a][b]){q.push({a, b});state[a][b] = true;}}} return count;}
};

题目分析:依次遍历每一块岛屿,在找到更大面积的岛屿后,更新ret。具体的思路和前面的题一样。

class Solution 
{int m , n;int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};
public:void solve(vector<vector<char>>& board) {m = board.size(), n = board[0].size();for(int i = 0 ; i < n ; i++){if(board[0][i] == 'O') bfs(board, 0, i);if(board[m-1][i] == 'O') bfs(board, m-1, i);}for(int i = 0 ; i < m ; i++){if(board[i][0] == 'O') bfs(board, i, 0);if(board[i][n-1] == 'O') bfs(board, i, n-1);} //2.还原for(int i = 0 ; i < m ; i++){for(int j = 0 ; j < n ; j++){if(board[i][j] == 'O') board[i][j] = 'X';else if(board[i][j] == '.') board[i][j] = 'O';}}  }void bfs(vector<vector<char>>& board, int i, int j){queue<pair<int, int>> q;q.push({i,j});board[i][j] = '.';while(q.size()){auto [x,y] = q.front();q.pop();for(int k = 0 ; k < 4 ; k++){int a = x + dx[k];int b = y + dy[k];if(a >= 0 && a < m && b >= 0 && b < n && board[a][b] == 'O'){q.push({a, b});board[a][b] = '.';}}}}
};

题目分析:这道题由于边上的的“O”不是被围绕的区域,所以直接采用BFS不好处理。我们可以反着想,先处理边上的“O”区域,将其替换为“.”,然后扫描矩阵,如果遇到“O”则替换为“X”,如果遇到“.”则替换为“O”。

http://www.khdw.cn/news/67091.html

相关文章:

  • 新野做网站电商怎么做
  • 找潍坊做网站的seo整站优化方案案例
  • 临海网站制作郑州seo网络营销
  • 网站建设图文片域名解析ip138在线查询
  • wordpress 3.5 下载地址英文seo
  • 成都制作网站工作室正规的代运营公司
  • 北京美的网站百度小说风云榜2022
  • 免费生成网站软件下载电商培训视频教程
  • 为什么打开网站是建设中专业郑州企业网站建设
  • 网站是哪个建站公司做的b2b平台
  • 我们不是做网站的2023年适合小学生的新闻
  • 营销型网站建设的步骤辅导班培训机构
  • 龙华区网站建设百度网盘怎么找片
  • 找网站建设企业关键词网站
  • 黑龙江网站设计百度指数查询排行榜
  • 网站视频超链接怎么做seo外包公司兴田德润官方地址
  • 万网域名注册价格如何将网站的关键词排名优化
  • 天台县城市建设规划局网站外链网站
  • 书荒小说阅读器是哪个网站做的福州网站排名
  • 火花机 东莞网站建设优化网站的步骤
  • 做企业形象网站培训推广 seo
  • 如何网站做淘客关系网站优化公司
  • 临朐网站建设定制网站制作定制
  • 狮城app更多网站百度客服24小时人工电话
  • 成都网站快速优化排名朝阳区seo
  • 网站开发 需求说明书爱站网关键词
  • 做医疗信息网站的域名深圳推广网络
  • 网站开发费杭州seo技术培训
  • 电子商务型网站网络推广项目计划书
  • 安徽合肥做网站无锡seo公司哪家好