It is java
Drunken Cockroach Problem
Expand the 1-dimension drunken cockroach problem to 2 dimensions. The cockroach can move left or right or up or down. The cockroach can move horizontally – left or right or virtically – up or down. Again the cockroach moves until it visits all the tiles at least once.
using this code
#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;
int main()
{
const int worldsize = 10;
default_random_engine e(1776);
uniform_int_distribution<int> ustagger(-1, 1);
uniform_int_distribution<int> uplace(0, worldsize);
int visits[worldsize];
for (int k = 0; k < worldsize; ++k)
visits[k] = 0;
int unvisited = worldsize;
int initialpos = uplace(e);
visits[initialpos] = 1;
unvisited--;
for (int k = 0; k < worldsize; ++k)
cout << visits[k] << ' ';
cout << endl;
system("pause");
int pos = initialpos;
while (unvisited > 0)
{
int newpos = pos + ustagger(e);
while (newpos < 0 || worldsize <= newpos || newpos == pos)
newpos = pos + ustagger(e);
pos = newpos;
visits[pos]++;
if (visits[pos] == 1)
unvisited--;
for (int k = 0; k < worldsize; ++k)
cout << visits[k] << ' ';
cout << endl;
system("pause");
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;
//defining worldsize as a global constant
//note: now that the world is 2 dimensional, use smaller values for worldsize
//otherwise it will take too much time to visit all tiles. here, I'm setting
//it as 3, which means our world is a 3*3 matrix
#define worldsize 3
//method to print the 2D array of dimensions: worldsize x worldsize
void printArr(int arr[worldsize][worldsize]){
for (int i = 0; i < worldsize; ++i){
for (int j = 0; j < worldsize; ++j){
cout << arr[i][j] << ' ';
}
cout<<endl;
}
}
int main()
{
default_random_engine e(1776);
uniform_int_distribution<int> ustagger(-1, 1);
uniform_int_distribution<int> uplace(0, worldsize);
//declaring 2d array instead of 1D
int visits[worldsize][worldsize];
//filling with 0
for (int i = 0; i < worldsize; ++i)
for (int j = 0; j < worldsize; ++j)
visits[i][j] = 0;
int unvisited = worldsize*worldsize; //3*3=9 unvisited locations
int r = uplace(e); //starting row
int c = uplace(e); //starting column
visits[r][c] = 1; //marking start location
unvisited--;
printArr(visits); //printing world
system("pause");
//looping
while (unvisited > 0)
{
//using ustagger to determine where we want to go (up/down or right/left)
if(ustagger(e)==0){
//going up or down
int newr = r + ustagger(e);
while (newr < 0 || worldsize <= newr || newr == r)
newr = r + ustagger(e);
r=newr;
}else{
//going left or right
int newc = c + ustagger(e);
while (newc < 0 || worldsize <= newc || newc == c)
newc = c + ustagger(e);
c=newc;
}
//marking visits to new location
visits[r][c]++;
if (visits[r][c] == 1)
unvisited--; //one more unvisited location is now visited
printArr(visits); //displaying world
system("pause");
}
}
/*OUTPUT (partial)*/
0 0 1
0 0 0
0 0 0
Press any key to continue . . .
0 1 1
0 0 0
0 0 0
Press any key to continue . . .
1 1 1
0 0 0
0 0 0
Press any key to continue . . .
1 2 1
0 0 0
0 0 0
Press any key to continue . . .
1 2 1
0 1 0
0 0 0
Press any key to continue . . .
1 2 1
0 1 1
0 0 0
Press any key to continue . . .
...
...
...
8 17 9
9 19 8
3 5 1
Press any key to continue . . .
Get Answers For Free
Most questions answered within 1 hours.