2016年7月29日 星期五

C129 油田數(c++)

// c129 有幾處油田,另加印最大處的數量
#include <iostream>
using namespace std;
int di[]={-1,1,0,0,-1,1,-1,1}; //上,下,左,右,左上,左下,右上,右下
int dj[]={0,0,-1,1,-1,-1,1,1};
char a[100][101];
int m,n , cnt;

void dfs(int i, int j)
{
   a[i][j]='*';     ++cnt;
//   cout <<cnt<<'*' << i <<',' << j << endl;

   for(int d=0; d<8; ++d)
   {
      int ni = i+di[d] , nj =  j+dj[d];
      if(ni<0 || ni>=m  || nj<0 || nj>=n  ) continue;
      if( a[ni][nj]=='@') dfs(ni,nj);
   }
   return;
}


int main()
{
  int i,j;
   while(cin >> m >> n)
   {
      if(m==0 && n==0) break; // m列n格, 0 0 結束
      int ans=0, mx=0;
      for(i=0;i<m;++i)
       cin >> a[i];  //第i列: a 是二維,字串可一整列讀入
      for(i=0;i<m;++i)
      {
         for(j=0; j<n; ++j)
         {
            if(a[i][j]=='@')
            {
               cnt=0;
               ++ans;
               dfs(i,j);
               if(cnt>mx) mx=cnt;
            }
         }
      }
      cout << ans <<' ' << mx << endl;   // 加印最大處的數量  原C129沒有
   }
   return 0;
}
/* 輸入資料
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
---- 輸出資料
0 0
1 5
2 2
2 8
*/

Related Posts:

  • 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
  • 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

0 意見:

張貼留言