pku1979

 

Source Code

Problem: 1979 User: henry11
Memory: 356K Time: 0MS
Language: G++ Result: Accepted
  • Source Code
  • /*
    廣度搜索 
    queue 為訪問堆; 
    status 表示訪問狀態;
    map 表示地圖; 
    */
    
    #include<stdio.h>
    #include<string.h>
    
    struct node
    {
        int a, b;
    };
    
    int direct[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
    node queue[401];
    char map[21][21];
    int head, tail;
    int count, len, height;
    
    void initQueue(int x, int y)
    {
        queue[0].a = x;
        queue[0].b = y;
        map[x][y] = '*';
        head = tail = 0;
    }         
    
    void push(int x, int y)
    {
        queue[tail+1].a = x;
        queue[tail+1].b = y;
        map[x][y] = '*';
        tail++;
    }   
    
    void DFS(int prea, int preb)
    {
        int n;
        int i, ta = prea, tb = preb;
        initQueue(ta, tb);
        //printf("%d %d/n", queue[head].a, queue[head].b);
        count = 1;
        while(head <= tail)
        {
            for(i=0; i<4; i++)
            {
                ta = queue[head].a+direct[i][0];
                tb = queue[head].b+direct[i][1];
                //printf("ta=%d+%d,tb=%d+%d/n", queue[head].a, direct[i][0],queue[head].b, direct[i][1]);
                //printf("ta=%d tb=%d/n", ta, tb);
                if(ta>=0 && ta<height && tb>=0 && tb<len)
                {
                    //printf("%c/n", map[ta][tb]);
                    if(map[ta][tb] == '.')
                    {
                        push(ta, tb);
                        count++;
                    }    
                }
                //printf("%d %d %d/n", i, queue[head].a, queue[head].b);    
            }
            //scanf("%d", &n);
            head++;    
        } 
        printf("%d/n", count);   
    }    
    
    int main()
    {
        int i, j;
        int pa, pb;
        while(scanf("%d%d", &len, &height) != EOF)
        {
            //printf("%d %d/n", len, height);
            if(!len && !height)break;
            for(i=0; i<height; i++)
            {
                scanf("%s", map[i]);
                for(j=0; j<len; j++)
                {
                    if(map[i][j] == '@')
                    {
                        pa = i;
                        pb = j;
                    }
                }      
            }
            //printf("%d %d/n", pa, pb);
            DFS(pa, pb);  
        }    
        return 1;
    }     
又一道寬搜的題目,練得還可以,深搜的話是不是必須就得遞歸了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值