leetcode surround regions
来源:互联网 发布:钢之炼金术师知乎 编辑:程序博客网 时间:2024/05/18 10:21
错误代码1:
class Solution {public: const int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; bool isvalid(int tmph, int tmpw,const int h,const int w){ return (tmph > 0 && tmph < h - 1 && w > 0 && w < tmpw - 1); } bool isEdge(int tmph, int tmpw,const int h, const int w){ if(tmph == 0 || tmph == h - 1 || tmpw == 0 || tmpw == w - 1){ return true; } return false; } bool isAbleToFill(int tmph, int tmpw, const int h, const int w, vector<vector<char> >&board){ bool isAble = true; if(board[tmph][tmpw] == 'X'){ return true; } for(int i = 0; i < 4; ++i){ int tmpx = tmph + dir[i][0]; int tmpy = tmpw + dir[i][1]; if(isvalid(tmpx, tmpy, h, w) || ()) } } void solve(vector<vector<char>> &board) { int h = board.size(); if(h == 0){ return; } int w = board[0].size(); if(w == 0){ return; } if(h <= 2 || w <= 2){ return; } int uph = 1, dwh = h - 2, lw = 1, rw = w - 2; while(uph <= dwh && lw <= rw){ // a rectangle for(int i = lw; i <= rw; ++i){ isAbleToFill(uph, i, h, w, board); isAbleToFill(dwh, i, h, w, board); } for(int i = uph; i <= dwh; ++i){ isAbleToFill(i, lw, h, w, board); isAbleToFill(i, rw, h, w, board); } uph++; dwh--; lw++; rw--; } }};
思路:最外圈的’0’无法被capture
,从外向内一圈一圈的扫描,看到有’0’,就看它上下左右的四个board格子里的值,如果都是’X’,就可以被capture
,否则,如果有可以被capture
的’0’,也可以被caputre
但是上面这段代码的问题在于:
判断是否有可以被capture
的0
时,需要递归,也即BFS
额。。bfs版本晚上洗完了衣服就写~
0 0
- leetcode surround regions
- LeetCode : Surrounded Regions
- [leetcode] Surrounded Regions
- leetcode 128: Surrounded Regions
- LeetCode 39: Surrounded Regions
- 【leetcode】Surrounded Regions
- LeetCode - Surrounded Regions
- LeetCode之Surrounded Regions
- leetcode:Surrounded Regions
- [LeetCode]Surrounded Regions
- LeetCode - Surrounded Regions
- [Leetcode]Surrounded Regions
- Leetcode Surrounded Regions
- [leetcode]Surrounded Regions
- leetcode Surrounded Regions
- Leetcode: Surrounded Regions
- LeetCode-Surrounded Regions
- [leetcode] Surrounded Regions
- ViewPager onPageChangeListener总结
- 第二周项目4 图书馆的书
- NEUQ 1431:快速幂入门
- #个人赛第四场解题总结#
- php while
- leetcode surround regions
- jquery easyui combobox设置默认选中项
- Sicily 1402. Panic Room
- 函数可重入性及编写规范
- Json数据格式的使用规则以及数据格式的选择
- FZU(2184)——逆序数还原
- 类的两种关系——继承和复合
- java判断字符串中是否有汉字并提取所有汉字部分
- [转]你想一辈子缩在角落里还是为梦想不顾一切?