鱼C论坛

 找回密码
 立即注册
查看: 1532|回复: 2

[已解决]二维二进制矩阵

[复制链接]
发表于 2020-10-26 19:18:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

输入:

[
   ["1","0","1","0","0"],

   ["1","0","1","1","1"],

   ["1","1","1","1","1"],

   ["1","0","0","1","0"]

]

输出: 6

PS: 请用C语言回答,C++没学,看不懂代码,你回答得再好,最佳答案也不能是你的,请谅解 !
最佳答案
2020-10-28 01:46:19
// use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11


  1. #include <stdio.h>
  2. #include <stdint.h>

  3. int main( void )
  4. {
  5.     size_t map[][5] = { {1, 0, 1, 0, 0}
  6.                       , {1, 0, 1, 1, 1}
  7.                       , {1, 1, 1, 1, 1}
  8.                       , {1, 0, 0, 1, 0} };

  9.     ///////////////////////////////////////////////

  10.     const size_t row = sizeof(map)/sizeof(*map);
  11.     const size_t col = sizeof(*map)/sizeof(**map);
  12.     for( size_t c=0; c!=col; ++c )
  13.     {
  14.         size_t deepth = 0;
  15.         for( size_t r=0; r!=row; ++r )
  16.         {
  17.             if( map[r][c] != 0 )
  18.                 map[r][c] = ++deepth;
  19.             else
  20.                 deepth = 0;
  21.         }
  22.     }

  23.     size_t s_max = 0;
  24.     for( size_t r=0; r!=row; ++r )
  25.     {
  26.         for( size_t c=0; c!=col; ++c )
  27.         {
  28.             size_t deepth = SIZE_MAX;
  29.             for( size_t width=0; c+width!=col && map[r][c+width]!=0; ++width )
  30.             {
  31.                 deepth = deepth<map[r][c+width] ? deepth : map[r][c+width];
  32.                 s_max = s_max>(width+1)*deepth ? s_max : (width+1)*deepth;
  33.             }
  34.         }
  35.     }
  36.    printf( "%zu\n", s_max );
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-28 01:46:19 | 显示全部楼层    本楼为最佳答案   
// use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11


  1. #include <stdio.h>
  2. #include <stdint.h>

  3. int main( void )
  4. {
  5.     size_t map[][5] = { {1, 0, 1, 0, 0}
  6.                       , {1, 0, 1, 1, 1}
  7.                       , {1, 1, 1, 1, 1}
  8.                       , {1, 0, 0, 1, 0} };

  9.     ///////////////////////////////////////////////

  10.     const size_t row = sizeof(map)/sizeof(*map);
  11.     const size_t col = sizeof(*map)/sizeof(**map);
  12.     for( size_t c=0; c!=col; ++c )
  13.     {
  14.         size_t deepth = 0;
  15.         for( size_t r=0; r!=row; ++r )
  16.         {
  17.             if( map[r][c] != 0 )
  18.                 map[r][c] = ++deepth;
  19.             else
  20.                 deepth = 0;
  21.         }
  22.     }

  23.     size_t s_max = 0;
  24.     for( size_t r=0; r!=row; ++r )
  25.     {
  26.         for( size_t c=0; c!=col; ++c )
  27.         {
  28.             size_t deepth = SIZE_MAX;
  29.             for( size_t width=0; c+width!=col && map[r][c+width]!=0; ++width )
  30.             {
  31.                 deepth = deepth<map[r][c+width] ? deepth : map[r][c+width];
  32.                 s_max = s_max>(width+1)*deepth ? s_max : (width+1)*deepth;
  33.             }
  34.         }
  35.     }
  36.    printf( "%zu\n", s_max );
  37. }
复制代码

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
乐乐学编程 + 1 + 1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-28 08:17:45 | 显示全部楼层
谢谢解答!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-2 11:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表