杭电 1312题

//朴素的广度优先搜索,可以作为模板
#include <iostream>
using namespace std;
#define arraysize 21
char map[arraysize][arraysize];  //记录该迷宫的结构图
int si,sj;
typedef struct node
{
 int x;
 int y;
}node;
node myqueue[arraysize*arraysize];
int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}}; //定义四个方向
int sum ;
int w,h;
void BFS()
{
 sum = 1;
 int i,j;
 int start = 0;  //start指向第一个元素
 int end = 1;  //end指向数组中的元素的下一个元素
 myqueue[0].x = si;
 myqueue[0].y = sj;
 map[si][sj] = '#'; //别忘了此处标记该节点加入到队列中
 while(end!=start) //迭代终止条件
 {
  node frontnode,next;
  frontnode = myqueue[start];
  start++;   //去除头部元素
  for(i=0;i<4;++i) //四个方向进行遍历
  {
   next.x = frontnode.x+dir[i][0];
   next.y = frontnode.y+dir[i][1];
   if(next.x >=0 && next.x <h && next.y >=0 && next.y <w && map[next.x][next.y]!='#') //该点是否在正确的范围内并且该点是
   {
    sum++;
    map[next.x][next.y] = '#'; //记录该位置加入到队列中
    myqueue[end] = next;
    end++;
   }
  }
 }
 cout<<sum<<endl;
}
int main()
{
 //freopen("1.txt","r",stdin);
 int i,j;
 while(cin>>w>>h)
 {
  if(w==0 && h==0)
   break;
  for(i=0;i<h;++i)
  {
   for(j=0;j<w;++j)
   {
    cin>>map[i][j];
    if(map[i][j] == '@')
    {
     si = i;
     sj = j;
    }
   }    
  }
  BFS();
 }
 return 0;
}

转载于:https://www.cnblogs.com/north_dragon/archive/2010/05/04/1727157.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值