forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
132 lines (111 loc) · 3.62 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-07-13
#include <vector>
#include <string>
#include <iostream>
#include <queue>
#include <cassert>
using namespace std;
/// BFS
/// Time Complexity: O(m*n)
/// Space Complexity: O(m*n)
///
/// This problem is amazing! Because using DFS will lead to Runtime Error,
/// Because in some specific cases, the recursive depth might be too high
/// The following is an example:
/// OOOOOOOOO
/// XXXXXXXXO
/// OOOOOOOXO
/// OXXXXXOXO
/// OXOOOXOXO
/// OXOXOXOXO
/// OXOXXXOXO
/// OXOOOOOXO
/// OXXXXXXXO
/// OOOOOOOOO
///
/// We can see, in above test case, the complexity of recursive depth is O(n*m),
/// where n and m describe the size of board.
/// Obviously, it's too high!
class Solution {
private:
int m, n;
int d[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}};
private:
bool inArea(int x, int y){
return x >= 0 && y >= 0 && x < m && y < n;
}
bool bfs(const vector<vector<char>> &board, int x, int y,
vector<vector<bool>>& visited, vector<pair<int,int>>& record){
queue<pair<int,int>> q;
// return true if we can only get to 'X' during BFS,
// otherwise, return false
bool ret = true;
visited[x][y] = true;
q.push(pair<int,int>(x, y));
while( !q.empty()){
pair<int, int> cur = q.front();
q.pop();
record.push_back(pair<int, int>(cur.first, cur.second));
for(int i = 0 ; i < 4 ;i ++){
int newX = cur.first + d[i][0];
int newY = cur.second + d[i][1];
if(!inArea(newX, newY))
// If newX, newY is not in the area,
// it means we get out of the board in this BFS,
// we need to return false in this case
ret = false;
else if(board[newX][newY] == 'O' && !visited[newX][newY]){
visited[newX][newY] = true;
q.push(pair<int, int>(newX, newY));
}
}
}
return ret;
}
public:
void solve(vector<vector<char>>& board) {
m = board.size();
if(m == 0)
return;
n = board[0].size();
if(n == 0)
return;
vector<vector<bool>> visited(m, vector<bool>(n, false));
vector<pair<int,int>> record;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (board[i][j] == 'O' && !visited[i][j]){
// clear record before each time we run BFS
record.clear();
if(bfs(board, i, j, visited, record))
// If BFS return true,
// means from this position,
// we will not get out of the board.
// As a result, we can make every position we visited in this BFS from 'O' to 'X'
for(int k = 0 ; k < record.size() ; k ++)
board[record[k].first][record[k].second] = 'X';
}
return;
}
};
int main(){
int n = 4, m = 4;
string board_array[] = {
"XXXX",
"XOOX",
"XXOX",
"XOXX"};
vector<vector<char>> board = vector<vector<char>>(n, vector<char>(m, ' '));
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
board[i][j] = board_array[i][j];
Solution().solve(board);
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++)
cout << board[i][j];
cout << endl;
}
return 0;
}