HDU ACM 1312解题报告

也是很水的一道题

直接用DFS或BFS搜索即可,甚至不用考虑剪枝

但是题目变量要注意,我就是没注意w和h,结果wa了

做题目一定要仔细

ContractedBlock.gif ExpandedBlockStart.gif Code
#include <iostream>
#include 
<queue>
using namespace std;
struct node
{
    
int x,y;
};
int w,h,sx,sy,sum;
int dir[4][2= {{1,0},{-1,0},{0,1},{0,-1}};
char map[25][25];
void BFS()
{
    queue
<node> q;
    node n,p;
    n.x 
= sx;
    n.y 
= sy;
    q.push(n);
    map[sx][sy] 
= '#';
    
while(!q.empty())
    {
        n 
= q.front();
        q.pop();
        
//sum++;
        for(int i = 0;i < 4;i++)
        {
            
int tx = n.x + dir[i][0];
            
int ty = n.y + dir[i][1];
            
//if(tx < 0 || tx >= w || ty < 0 || ty >= h)
                
//continue;
            
//if(map[tx][ty] == '#')
                
//continue;
            if(tx >= 0 && tx < h && ty >= 0 && ty < w && map[tx][ty] == '.')
            {
                sum
++;
                p.x 
= tx;
                p.y 
= ty;
                q.push(p);
                map[tx][ty] 
= '#';
            }
        }
    }
}
int main()
{
    
int i,j;
    
//freopen("1.txt","r",stdin);
    while(scanf("%d %d",&w,&h) == 2)
    {
        
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] == '@')
                {
                    sx 
= i;
                    sy 
= j;
                }
            }
        sum 
= 1;
        BFS();
        printf(
"%d\n",sum);
    }
    
return 0;
}

 

转载于:https://www.cnblogs.com/cnnbboy/archive/2009/04/10/1433305.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值