模拟BFS搜索

对于一个将要爆炸的点,可能同时由多个点引起。

 /*
模拟搜索过程
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = ;
int mat[ maxn ][ maxn ];
int mytime[ maxn ][ maxn ];
const int dx[] = {,,-,};
const int dy[] = {,-,,};
struct node{
int x,y,ti,flag;
};
void solve( int x,int y ){
queue<node>q;
node cur,nxt;
cur.x = x;
cur.y = y;
cur.ti = ;
cur.flag = -;
mytime[x][y] = ;
mat[cur.x][cur.y]++;
q.push( cur ); while( !q.empty() ){
cur=q.front();
q.pop();
if( cur.flag==- )
mat[cur.x][cur.y] = ;
for( int i=;i<;i++ ){
nxt.x = cur.x+dx[i];
nxt.y = cur.y+dy[i];
nxt.ti = cur.ti+;
if( cur.flag!=-&&cur.flag!=i ) continue;
if( nxt.x<||nxt.x>||nxt.y<||nxt.y> ) continue;
if( mat[nxt.x][nxt.y]== ){
nxt.flag = i;
q.push( nxt );
}
else if( mat[nxt.x][nxt.y]>=&&mat[nxt.x][nxt.y]<= ){
mat[nxt.x][nxt.y]++;
}
else if( mat[nxt.x][nxt.y]== ){
nxt.flag = -;
q.push( nxt );
mat[ nxt.x ][ nxt.y ]++;
mytime[nxt.x][nxt.y] = nxt.ti;
}
else if( mat[nxt.x][nxt.y]> ){
if( nxt.ti<=mytime[nxt.x][nxt.y] ){
mat[nxt.x][nxt.y]++;
}
else {
nxt.flag = i;
q.push( nxt );
}
}
}
}
return ;
} int main(){
while( scanf("%d",&mat[ ][ ])== ){
for( int i=;i<=;i++ )
scanf("%d",&mat[ ][ i ]);
for( int i=;i<=;i++ )
for( int j=;j<=;j++ )
scanf("%d",&mat[ i ][ j ]);
int m;
int x,y;
scanf("%d",&m);
while( m-- ){
scanf("%d%d",&x,&y);
if( mat[ x ][ y ]<= ){
mat[ x ][ y ] ++;
continue;
}
memset( mytime,-,sizeof( mytime ) );
solve( x,y );
}
for( int i=;i<=;i++ ){
for( int j=;j<=;j++ ){
if( j== ) printf("%d",mat[ i ][ j ]);
else printf(" %d",mat[ i ][ j ]);
}
printf("\n");
}
printf("\n");
}
return ;
}

HDU4527+BFS的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. jQuery-对Select的操作

    语法解释: $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkTex ...

  2. Floyd-Warshall算法详解(转)

    Floyd-Warshall算法,简称Floyd算法,用于求解任意两点间的最短距离,时间复杂度为O(n^3).我们平时所见的Floyd算法的一般形式如下: void Floyd(){ int i,j, ...

  3. Android沉浸式状态栏实现

    Step1:状态栏与导航栏半透明化 方法一:继承主题特定主题 在Android API 19以上可以使用****.TranslucentDecor***有关的主题,自带相应半透明效果 例如: < ...

  4. powerdesigner 使用的几点问题

    一.powerdesigner 没有DataBase?: powerdesigner 只有在选择物理模型PDM的时候才会出现数据库菜单. 二.PowerDesigner版本控制功能? 1.首先介绍一下 ...

  5. 关于修改tabbar的颜色的问题

    首先,项目是在故事板中搭建的,所以遇到这个问题的时候,首先是想到在故事板中找到相关的属性,确实是有一个Selected Image,但是设置了这个图片以后,运行的效果是,点击选择后,本身的image就 ...

  6. 12天学好C语言——记录我的C语言学习之路(Day 6)

    12天学好C语言--记录我的C语言学习之路 Day 6: 今天,我们要开始学习数组了. //①数组部分,数组的大小不能够动态定义.如下: //int n;   scanf("%d,& ...

  7. 【转】SQL删除重复数据方法

    例如: id           name         value 1               a                 pp 2               a           ...

  8. Qt-获取主机网络信息之QHostInfo

    Qt中提供了几个用于获取主机网络信息的类,包括QHostInfo.QHostAddress.QNetworkInterface以及QNetworkAddress.在本节中,我将在这里总结QHostIn ...

  9. mysql---union和左连接的两倒面试题

    第一道: 思路:无非是将hid与gid与t表中的tname关联起来.实质上是三表关联(m,t,t) 先将hid与tname关联起来,运用左连接 再将结果集与t表中的tname关联起来,使得gid与tn ...

  10. jQuery 1.4.4 中 function( window, undefined ) 写法原因

    读 jQuery 1.4.4 版本代码的时候,发现下面的写法: (function( window, undefined ) { ... // code goes here })(window); w ...