#include <iostream> #include <utility> #include <string> #include <vector> #include <queue> using namespace std; #define N 4 #define M 4 int SearchingChallenge(string strArr[], int arrLength) { int n = arrLength, m = n, steps = 0, food = 0; // initial position of charlie int init_j = 0; int init_i = 0; queue<pair<int,int>> q; // directions vector<int> offsets = {0,-1,0,1,0}; vector<pair<int,int>> food_nodes; //store visited nodes, no need for extra work to be done. int visited_nodes[4][4] = {{0}}; // get number of food pieces for(int i = 0; i < m; i++){ for(int j = 0; j < n ; j++){ if(strArr[i][j] == 'F') { food++; } if(strArr[i][j] == 'C') { strArr[i][j] = 'O'; food_nodes.push_back({i,j}); } } } while(food_nodes.size()>0){ food_nodes.erase(food_nodes.begin()); int break_flag=0; q.push(food_nodes[0]); while(!q.empty()){ int size = q.size(); while(size-->0){ pair<int,int> p = q.front(); q.pop(); for(int k = 0; k < 4; k++){ int ii = p.first + offsets[k], jj = p.second + offsets[k+1]; /* if(ii == 0 && jj == 3) printf("HI"); */ if(jj >= 0 && jj < 4 && ii < 4 && ii >=0){ if(strArr[ii][jj] == 'F'){ strArr[ii][jj] = 'O'; while(!q.empty()) q.pop(); break_flag=1; food--; food_nodes.push_back({ii,jj}); break; } if(strArr[ii][jj] == 'O') q.push({ii,jj}); if(strArr[ii][jj] == 'H' && food == 0) return ++steps; } } if(break_flag==1) break; } steps++; if(break_flag==1) break; } } return 0; } int main(void) { // keep this function call here /* Note: In C++ you first have to initialize an array and set it equal to the stdin to test your code with arrays. */ //passing testcase //string A[4] = {"OOOO", "OOFF", "OCHO", "OFOO"}; //failing testcase string A[4] = {"FOOF", "OCOO", "OOOH", "FOOO"} int arrLength = sizeof(A) / sizeof(*A); cout << SearchingChallenge(A, arrLength); 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
}