need help Problem 2-Name your class Totally NotPacman You are develo ping a new
ID: 3705422 • Letter: N
Question
need help
Explanation / Answer
since the language is not mentioned. I have added in java
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Maze
{
public static double difficult(int[][] maze,int r,int c){
int count0=0,count2=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(maze[i][j]==2){
count2++;
}
if(maze[i][j]==0||maze[i][j]==3){
count0++;
}
}
}
return (double)count2/(count0+count2);
}
public static boolean isUnderThreat(int[][] maze,int r,int c){
boolean foundMain=false;
int i,j=0;
//find the position of main
for( i=0;i<r;i++){
for( j=0;j<c;j++){
if(maze[i][j]==3){
foundMain=true;
break;
}
}
if(foundMain)
break;
}
if(!foundMain)
return false;
int rowmain=i;
int colmain=j;
int enemies=0;
// crawl for 8 cells in all directions
for( i=rowmain-8;i<rowmain;i++){
if(i<0)
continue;
if(maze[i][colmain]==2)
enemies++;
}
for( i=colmain-8;i<colmain;i++){
if(i<0)
continue;
if(maze[rowmain][i]==2)
enemies++;
if(enemies>=2)
return true;
}
for( i=rowmain+1;i<rowmain+8;i++){
if(i>=r)
break;
if(maze[i][colmain]==2)
enemies++;
if(enemies>=2)
return true;
}
for( i=colmain+1;i<colmain+8;i++){
if(i>=c)
break;
if(maze[rowmain][i]==2)
enemies++;
if(enemies>=2)
return true;
}
if(enemies>=2)
return true;
else
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
int[][] maze={{1,1,2,1,0,1,1},{1,1,0,2,0,1,1},{1,0,0,1,1,1,1},{1,0,1,0,0,0,1},{1,0,1,0,1,0,1},{0,3,0,2,1,0,1}};
System.out.println(difficult(maze,6,7));
System.out.println(isUnderThreat(maze,6,7));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.