Question

Can someone help me with this program in C++. I would really appreciate your help. Thank You.
Given a 2D board containing X and O, capture all regions surrounded by X A region is captured by flipping all Os into

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
#define fi first
#define se second
const int MAX_SIZE = 200;
using namespace std;

bool containsFreeO(int i, int j, char (&c)[MAX_SIZE][MAX_SIZE], bool (&used1)[MAX_SIZE][MAX_SIZE], int n) {
//Base case
if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
return true;
}

//Go Up
if (i - 1 >= 0) {
if (!used1[i - 1][j]) {
if (c[i - 1][j] == 'O') {
used1[i - 1][j] = 1;
if (containsFreeO(i - 1, j, c, used1, n)) {
return true;
}
}
}
}

//Go Down
if (i + 1 < n) {
if (!used1[i + 1][j]) {
if (c[i + 1][j] == 'O') {
used1[i + 1][j] = 1;
if (containsFreeO(i + 1, j, c, used1, n)) {
return true;
}
}
}
}

//Go Left
if (j - 1 >= 0) {
if (!used1[i][j - 1]) {
if (c[i][j - 1] == 'O') {
used1[i][j - 1] = 1;
if (containsFreeO(i, j - 1, c, used1, n)) {
return true;
}
}
}
}

//Go Right
if (j + 1 < n) {
if (!used1[i][j + 1]) {
if (c[i][j + 1] == 'O') {
used1[i][j + 1] = 1;
if (containsFreeO(i, j + 1, c, used1, n)) {
return true;
}
}
}
}

return false;

}

void fillWithXes(int i, int j, char (&c)[MAX_SIZE][MAX_SIZE], bool (&used2)[MAX_SIZE][MAX_SIZE], int n) {
c[i][j] = 'X';

//Go Up
if (i - 1 >= 0) {
if (!used2[i - 1][j]) {
if (c[i - 1][j] == 'O') {
used2[i - 1][j] = 1;
fillWithXes(i - 1, j, c, used2, n);
}
}
}

//Go Down
if (i + 1 < n) {
if (!used2[i + 1][j]) {
if (c[i + 1][j] == 'O') {
used2[i + 1][j] = 1;
fillWithXes(i + 1, j, c, used2, n);
}
}
}

//Go Left
if (j - 1 >= 0) {
if (!used2[i][j - 1]) {
if (c[i][j - 1] == 'O') {
used2[i][j - 1] = 1;
fillWithXes(i, j - 1, c, used2, n);
}
}
}

//Go Right
if (j + 1 < n) {
if (!used2[i][j + 1]) {
if (c[i][j + 1] == 'O') {
used2[i][j + 1] = 1;
fillWithXes(i, j + 1, c, used2, n);
}
}
}
}

int main()
{
freopen("input.txt", "r", stdin);

int n;
cin >> n;
char c[MAX_SIZE][MAX_SIZE];
bool used1[MAX_SIZE][MAX_SIZE];
bool used2[MAX_SIZE][MAX_SIZE];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> c[i][j];
used1[i][j] = used2[i][j] = 0;
}
}

vector < pair < int,int > > vc;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (c[i][j] == 'O')
if (!used1[i][j] && !containsFreeO(i, j, c, used1, n))
vc.push_back({i,j});
}
}

for (int i = 0; i < vc.size(); i++) {
fillWithXes(vc[i].fi, vc[i].se, c, used2, n);
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << c[i][j] << " ";
}
cout << "\n";
}

return 0;
}
|input.txt Notepad File Edit Format View Help хххXхо хохоох ххооох хххохх хоххоо ххXXXX

ххXXхо хххххх хххххх хххXхх ххXхоо хххххх Process returned @ (Өx0) Press any key to continue. execution time 0.189 s

comment down for any queries

please give a thumbs up

Add a comment
Know the answer?
Add Answer to:
Can someone help me with this program in C++. I would really appreciate your help. Thank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT