Red and Black hdu 1312

/* 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;
  
  

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值