Answer to Question #240790 in C for FAJLA

Question #240790

You have given a game board in 2D matrix format where ‘S’ is the start point and ‘E’ isthe end point. There is only a single path from ‘S’ to ‘E’. In this path you will be face 3 types of Vampires which are marked as ‘M’, ‘N’ and ‘O’. But to kill those vampires you need Blades. You will find 3 types of Blades on your way which are marked as ‘A’, ‘B’ and ‘C’. Use Blade ‘A’ to kill Vampire ‘M’, Blade ‘B’ to kill Vampire ‘N’ and Blade ‘C’ to kill Vampire ‘O’. You will have one chance to kill a vampire using a Blade. If you miss it, you will be killed by that vampire. For carrying those Blades you have given a Bag of size 100 which is initially empty. For speed attack this bag works as Last in first out order for accessing blades. Now simulate the game starting from ‘S’ for different game board input. 


1
Expert's answer
2021-09-22T23:48:31-0400
#include <stdio.h>
#define n 3


int x[n][n];
int y[n][n];


int min_steps(int i, int j, int array1[][n])
{
	if (i == n - 1 && j == n - 1)
		return 0;


	if (i > n - 1 || j > n - 1)
		return 9999999;


	if (y[i][j])
		return x[i][j];


	y[i][j] = 1;
	x[i][j] = 1 + min(min_steps(i + array1[i][j], j, array1),
					min_steps(i, j + array1[i][j], array1));


	return x[i][j];
}


int main()
{
	int array1[n][n] = { { 2, 1, 2 },
					{ 1, 1, 1 },
					{ 1, 1, 1 } };


	int res = min_steps(0, 0, array1);
	if (res >= 9999999)
	    printf(-1);
	else
		printf(res);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS