2016年7月29日 星期五

a753最大面積(C++版)

// a753 最大面積 最多 AxB , 5<=A,B<=30
// 矩形一個 AXB ,查詢有 n 個, 每個高度為h 的最大面積,
// 如範例中 1的有7個、2的有5個、3的有6個、4的為0個 {1個的印0}
#include <iostream>
using namespace std;
int di[]={-1,1,0,0};
int dj[]={0,0,-1,1};
int a[30][30];
int A,B , cnt;

void dfs(int i, int j, int h)
{
   a[i][j]=0;     ++cnt;   // 設為0表示已走過 計數 +1

   for(int d=0; d<4; ++d) //上下左右

   {
      int ni = i+di[d] , nj =  j+dj[d];
      if(ni<0 || ni>=A  || nj<0 || nj>=B  ) continue;
      if( a[ni][nj]==h) dfs(ni,nj,h);
   }
   return;
}


int main()
{
  int i,j,h,n;
  int hcnt[10];
cin >> A >> B;
for(h=0; h<10; ++h) hcnt[h] = 0;  // 0~9
      for(i=0; i<A; ++i)
    for(j=0; j<B; ++j)
      cin >> a[i][j];
 // 建表 高度 h 的最大面積 放至 hcnt[h] 
   for(i=0;i<A;++i)
    for(j=0; j<B; ++j)
          if(a[i][j] != 0 )  '非0:某一高度 h, 以dfs求 h 面積
          {
               h = a[i][j];
               cnt=0;
               dfs(i,j,a[i][j]);
               if(cnt>hcnt[h]) hcnt[h]=cnt;  '這次h的面積比 前1個大存入表中
          }
 // n 個查詢 , 高度 h 的最大面積?
      cin >> n;
    while(n--)
    {
         cin >> h;
         cout << (hcnt[h]>=2 ? hcnt[h] : 0 ) << endl;  // <2的印 0
   }
   return 0;
}
/*  輸入範例
5 7
1 1 1 4 1 1 1
1 2 2 3 1 3 1
1 2 3 3 3 1 1
2 2 3 3 2 2 2
4 3 2 2 1 2 2
4
1
2
3
4
------輸出範例
7
5
6
0
*/

Related Posts:

  • b549 數位相片檔名/* b549 nr102-5 北二區102年-5 數位相片檔名  */ #include <iostream> #include <iomanip> #include <set> #include <map> using namespace… Read More
  • 1129C++ c/c++研習資料夾 #include <iostream> using namespace std; int p[ 100 ]; int pcnt; bool isp(int x) {      int i;  //  &… Read More
  • b547 巧克力口味/* b547-巧克力口味 */ #include <iostream> #include <sstream> #include <iomanip> #include <cstring> #include <cstdio> #includ… Read More
  • 2017,TOI-5月練習賽,自訂測資TOI-M5 第1,2,4題的自訂資料 TOI-M5 第3題的自訂資料 zerojudge已建題:c199、c200、c201、c202 … Read More
  • 篩法、陣列的應用篩法400.xlsx 找 2~n(<10^7)的所有質數 虛擬碼 動態陣列 p 、靜態陣列 c(n) 初值 false設為質數、true被刪記(非質數) push (2) for i= 3 ~ n jump 2   if not c(i) then   &nbs… Read More

0 意見:

張貼留言