当前位置:网站首页>Surrounded Regions
Surrounded Regions
2022-07-23 14:16:00 【One 829】
Title Description
Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
Example 1:

Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Explanation: Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Complete code:
// xxxOOO.cpp : This file contains "main" function . Program execution will start and end here .
//
#include <iostream>
using namespace std;
char board[201][201];
int book[201][201];
int n, m;
void dfs(int x, int y) {
int i, j, k,tx,ty;
// Four directions Right down left up
int next[4][2] = {
{0,1},{1,0},{0,-1},{-1,0}
};
for (k = 0;k < 4;k++) {
tx = x + next[k][0];// New coordinates
ty = y + next[k][1];
// Judgement boundary Out of bounds exit
if (tx < 1 || tx > n || ty < 1 || ty > m)
continue;
// If it is currently O Just judge If it is X Just skip
if (board[x][y] == 'O') {
// If the next point is also O It shows that the two are connected Directly flip the current point to X
if (board[tx][ty] == 'O'&&book[tx][ty]==0) {
book[tx][ty] = 1;
board[x][y] = 'X';
dfs(tx, ty);
}
// If the next point is X, That is, you need to judge whether the current point is surrounded by X, If so, turn it over , If there is O Then flip the current point
if (board[tx][ty] == 'X'&&book[tx][ty] == 0) {
book[tx][ty] = 1;
if (k ==2){
board[x][y] ='X';
return;
}
dfs(x, y);
}
}
}
return;
}
int main()
{
int i, j;
cin >> n >> m;
// input data
for (i = 1;i <= n;i++)
for (j = 1;j <= m;j++)
cin >> board[i][j];
// Except for the surrounding points
for(i=2;i<n-1;i++)
for(j=2;j<m-1;j++)
dfs(i, j);// Get into
cout << endl;
// The output matrix
for (i = 1;i <= n;i++)
{
for (j = 1;j <= m;j++)
cout << board[i][j] << " ";
cout << endl;
}
return 0;
}


边栏推荐
- Fabric.js 基础笔刷
- 酷睿i5 12490f和i5 12600k差距大吗
- 数据库连接池 & DBUtils
- Uiscrollview (uicollectionview) prohibits horizontal and vertical sliding at the same time
- How about the nuclear display performance of Ruilong R7 Pro 6850h? What level is it equivalent to
- Medium range
- How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
- 链表复习!
- Remember that a vulnhub target plane exercise successfully won the root permission
- Is machine learning difficult to get started? Tell me how I started machine learning quickly!
猜你喜欢

子序列 --- 编辑距离

Rtx3080 is equivalent to GTX. What kind of graphics card is rtx3080? What level is rtx3080

Notes on the seventh day

Overlayfs source code parsing

rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高

天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样

Configure the firetracker process, i.e. stepping on the pit record

Is machine learning difficult to get started? Tell me how I started machine learning quickly!

太平洋大西洋水流问题

ThreadLocal 面试夺命11连问
随机推荐
104 二叉树的最大深度 和 543 二叉树的直径和 124 二叉树的最大路径和
Using redis to realize distributed lock (single redis)
MySQL enables scheduled task execution
Description of test platform and hardware design
子序列 --- 编辑距离
动态规划-- 背包问题
Swift 16进制字符串与UIColor互转
Uiscrollview (uicollectionview) prohibits horizontal and vertical sliding at the same time
Changing the historical length of chart during LabVIEW operation
What level of rtx3070ti graphics card? What level of rtx3070ti graphics card? How about rtx3070ti graphics card
-bash: ifconfig: command not found
Kafka consumption reports an error coordinator unavailable Rediscovery will be attempt redisCovery
Drag and drop----
第十二天笔记
拖拽----
The difference between rtx3080ti and 3090. Which one is more cost-effective, rtx3080ti or 3090
单调栈!!!
Tensor, numpy, PIL format conversion and image display
将我理解的web3.0讲给你听
完全背包!