-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130.cpp
More file actions
65 lines (59 loc) · 1.66 KB
/
130.cpp
File metadata and controls
65 lines (59 loc) · 1.66 KB
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
//
// 130.cpp
// LeetCode
//
// Created by 张佐玮 on 15/8/8.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.size() <= 2 || board[0].size() <= 2) {
return;
}
bfsBound(board);
modifyBoard(board, 'O', 'X');
modifyBoard(board, '#', 'O');
}
void modifyBoard(vector<vector<char>> &board, char src, char dst) {
for (auto &row: board) {
for (auto &ch: row) {
if (ch == src) {
ch = dst;
}
}
}
}
void bfsBound(vector<vector<char>> &board) {
for (int i = 0; i < board[0].size(); i++) {
markPound(board, 0, i);
markPound(board, (int) board.size() - 1, i);
}
for (int i = 1; i < board.size() - 1; i++) {
markPound(board, i, 0);
markPound(board, i, (int) board[0].size() - 1);
}
}
void markPound(vector<vector<char>> &board, int row, int col) {
if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != 'O') {
return;
}
board[row][col] = '#';
if (row > 1) {
markPound(board, row - 1, col);
}
if (row < board.size() - 2) {
markPound(board, row + 1, col);
}
if (col > 1) {
markPound(board, row, col - 1);
}
if (col < board[0].size() - 2) {
markPound(board, row, col + 1);
}
}
};