模拟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. 【转】adb uninstall卸载apk文件说明

    昨天在使用adb卸载程序,结果死活卸载不了.我输入的命令和系统提示如下: [plain] view plaincopy   arthur@arthur-laptop:~$ adb uninstall  ...

  2. 【转】周末班LR笔记总结—新手入门必备

    本来想上传文件的,上传半天没反应,只有这样了,图片不知道能显示不. 上午 学到2012.1.13 七天课 第一天(入门)二.三.四天(VUGEN脚本) 五天(Controller)六天(Analyse ...

  3. [MSDN] 使用 SharePoint 2013 中的 JavaScript 库代码完成基本操作

    MSDN:http://msdn.microsoft.com/zh-cn/library/jj163201.aspx 了解如何编写代码以在 SharePoint 2013 中使用 JavaScript ...

  4. 多个html编辑器在同一页面加载

    http://127.0.0.1:3750/test.aspx 下载:ckfinder,ckeditor编辑器 <script type="text/javascript" ...

  5. mybatis 无法转换为内部表示 解决

    出现这个问题,一般是JavaBean定义的时候数据类型不准造成的. 其中javabean定义的字段的类型并不需要完全和数据库中的字段一样. 在mapper.xml中可能做了转换  比如 CASE WH ...

  6. Microsoft Word 的键盘快捷方式

    Microsoft Word 的键盘快捷方式 全部显示 全部隐藏 本帮助文章中描述的键盘快捷方式适用于美式键盘布局.其他键盘布局的键可能与美式键盘上的键 不完全对应. 注释   本文不介绍如何为宏或自 ...

  7. Merge Into example

    merge into users a using temp_users b on (a.userid = b.user_id) when matched then update set a.passw ...

  8. ###学习《C++ Primer》- 4

    点击查看Evernote原文. #@author: gr #@date: 2014-10-16 #@email: forgerui@gmail.com Part 4: STL关联容器(第11章) 一. ...

  9. iOS9开发之新增通知行为详解

    苹果在iOS8发布时,收到短信时可以直接在通知栏输入文字并回复,非常炫酷,然而这一功能并未真正开放给开发者.iOS9新增了用户通知行为UIUserNotificationActionBehaviorT ...

  10. android 电话拨号器

    电话拨号器(重点)            1.产品经理: 需求分析文档,设计原型图    2.UI工程师: 设计UI界面    3.架构师: 写架构,接口文档    4.码农: 服务端,客户端     ...