Q1. There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells (neighbour). Each day, for each cell, if its neighbours are both active and both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
rnAssumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumed to be always inactive.
Even after updating the cell state. Consider its previous state for updating the state of other cells. Update the cell information of all cells simultaneously.
Write a function cell Compete which takes one 8 element array of integer’s cells representing the current state of 8 cells and one integer days representing the number of days to simulate.
An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Existing Program
rnint* cellCompete(int* cells,int days)
rn{/
rn/write your code here
rn}
rn//function signature ends
rnrn
Test Cases
rnTESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
Solution:
rn#include<iostream>
rnusing namespace std;
rnint cellCompete(int *cells ,int day){
rn//write your code here
rnfor (int i=0;i<day;i++){
rncells[-1]=0; //assumptions
rncells[8]=0;//assumptions
rnint u[8]; //another array to copy value
rnfor (int i=-1;i<9;i++){
rnu[i]=cells[i];
rn}
rnfor(int j=0;j<8;j++){
rnif(u[j-1]==u[j+1]){ //comparing the value of the neighbouring cells of u[]
rncells[j]=0; //changing value of cell according to condition
rn}
rnelse
rncells[j]=1;
rn} }
rnfor (int i=0;i<8;i++){
rncout<<cells[i];
rn}
rnreturn 0;}
rnrn
int main(){ //main function
rnint days,cells[]={1,0,0,0,0,1,0,0}; //array to pass through function
rnint *cellsptr=cells; //creating array values to pointer
rncout<<“enter days:”; //for days
rncin>>days;
rncout<<“n[1,0,0,0,0,1,0,0]n”;
rncellCompete(cellsptr, days); //passing to function
rnreturn 0;
rn}
rnrn
Q2. Mooshak the mouse has been placed in a maze. There is a huge chunk of cheese somewhere in the maze. The maze is represented as a two-dimensional array of integers, where 0 represents walls.1 represents paths where Mooshak can move and 9 represents the huge chunk of cheese.
rnMooshak starts in the top left corner at 0.
Write a method is Path of class Maze Path to determine if Mooshak can reach the huge chunk of cheese. The input to is Path consists of a two dimensional array and for the maze matrix. The method should return 1 if there is a path from Mooshak to the cheese. And 0 if not Mooshak is not allowed to leave the maze or climb on walls.
EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
rn1 0 1 1 1 0 0 1
1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0
1 0 1 0 9 0 1 1
1 1 1 0 1 0 0 1
1 0 1 0 1 1 0 1
1 0 0 0 0 1 0 1
1 1 1 1 1 1 1 1
Test Cases:
Case 1:
Input:[[1,1,1,][9,1,1],[0,1,0]]
Expected return value :1
Explanation:
The piece of cheese is placed at(1,0) on the grid Mooshak can move from (0,0) to (1,0) to reach it or can move from (0,0) to (0,1) to (1,1) to (1,0)
Test case 2:
Input: [[0,0,0],[9,1,1],[0,1,1]]
Expected return value: 0
Explanation:
Mooshak cannot move anywhere as there exists a wall right on (0,0)
Existing Program
rn/*include header files needed by your program
rnsome libray functionality may be restricted
rndefine any function needed
rnfuction signature begins, this function is required*/
rnInt isPath(int **grid,int m,int n)
rn{/*
rnwrite your code here
rn*/}
rn/function signature ends
rnSolution:
rnif (x,y outside maze) return false
rnif (x,y is goal) return true
rnif (x,y not open) return false
rnmark x,y as part of solution path
rnif (FIND-PATH(North of x,y) == true) return true
rnif (FIND-PATH(East of x,y) == true) return true
rnif (FIND-PATH(South of x,y) == true) return true
rnif (FIND-PATH(West of x,y) == true) return true
rnunmark x,y as part of solution path
rnreturn false*/
rnpublic boolean findPath(int x, int y){
rn// check x,y are outside maze.
rnif(x < 0 || x >= mazelength || y < 0 || y >= mazelength ){
rnreturn false;
rn}
rnif(maze[x][y] == 9) {
rnreturn true;
rn}
rnif(maze[x][y] != 1) return false;
rn//mark x, y as part of the solution path
rnif(maze[x][y] == 1){
rnmaze[x][y] = 3;
rn}
rn// move North
rnif( findPath(x-1,y)){
rnreturn true;
rn}
rn//move East
rnif( findPath(x,y+1)) return true;
rn//move South
rnif( findPath(x+1,y)) return true;
rn//move West
rnif( findPath(x,y-1)) return true;
rn// unMark x,y as part of the solution.
rnmaze[x][y] = 0;
rnreturn false;
rn}
rnpublic void printSolution(){
rnSystem.out.println(“Final Solution ::::::: “);
rnfor(int i=0;i<mazelength;i++){
rnfor(int j=0;j<mazelength;j++){
rnSystem.out.print(” “+maze[i][j]+” “);
rn}
rnSystem.out.println();
rn}
rn}
rnpublic static void main(String args[]){
rnRatMazeProblem ratMazeProblem = new RatMazeProblem();
rnSystem.out.println(” is Path exist ::: “+ratMazeProblem.findPath(0,0));
rnratMazeProblem.printSolution();
rn}
rn}