Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 146d180

Browse filesBrowse files
author
student114
committed
Add Solution python
1 parent dda78fd commit 146d180
Copy full SHA for 146d180

File tree

1 file changed

+46
-0
lines changed
Filter options

1 file changed

+46
-0
lines changed
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
def solve(self, board: List[List[str]]) -> None:
3+
"""
4+
Do not return anything, modify board in-place instead.
5+
"""
6+
if board:
7+
for i in range(len(board)):
8+
if board[i][0]=='O':
9+
self.bfs(board,i,0)
10+
if board[i][len(board[0])-1]=='O':
11+
self.bfs(board,i,len(board[0])-1)
12+
for j in range(len(board[0])):
13+
if board[0][j]=='O':
14+
self.bfs(board,0,j)
15+
if board[len(board)-1][j]=='O':
16+
self.bfs(board,len(board)-1,j)
17+
for i in range(len(board)):
18+
for j in range(len(board[0])):
19+
if board[i][j]=='O':
20+
board[i][j]='X'
21+
if board[i][j]=='2':
22+
board[i][j]='O'
23+
24+
def bfs(self,board,i,j):
25+
q = [[i,j]]
26+
while q!=[]:
27+
pos = q.pop(0)
28+
a = pos[0]
29+
b = pos[1]
30+
board[a][b] = '2'
31+
if 0<=a+1<len(board):
32+
if board[a+1][b]=='O':
33+
board[a+1][b]='2'
34+
q.append([a+1,b])
35+
if 0<=a-1<len(board):
36+
if board[a-1][b]=='O':
37+
board[a-1][b]='2'
38+
q.append([a-1,b])
39+
if 0<=b+1<len(board[0]):
40+
if board[a][b+1]=='O':
41+
board[a][b+1] = '2'
42+
q.append([a,b+1])
43+
if 0<=b-1<len(board[0]):
44+
if board[a][b-1]=='O':
45+
board[a][b-1] = '2'
46+
q.append([a,b-1])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.