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

2625

被折叠的 条评论
为什么被折叠?



