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
  • I would really appreciate it if someone can help me with the whole question. thank you....

    I would really appreciate it if someone can help me with the whole question. thank you. You may be familiar with Newton's Second Law of Motion, SF = mā. In English, this equation says: The sum, or net, (S) of the forces (F) acting upon an object equals (=) the mass (m) of the object multiplied by the object's acceleration (ā). Even if you are familiar with this famous equation, did you notice the arrows above F and a before?...

  • Someone plz plz help with this Statistics Intro to R programming question!!! Here are the examples and follow by my question!! Thank you so much!! I appreciate it !!!!My question!!!! Question T...

    Someone plz plz help with this Statistics Intro to R programming question!!! Here are the examples and follow by my question!! Thank you so much!! I appreciate it !!!!My question!!!! Question Type 1: If possible, calculate the 90% confidence intervals for the temperature it takes for crickets to chirp 15 chirps per second. Code (you must copy and paste your code like below in blue color): # Reading in the data Crickets-read.table(C:/Desktop/CricketChirpsvsTemperature.csv', header TRUE, #View Data View Crickets) #Data analysis...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

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