您的位置:首页 > 编程语言

HDU 4527 小明系列故事——玩转十滴水 2013腾讯编程马拉松初赛第五场第二题

2013-03-26 22:53 381 查看
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4527
 

这个和我们玩的3366游戏十滴水差不多,规则就是当水滴大于4的时候会爆裂像4个方向各传递一滴水。

有一点要注意的是,水滴的速度是相同的,所以可能有大于等于2滴水滴同时到达某一地方。

 

 

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;

/*
freopen("input.txt",  "r", stdin);  //读数据
freopen("output.txt", "w", stdout); //注释掉此句则输出到控制台
*/

int xh[7][7],t[4][2]={1,0,-1,0,0,1,0,-1};//方向
int visit[7][7];//记录时间

struct xiaohao
{
int x,y,t,d;//x,y坐标,t代表时间,d代表方向
}w,e;

void dfs(int x,int y)
{
int i;
queue<xiaohao> q;
xh[x][y]++;
if(xh[x][y]>4)
{
visit[x][y]=0;
w.x=x;w.y=y;w.t=0;w.d=-1;
q.push(w);
}
while(!q.empty())
{
w=q.front();
q.pop();
if(w.d==-1)//w.d等与-1表示爆裂,其他表示分裂方向
xh[w.x][w.y]=0;
for(i=0;i<4;i++)
{
if(w.d!=-1&&w.d!=i)//如果不是爆裂也不是沿着原方向,则continue
continue;
int xx=w.x+t[i][0],yy=w.y+t[i][1];
if(xx>0&&xx<=6&&yy>0&&yy<=6)
{
if(xh[xx][yy]==0)
{
e.x=xx;e.y=yy;e.t=w.t+1;e.d=i;
q.push(e);
}
else if(xh[xx][yy]<4)//小于4,直接++
{
xh[xx][yy]++;
}
else if(xh[xx][yy]==4)//这个地方只能写等于4,因为有同时到达可能大于四
{
xh[xx][yy]++;
visit[xx][yy]=w.t+1;
e.x=xx;e.y=yy;e.t=w.t+1;e.d=-1;
q.push(e);
}
else
{
//如果快于或等于上一次到达,则++
if(w.t+1<=visit[xx][yy])
xh[xx][yy]++;
//比上一次慢那么继续前进
else
{
e.x=xx;e.y=yy;e.t=w.t+1;e.d=i;
q.push(e);
}
}
}
}
}
}

int main()
{
int i,j,x,y,m;
while(cin>>xh[1][1])
{
for(i=1;i<=6;i++)
for(j=1;j<=6;j++)
if(i!=1||j!=1)
scanf("%d",&xh[i][j]);
cin>>m;
while(m--)
{
memset(visit,-1,sizeof(visit));
scanf("%d%d",&x,&y);
dfs(x,y);
}
for(i=1;i<=6;i++)
printf("%d %d %d %d %d %d\n",xh[i][1],xh[i][2],xh[i][3],xh[i][4],xh[i][5],xh[i][6]);
printf("\n");
}
return 520;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐