模拟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. PHP输入流php://input介绍

    在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组.所以,这里主要探讨php输入流php://input 对一php://inpu ...

  2. Spring引用测试

    上下文 using System; using Spring.Core; using Spring.Aop; using System; using Spring.Core; using Spring ...

  3. casperjs 抓取爱奇艺高清视频

    CasperJS 是一个开源的导航脚本和测试工具,使用 JavaScript 基于 PhantomJS 编写,用于测试 Web 应用功能,Phantom JS是一个服务器端的 JavaScript A ...

  4. 20160503-spring入门1

    一.Spring是什么 Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发. IOC 控制反转  publ ...

  5. shell中if判断一个变量为空

    1.最直接简单的判断 [ ! $a ] && echo "a is null" 不用那些if语句了,直接缩短代码量. 2. 变量通过" "引号引 ...

  6. SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务

      一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32s ...

  7. 第十八篇、keychain保存UUID(保持唯一性,应用卸载,下载安装也不变)和获取IP地址

    .h #import <Foundation/Foundation.h> #import <Security/Security.h> /**使用**/ //-(void) se ...

  8. Java I/O继承图

    Reader/Writer继承关系图 RandomAccess继承关系图

  9. 九度OJ 1505 两个链表的第一个公共结点 【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1505 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例. 对于每个测试案例, ...

  10. OpenJudge/Poj 1844 Sum

    1.链接地址: http://bailian.openjudge.cn/practice/1844 http://poj.org/problem?id=1844 2.题目: Sum Time Limi ...