Red and Black hdu 1312

來源:互聯網
上載者:User

/* 1y 不錯的,昨天聽鐘超分析了dfs bdf 感覺好像有點懂起來了
   這個題目的意思很簡單,一開始我想深搜的時候怕逾時,但是還是覺得可以,就敲代碼了,
   可是遇到問題了,這個題目的行列輸入跟平時的不一樣,在這裡的就用了,傳統的習慣,糾結ing
   跟糾結的@的地方用了map[si][sj] = false 結果答案少了1,後來進行調試,也是鐘超哪裡學來的
   於是馬上發現錯誤了
 */
#include<iostream>//2374171 2010-04-23 12:59:21 Accepted 1312 15MS 300K 1053 B C++ 悔惜晟
#include<cstring>
using namespace std;

char map[22][22];
bool hash[22][22];
int w, h;
int count;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y)
{
 int i;
 if(x < 0 || x >= h || y < 0 || y >= w)
  return ;
 for(i = 0; i < 4; i++)
 {
  int tx = x + dir[i][0];
  int ty = y + dir[i][1];
  //if(map[tx][ty] == '#' || map[tx][ty] )
   //continue;
  if(map[tx][ty] == '.' && !hash[tx][ty])
  {
   hash[tx][ty] = true;
   dfs(tx, ty);
   //hash[tx][ty] = false;
  }
 }

}

int main()
{
 int i, j;
 int si, sj;
 while(cin>>w>>h)
 {
  if(w + h == 0)
   continue;
  for(i = 0; i < h; i++)
   for(j = 0; j < w; j++)//列
   {
    cin>>map[i][j];
    if(map[i][j] == '@')
    {
     si = i;
     sj = j;
    }
   }
  memset(hash, false , sizeof(hash));
  hash[si][sj] = true; //@
  count= 0;
  dfs(si, sj);
  int count = 0;
  for(i = 0; i < h; i++)
   for(j = 0; j < w; j++)//列
   {
    if(hash[i][j])
     count++;
   }
  cout<<count<<endl;
  
  

 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.