surrounded regions
递归的方法,最边缘的一系列元素,访问一次,递归。属于DFS的方式,中途为了简化,是只能朝一个方向进行DFS,但是方法没通过,本来想用DP,但是貌似不能用,向量P代表该元素是否访问过,不过后来发现其实可以,将该board数组进行替换,替换为其他元素,这样就可以了判断是否访问过。
但是这个方法是不行的,我在网上找了一些解决方案,大部分和自己的方案其实差不多,而且对于那些算法的DFS和BFS其实比我第二种方案更耗时,而且我自己测试过,其实对于大数据是通不过的,也不知道他们是怎么accepted
超时
private:
int lenx;
int leny;
public:
void solve(vector<vector<char>> &board) {
lenx = board.size();
if(lenx<=0){
return ;
}
leny = board[0].size();
if(leny<=0){
return;
}
bool P[100][100];
for(int i = 0;i<lenx;i++){
for(int j = 0;j<leny;j++){
P[i][j] = false;
}
}
for(int i = 0;i<leny;i++){
if(board[0][i] == 'o'){
ChangeState(0,P,board,0,i);//从上到下
}
if(board[lenx-1][i] == 'o'){
ChangeState(3,P,board,lenx-1,i);//从下道上
}
}
for(int j = 0;j<lenx;j++){
if(board[j][0] == 'o'){
ChangeState(1,P,board,j,0);//从左到右
}
if(board[j][leny-1] == 'o'){
ChangeState(2,P,board,j,leny-1);//从右到左
}
}
for(int i = 0;i<lenx;i++){
for(int j = 0;j<leny;j++){
if(!P[i][j]&&board[i][j] == 'o'){
board[i][j] = 'x';
}
}
}
}
void ChangeState(int direct,bool P[100][100],vector<vector<char>> &board,int i,int j){
if(i<0||j<0||i>=lenx||j>=leny||P[i][j]){
return;
}
P[i][j] = true;
if(board[i][j] == 'x'){
return;
}
else{
if(direct == 0){//从上到下
ChangeState(0,P,board,i+1,j);
ChangeState(0,P,board,i,j+1);
ChangeState(0,P,board,i,j-1);
}
else if(direct == 1){//从左到右
ChangeState(1,P,board,i,j+1);
ChangeState(1,P,board,i+1,j);
ChangeState(1,P,board,i-1,j);
}
else if(direct == 2){//从右到左
ChangeState(2,P,board,i,j-1);
ChangeState(2,P,board,i+1,j);
ChangeState(2,P,board,i-1,j);
}
else if(direct == 3){//从下倒上
ChangeState(3,P,board,i,j-1);
ChangeState(3,P,board,i,j+1);
ChangeState(3,P,board,i-1,j);
}
}
}
新的方法,完全用DFS,去掉状态矩阵P
class Solution {
private:
int lenx;
int leny;
public:
void solve(vector<vector<char>> &board) {
lenx = board.size();
if(lenx<=0){
return ;
}
leny = board[0].size();
if(leny<=0){
return;
}
for(int i = 0;i<leny;i++){
if(board[0][i] == 'O'){
ChangeState(0,board,0,i);//从上到下
}
if(board[lenx-1][i] == 'O'){
ChangeState(3,board,lenx-1,i);//从下道上
}
}
for(int j = 0;j<lenx;j++){
if(board[j][0] == 'O'){
ChangeState(1,board,j,0);//从左到右
}
if(board[j][leny-1] == 'O'){
ChangeState(2,board,j,leny-1);//从右到左
}
}
for(int i = 0;i<lenx;i++){
for(int j = 0;j<leny;j++){
if(board[i][j] == '#'){
board[i][j] = 'O';
}
else{
board[i][j] = 'X';
}
}
}
}
void ChangeState(int direct,vector<vector<char>> &board,int i,int j){
if(i<0||j<0||i>=lenx||j>=leny){
return;
}
if(board[i][j] != 'O'){
return;
}
else{
board[i][j] = '#';
if(direct == 0){//从上到下
ChangeState(0,board,i+1,j);
ChangeState(0,board,i,j+1);
ChangeState(0,board,i,j-1);
}
else if(direct == 1){//从左到右
ChangeState(1,board,i,j+1);
ChangeState(1,board,i+1,j);
ChangeState(1,board,i-1,j);
}
else if(direct == 2){//从右到左
ChangeState(2,board,i,j-1);
ChangeState(2,board,i+1,j);
ChangeState(2,board,i-1,j);
}
else if(direct == 3){//从下倒上
ChangeState(3,board,i,j-1);
ChangeState(3,board,i,j+1);
ChangeState(3,board,i-1,j);
}
}
}
};
这个方式也是不行,崩溃~
最后这种方法是通过了的,其实大体思想一样,但是避免了很多递归,而且减少了很多比较次数,这也就是为什么通过的原因吧。这个方法,主要是将与边缘的O可联通的O都如队列,然后将他们全部修改为Z,最后将Z修改为O,而O修改为X。
class Solution {
public:
queue<int> q;
int m, n;
void add(int x, int y, vector<vector<char>> &board) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
board[x][y] = 'Z';
q.push(x * n + y);
}
}
void traversal(int x, int y, vector<vector<char>> &board) {
add(x, y, board);
while (!q.empty()) {
int p = q.front();
q.pop();
int px = p / n, py = p % n;
add(px - 1, py, board);
add(px + 1, py, board);
add(px, py - 1, board);
add(px, py + 1, board);
}
}
void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (board.empty() || board.size() == 0 || board[0].size() == 0) {
return;
}
m = board.size(), n = board[0].size();
for (int i = 0; i < n; i++) {
traversal(0, i, board);
traversal(m - 1, i, board);
}
for (int i = 0; i < m; i++) {
traversal(i, 0, board);
traversal(i, n - 1, board);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = board[i][j] == 'Z' ? 'O' : 'X';
}
}
}
};