HDU4527+BFS
模拟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的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- PeopleReady 是什么系统?其中文为全员就绪
PeopleReady 中文:全员就绪. 顾名思义: 一句话:PeopleReady 就是能使人快速Ready的系统. 稍长一句话:就是能使大家快速Ready(经验+知识)的系统. 详细的来说:通过积 ...
- PHP之curl
当我第一次接触curl的时候,看文档,以及网上search各种资料,官方(http://cn2.php.net/manual/en/intro.curl.php)的解释是,这是某大牛写的一个libcu ...
- 【转】Oracle - 数据库的实例、表空间、用户、表之间关系
[转]Oracle - 数据库的实例.表空间.用户.表之间关系 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机 ...
- JAXB - XML Schema Types, Date and Time
JAXB binds all three of the schema types xsd:date, xsd:time and xsd:dateTime to XMLGregorianCalendar ...
- R cannot be resolved to a variable 解决办法
Android开发过程中,碰到R cannot be resolved to a variable的报错信息,好像没有很确定的错误原因,一般来说,我总结出几个可能的解决方法,希望试过以后管用... 1 ...
- 12天学好C语言——记录我的C语言学习之路(Day 10)
12天学好C语言--记录我的C语言学习之路 Day 10: 接着昨天的指针部分学习,有这么一个题目: //还是四个学生,四门成绩,只要有学生一门功课没及格就输出这个学生的所有成绩 /*//progra ...
- 9月18日,SQL学习基础1
数据库管理和应用 Oltp是小型的管理,OLAP是大型的管理 开发的内容如触发器 数据库管理系统(Database Management System,简称为DBMS)是位于用户与操作系统之间的一层数 ...
- 【转】querystring传递中文出现乱码的问题
原帖地址:http://www.cnblogs.com/Fly-sky/archive/2009/04/22/1441015.html 现象:近期项目中用到查询字符串传值,如果传递的是英文一切正常,但 ...
- 用 CSS 隐藏页面元素的 5 种方法
原文链接:用 CSS 隐藏页面元素的 5 种方法,转载请注明来源! 用 CSS 隐藏页面元素有许多种方法.你可以将 opacity 设为 0.将 visibility 设为 hidden.将 disp ...
- sea.js总结
SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架. 参考以下网址进行详细学习: https://segmentfault.com/a/1190000000357191?pag ...