AMCAT Automata Question bank | Set-1

AMCAT Automata Question bank | Set-1

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.

rn

Assumptions: 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.

rn

Existing Program

rn

int* cellCompete(int* cells,int days)

rn

{/

rn

/write your code here

rn

}

rn

//function signature ends

rn

 

rn

Test Cases

rn

TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]

rn


TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]

rn

Solution:

rn

#include<iostream>

rn

using namespace std;

rn

 int cellCompete(int *cells ,int day){

rn

//write your code here

rn

 for (int i=0;i<day;i++){

rn

 cells[-1]=0; //assumptions

rn

cells[8]=0;//assumptions

rn

int u[8]; //another array to copy value

rn

for (int i=-1;i<9;i++){

rn

u[i]=cells[i];

rn

}

rn

for(int j=0;j<8;j++){

rn

if(u[j-1]==u[j+1]){ //comparing the value of the neighbouring cells of u[]

rn

cells[j]=0; //changing value of cell according to condition

rn

}

rn

else

rn

cells[j]=1;

rn

} } 

rn

for (int i=0;i<8;i++){

rn

cout<<cells[i];

rn

}

rn

return 0;}

rn

 

rn

int main(){ //main function

rn

int days,cells[]={1,0,0,0,0,1,0,0}; //array to pass through function

rn

int *cellsptr=cells; //creating array values to pointer

rn

cout<<“enter days:”; //for days

rn

cin>>days;

rn

cout<<“n[1,0,0,0,0,1,0,0]n”;

rn

cellCompete(cellsptr, days); //passing to function

rn

return 0;

rn

}

rn

 

rn

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.

rn

Mooshak 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.

rn

EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.

rn

1 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

rn

Test Cases:
Case 1:

rn

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)

rn

Existing Program

rn

/*include header files needed by your program

rn

some libray functionality may be restricted

rn

define any function needed

rn

fuction signature begins, this function is required*/

rn

Int isPath(int **grid,int m,int n)

rn

{/*

rn

write your code here

rn

*/}

rn

/function signature ends 

rn

Solution:

rn

if (x,y outside maze) return false

rn

if (x,y is goal) return true

rn

if (x,y not open) return false

rn

mark x,y as part of solution path

rn

if (FIND-PATH(North of x,y) == true) return true

rn

if (FIND-PATH(East of x,y) == true) return true

rn

if (FIND-PATH(South of x,y) == true) return true

rn

if (FIND-PATH(West of x,y) == true) return true

rn

unmark x,y as part of solution path

rn

return false*/

rn

public boolean findPath(int x, int y){

rn

// check x,y are outside maze.

rn

if(x < 0 || x >= mazelength || y < 0 || y >= mazelength ){

rn

return false;

rn

}

rn

if(maze[x][y] == 9) {

rn

return true;

rn

}

rn

if(maze[x][y] != 1) return false;

rn

//mark x, y as part of the solution path

rn

if(maze[x][y] == 1){

rn

maze[x][y] = 3;

rn

}

rn

// move North

rn

if( findPath(x-1,y)){

rn

return true;

rn

}

rn

//move East

rn

if( findPath(x,y+1)) return true;

rn

//move South

rn

if( findPath(x+1,y)) return true;

rn

//move West

rn

if( findPath(x,y-1)) return true;

rn

// unMark x,y as part of the solution.

rn

maze[x][y] = 0;

rn

return false;

rn

}

rn

public void printSolution(){

rn

System.out.println(“Final Solution ::::::: “);

rn

for(int i=0;i<mazelength;i++){

rn

for(int j=0;j<mazelength;j++){

rn

System.out.print(” “+maze[i][j]+” “);

rn

}

rn

System.out.println();

rn

}

rn

}

rn

public static void main(String args[]){

rn

RatMazeProblem ratMazeProblem = new RatMazeProblem();

rn

System.out.println(” is Path exist ::: “+ratMazeProblem.findPath(0,0));

rn

ratMazeProblem.printSolution();             

rn

}

rn

}

c