您的位置:首页 > 其它

Red and Black hdu 1312

2010-04-23 13:07 435 查看
/* 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;
  
  

 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  2010 c