Question

C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located...

C Programming Language

Problem Title: Komodo National Park

Do You know that Komodo National Park, located in Nusa Tenggara Timur (NTT), is a
World Heritage Site by UNESCO, the specialized agency under United Nations (UN).

One day, Lili and Bibi were on vacation on one of the islands in the Komodo National

Park area and they just realized that they were living alone on that island because the cot-
tage owner was completing his unfinished business on the main island. We all know that

Komodo, as the endemic island, live in this archipelago. Lili is very worried, and won-
dering is there a barrier between the komodos on the island and their cottage. Lili once

read an article that said that when there is no food supply, komodos can prey on humans.

You, as the person who took Lili and Bibi to the island, are trying to whether the con-
ditions are safe for them to live on the island or not.

The map contains several characters that described the island with the cottage. You may
assume that the island can fit in the map and the map always in rectangular form
. The
characters inside the map are described below.
• S - indicates sea that surround the island. You may assume that all area outside
the map is always ocean area.
• H - is the location of the cottage that Lili and Bibi want to stay. It is guaranteed
that there is only one cottage in the island.
• K - indicates the position of all komodos in the island. Komodo is able to move
north, west, south, and east. Komodos in the island also quite lazy to swim or even
touch the water.

• F - is the secure barrier between the komodos and the island’s visitor.
• L - is the empty area inside the island.

Help Lili and Bibi to make sure their own security and safety from the dangerous Komodo.
You need to count how many dangerous Komodo as their threats. If it is not safe for
them, they should leave the island as soon as possible.

Format Input

Input consists of one integer T - number of test case. For every test case, two integers
follow - N and M - number of rows and columns on the map. Then, N lines follow.
Those N lines contain M characters for each line. All characters have been explained
above.

Format Output

Output should be expressed in format ”Case #X: Y ” - X is the number of test case, and
followed by Y - number of Komodo that should be secured from them.

Constraints

• 1 ≤ T ≤ 1000
• 1 ≤ N ≤ 100
• 1 ≤ M ≤ 100

Sample Input & Output (standard input & output)

2

3 3

SSS

SHS

KSS

Case #1: 0

4 5

SKKKS

SKKKS

SSHSS

SSSSS

Case #2: 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

logic:

use dfs to traverse the map starting with the positio nof H whenever encounter K increment ans and in case of S and F return from the current recursion.

code screenshot:

sample output:

code:


#include<stdio.h>
// function that calculate number of danger Komodo
void countdanger(int ix,int iy,int vis[101][101],char mat[101][101],int n,int m,int *ans){

   // if the current index is out of range return
   if(ix<0||ix>=n||iy<0||iy>=m){
       return;
   }

     // if the current index has been already visited return
   if(vis[ix][iy]){
       return;
   }

   // visit the current index
   vis[ix][iy]=1;
   // if the current index is sea or F it is safe so return
  
   if(mat[ix][iy]=='S'||mat[ix][iy]=='F'){
       return;
   }

   // if the current is K increment ans
   if(mat[ix][iy] =='K'){
       (*ans)++;
   }

   // recursive call to check in south ix+1, iy
   countdanger(ix+1,iy,vis,mat,n,m,ans);
   // recursive call to check in east ix, iy+1
   countdanger(ix,iy+1,vis,mat,n,m,ans);
   // recursive call to check in north ix11, iy
   countdanger(ix-1,iy,vis,mat,n,m,ans);
   // recursive call to check in west ix, iy-1
   countdanger(ix,iy-1,vis,mat,n,m,ans);
  

}
int main(){
   int t;
   scanf("%d",&t);
   int n,m;
   int stx,sty;      // variable to store staring x and y
   int caseno=1;     // variable to keep track of case
   char mat[101][101];
   int vis[101][101];   // 2d matrix to keep track of visited position

   while(t--){
      
          // initialising visited of all position to false
       for(int i=0;i<n;i++){
           for(int j=0;j<m;j++){

               vis[i][j]=0;

           }
       }  
      
       scanf("%d%d",&n,&m);                      // reading n m
      

       for(int i=0;i<n;i++){                      // READING N STRING
           scanf("%s",mat[i]);
       }

       for(int i=0;i<n;i++){           
           for(int j=0;j<m;j++){

              
               if( mat[i][j] == 'H'){           // checking for H to get starting position
                   stx=i;
                   sty=j;
               }

           }
       }


       int ans=0;

       countdanger(stx,sty,vis,mat,n,m,&ans);      // calling dfs from the starting position
      
       printf("Case #%d: %d\n",caseno,ans);

       caseno++;
   }
}

Add a comment
Know the answer?
Add Answer to:
C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...

    C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every time Bibi climbs a set of stairs, she counts the steps starting from 1 to the number of steps in that particular set of stairs while climbing the stairs. For example if she climbs two set of stairs, the first containing 5 steps and the second containing 3 steps, she will say 1, 2, 3, 4, 5, 1, 2, 3 and the total number...

  • Write in C language . Thank you Count Sheep Jojo is having problem to sleep at...

    Write in C language . Thank you Count Sheep Jojo is having problem to sleep at night. He can't fall asleep and it makes him feel tired every day. Having this problem, Jojo told his friend Bibi, and Bibi advised him to do sheep counting while he tries to sleep. Jojo decided to try using this trick for N nights, to test its effectiveness. Jojo realized that he would fall asleep if he imagined a total of 10 white sheep....

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • C Programming Language The code below matches the sample input/output but is marked wrong. Are there...

    C Programming Language The code below matches the sample input/output but is marked wrong. Are there other ways to do this problem? The focus is on recursion and functions. #include<stdio.h> int joCheck(unsigned long long int number) { if(number % 7 == 0 || number % 8 == 0) { return 1; } else return 0; } int main() { int cases = 0; scanf("%d", &cases); for(int i = 1; i<=cases; i++) { unsigned long long int number; scanf("%d", &number); if(joCheck(number)...

  • Write in C language Identical Prime Factors Today is another bad school day for Bibi. Her...

    Write in C language Identical Prime Factors Today is another bad school day for Bibi. Her math teacher just announced that the class' exam results are horrible: more than half students don't even get a passing grade! After babbling and lecturing the class for almost an hour, the teacher finally calmed down and told the class that she is giving them a chance to fix their score. She will give a challenge, and two fastest students to answer correctly can...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

    Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase of program compilation Assignment: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a Java, program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT