Hdu 4527 小明系列故事——玩转十滴水

本文介绍了一种解决HDU 4527问题的方法,该问题是腾讯马拉松初赛第六场的第三题。针对两个飞溅小水滴聚集在等级为4的方格上时的特殊情况,采用了一种改进的广度优先搜索(BFS)策略来确保状态更新的同步性。

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4527

腾讯马拉松初赛第六场第三题。

搜索题。

需要特别注意的一点是两个飞溅的小水滴聚集在等级为4的方格上的情况,这种情况实际应该是先变成6,然后爆裂飞溅,其本身变成等级0,即本身应该是同步的。

但是如果用一般思维的BFS,因为出队列毕竟有先后,无法达到实际的同步,因为一般的思维,我们总是使一个方向走的尽可能的远,就是飞溅的小水滴的一个方向要持续走下去。就像这道题的处理方式一样:http://blog.csdn.net/niuox/article/details/8684100

对于本题,可以一个单位一个单位走,在队列的结构体中加入方向的属性,这样既可。对于交叉的情况,如果dist相等,说明实际情况是叠加的,特判即可。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;

struct Point
{
    int x;
    int y;
    int direc;//方向属性
};

int disx[4] = {-1,0,1,0};
int disy[4] = {0,1,0,-1};
int dist[10][10];
int map[10][10];
int startx,starty;

void bfs()
{
    queue<Point> q;
    Point temp,nex,s;
    s.x = startx;
    s.y = starty;
    s.direc = -1;//代表四个方向全有
    q.push(s);
    map[s.x][s.y] = 0;
    dist[s.x][s.y] = 0;
    while(!q.empty())
    {
        temp = q.front();
        if(temp.direc == -1)
        {
            map[temp.x][temp.y] = 0;
        }
        q.pop();
        for(int i=0; i<4; i++)
        {
            if(!(temp.direc == -1 || temp.direc == i))
            {
                continue;
            }
            int tempx = temp.x + disx[i];
            int tempy = temp.y + disy[i];

            if(tempx>=1 && tempx<=6 && tempy>=1 && tempy<=6)
            {
                nex.x = tempx;
                nex.y = tempy;
                if(map[nex.x][nex.y] == 0)
                {
                    dist[nex.x][nex.y] = dist[temp.x][temp.y] + 1;
                    nex.direc = i;
                    q.push(nex);
                }
                else if(map[nex.x][nex.y]>=1 && map[nex.x][nex.y]!=4)
                {
                    dist[nex.x][nex.y] = dist[temp.x][temp.y] + 1;
                    map[nex.x][nex.y]++;
                }
                else if(map[nex.x][nex.y] == 4)
                {
                    nex.direc = -1;
                    q.push(nex);
                    map[nex.x][nex.y]++;
                    dist[nex.x][nex.y] = dist[temp.x][temp.y] + 1;
                }
                //相交
                else if(dist[nex.x][nex.y] == dist[temp.x][temp.y] + 1)
                {
                    map[nex.x][nex.y]++;
                }
            }
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    while(scanf(" %d %d %d %d %d %d",&map[1][1],&map[1][2],&map[1][3],&map[1][4],&map[1][5],&map[1][6])!=EOF)
    {
        for(int i=2; i<=6; i++)
        {
            for(int j=1; j<=6; j++)
            {
                scanf(" %d",&map[i][j]);
            }
        }
        memset(dist,0,sizeof(dist));
        int m;
        scanf(" %d",&m);

        for(int i=1; i<=m; i++)
        {
            scanf(" %d %d",&startx,&starty);
            if(map[startx][starty]!=4)
            {
                map[startx][starty]++;
            }
            else
            {
                bfs();
            }
        }
        for(int i=1; i<=6; i++)
        {
            for(int j=1; j<=5; j++)
            {
                printf("%d ",map[i][j]);
            }
            printf("%d\n",map[i][6]);
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值