Home > Back-end >  How to expand a number's radius from its center in a bidimensional array
How to expand a number's radius from its center in a bidimensional array

Time:10-22

The title is not clear enough but I'll explain the exercise so you have a better understanding.

In this exercise, the user inputs p for the "power" of the bomb and the 10x10 map of where they are going to explode (in char). And the "power" decreases by 1 according to the explosion radius.

The output should be the map with the explosions traces and the number of safe spots (zeros on the map)

So, with the input:

3

O O O O O O O O O O
O X O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O X O O
O O O O O O O O O O
O O O O O O O O O O
O O O X O O O O O O
O O O O O O O O O O
O O O O O O O O O O

The output should be:

2 2 2 1 0 0 0 0 0 0
2 3 2 1 0 0 0 0 0 0
2 2 2 1 0 1 1 1 1 1
1 1 1 1 0 1 2 2 2 1
0 0 0 0 0 1 2 3 2 1
0 1 1 1 1 2 2 2 2 1
0 1 2 2 2 2 1 1 1 1
0 1 2 3 2 1 0 0 0 0
0 1 2 2 2 1 0 0 0 0
0 1 1 1 1 1 0 0 0 0

36

In the spots that receives damages from different bombs, the final value is the sum of those damages.

This is my code that only reads the input p and the char map and substitute the spots where are marked as 'X' for the power of the bomb. And the rest is filled with 0.

#include <stdio.h>

int main()
{
  int map[10][10], p, i, j, safe;
  char charmap[10][10];

  scanf("%d", &p);

  // int map is filled with 0
  for (i = 0; i < 10; i  )
    for (j = 0; j < 10; j  )
      map[i][j] = 0;

  // receives the char map, identifies where the Xs are located and in the same element, puts 'p' in the int map
  for (i = 0; i < 10; i  )
    for (j = 0; j < 10; j  )
    {
      scanf(" %c", &charmap[i][j]);
      if (charmap[i][j] == 'X')
        map[i][j] = p;
    }

  // prints the int map where the Xs are switched by 'p' and the Os by zeros
  for (i = 0; i < 10; i  )
  {
    for (j = 0; j < 10; j  )
    {
      printf("%d ", map[i][j]);
    }
    printf("\n");
  }
  return 0;
}

How could I make the code to add the radius of the explosion?

CodePudding user response:

you need a distancefunction norm() and another 2dim-array outputmap[][]. After you have calculated the entries of outputmap[][] you can get the number of savespots easy.


int norm( int i, int j, int n, int m )
{
  int din = i - n;
  if( din < 0 )
  {
    din *= -1;
  }

  int djm = j - m;
  if( djm < 0 )
  {
    djm *= -1;
  }

  if( din > djm )
  {
    return din;
  }else
  {
    return djm;
  }
}


int main(void)
{
  int x = 5;
  int y = 5;
  int map[y][x], p, i, j, safe;
  char charmap[y][x];
  int outputmap[y][x];
  scanf("%d", &p);

  // int map is filled with 0
  for (i = 0; i < y; i  )
  {  
    for (j = 0; j < x; j  )
    {
      map[i][j] = 0;
      outputmap[i][j] = 0;
    }
  }
  // receives the char map, identifies where the Xs are located and in the same element, puts 'p' in the int map
  for (i = 0; i < y; i  )
  {
    for (j = 0; j < x; j  )
    {
      scanf(" %c", &charmap[i][j]);
      if (charmap[i][j] == 'X')
      {
        map[i][j] = p;
      }
    }
  }

  for( i = 0; i < y; i   )
  {
    for( j = 0; j < x; j   )
    {
      if( map[i][j] == p )
      {
        printf( "X " );
      }else
      {
        printf( "0 " );
      }
    }
    printf("\n");
  }


  for( i = 0; i < y;   i )
  {
    for( j = 0; j < x;   j )
    {
      if( map[i][j] == p )
      {
        for( int n = 0; n < y;   n )
        {
          for( int m = 0; m < x;   m )
          {
            if( norm( i,j,n,m ) < p )
            {
              outputmap[n][m]  = p - norm( i,j,n,m );
            }
          }
        }
      }
    }
  }

  printf( "\n\n\n\n");

  for( i = 0; i < y; i   )
  {
    for( j = 0; j < x; j   )
    {
    
      printf( "%d ", outputmap[i][j] );
      
    }
    printf("\n");
  }
}
  • Related