#include <bits/stdc++.h>
using namespace std;
vector<string> detonate(const vector<string>& grid) {
int rows = grid.size();
int cols = grid[0].size();
vector<string> result(rows, string(cols, 'O')); // Create a grid full of bombs
cout<<"before detonating"<<endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout<<result[i][j]<<" ";
}
cout<<endl;
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (grid[i][j] == 'O') {
// Detonate bomb and clear surrounding cells
result[i][j] = '.';
if (i > 0) result[i - 1][j] = '.';
if (i < rows - 1) result[i + 1][j] = '.';
if (j > 0) result[i][j - 1] = '.';
if (j < cols - 1) result[i][j + 1] = '.';
}
}
}
cout<<"After detonating"<<endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout<<result[i][j]<<" ";
}
cout<<endl;
}
return result;
}
vector<string> bomberMan(int n, vector<string> grid) {
if (n == 1) return grid;
if (n % 2 == 0) return vector<string>(grid.size(), string(grid[0].size(), 'O'));
vector<string> firstDetonation = detonate(grid);
vector<string> secondDetonation = detonate(firstDetonation);
// intial state at 1
// 3->7->11>15->19 this all will be same as 1st state
// 5->9>13->17->21 this all will be asme as 2nd state
// 1st state find if(n%4 == 3) that means it there is the 1st state grid
// 2nd state finding if (n%4 == 1) RETURN the 2nd second State Grid
if(n%4 == 1) return secondDetonation;
return firstDetonation;
}
int main()
{
int rows = 3;
int cols = 3;
vector<string> grid(rows, string(cols, '.'));
grid[1][1] = 'O';
vector<string> st = bomberMan(11,grid);
// for(int i = 0; i<st.size(); i++){
// for(int j = 0; j<st[i].size(); j++){
// cout<<st[i][j]<<" ";
// }
// cout<<endl;
// }
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}