【问题标题】:How to expand a number's radius from its center in a bidimensional array如何在二维数组中从中心扩展数字的半径
【发布时间】:2021-12-07 17:38:31
【问题描述】:

标题不够清楚,但我会解释一下练习,以便您更好地理解。

在这个练习中,用户输入p 来表示炸弹的“威力”和爆炸地点的 10x10 地图(以字符形式)。并且“威力”根据爆炸半径减1。

输出应该是带有爆炸痕迹的地图和安全点的数量(地图上的零)

所以,输入:

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

输出应该是:

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

在受到不同炸弹伤害的地点,最终的价值是这些伤害的总和。

这是我的代码,它只读取输入 p 和字符映射,并将标记为“X”的位置替换为炸弹的威力。其余的都是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;
}

如何编写代码来添加爆炸半径?

【问题讨论】:

  • 它可以在O(w*h*log(w*h)) 中使用 2d 快速傅里叶变换来解决,但这可能是一种矫枉过正

标签: arrays c multidimensional-array


【解决方案1】:

你需要一个距离函数norm() 和另一个二维数组outputmap[][]。计算完outputmap[][] 的条目后,您可以轻松获得保存点的数量。


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");
  }
}

【讨论】:

  • 谢谢,就像魅力一样
猜你喜欢
  • 2020-09-25
  • 2019-10-09
  • 2018-07-24
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多