codeforces 60B bfs
题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满。
思路:从水龙头开始bfs一遍。每步让所有能转移到的空的点入队,总步数即为答案。
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
struct Nod {
int x;
int y;
int z;
};
char node[][][]; //第几层第几行第几列
int main()
{
int k,n,m;
while(~scanf("%d%d%d",&k,&n,&m)) {
int sum=;
getchar();
getchar();
for(int i=;i<=k;i++) {
for(int j=;j<=n;j++) {
for(int p=;p<=m;p++) {
scanf("%c",&node[i][j][p]);
}
getchar();
}
getchar();
} queue<Nod> q; int sx,sy,nx,ny,nz;
scanf("%d%d",&sx,&sy);
Nod nod;
nod.x=sx;
nod.y=sy;
nod.z=;
q.push(nod);
sum++;
node[][sx][sy]='#'; while(!q.empty()) {
nx=q.front().x;
ny=q.front().y;
nz=q.front().z;
q.pop();
if(ny+<=m && node[nz][nx][ny+]=='.') {
nod.x=nx;nod.y=ny+;nod.z=nz;
q.push(nod);
sum++;
node[nz][nx][ny+]='#';
}
if(ny->= && node[nz][nx][ny-]=='.') {
nod.x=nx;nod.y=ny-;nod.z=nz;
q.push(nod);
sum++;
node[nz][nx][ny-]='#';
}
if(nx+<=n && node[nz][nx+][ny]=='.') {
nod.x=nx+;nod.y=ny;nod.z=nz;
q.push(nod);
sum++;
node[nz][nx+][ny]='#';
}
if(nx->= && node[nz][nx-][ny]=='.') {
nod.x=nx-;nod.y=ny;nod.z=nz;
q.push(nod);
sum++;
node[nz][nx-][ny]='#';
}
if(nz->= && node[nz-][nx][ny]=='.') {
nod.x=nx;nod.y=ny;nod.z=nz-;
q.push(nod);
sum++;
node[nz-][nx][ny]='#';
}
if(nz+<=k && node[nz+][nx][ny]=='.') {
nod.x=nx;nod.y=ny;nod.z=nz+;
q.push(nod);
sum++;
node[nz+][nx][ny]='#';
}
}
cout<<sum<<endl;
}
return ;
}
大腿说太年轻写的如此不优雅,debug很累
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
struct Nod {
int x;
int y;
int z;
};
char node[][][]; //第几层第几行第几列 int xx[]={,,,,-,};
int yy[]={,,-,,,};
int zz[]={,-,,,,};
int main()
{
int k,n,m;
while(~scanf("%d%d%d",&k,&n,&m)) {
int sum=;
getchar();
getchar();
for(int i=;i<=k;i++) {
for(int j=;j<=n;j++) {
for(int p=;p<=m;p++) {
scanf("%c",&node[i][j][p]);
}
getchar();
}
getchar();
} queue<Nod> q; int sx,sy,nx,ny,nz;
scanf("%d%d",&sx,&sy);
Nod nod;
nod.x=sx;
nod.y=sy;
nod.z=;
q.push(nod);
sum++;
node[][sx][sy]='#'; while(!q.empty()) {
int nx=q.front().x;
int ny=q.front().y;
int nz=q.front().z;
q.pop();
for(int i=;i<;i++) {
int X=nx+xx[i];
int Y=ny+yy[i];
int Z=nz+zz[i]; if(X>= && X<=n && Y>= && Y<=m && Z>= && Z<=k) {
if(node[Z][X][Y]=='.') {
nod.x=X;
nod.y=Y;
nod.z=Z;
q.push(nod);
sum++;
node[Z][X][Y]='#';
}
}
}
}
cout<<sum<<endl;
}
return ;
}
小小优雅后
codeforces 60B bfs的更多相关文章
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
- Three Pieces CodeForces - 1065D (BFS)
链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...
- Fair CodeForces - 987D (bfs)
链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- Board Game CodeForces - 605D (BFS)
大意: 给定$n$张卡$(a_i,b_i,c_i,d_i)$, 初始坐标$(0,0)$. 假设当前在$(x,y)$, 若$x\ge a_i,y\ge b_i$, 则可以使用第$i$张卡, 使用后达到坐 ...
- Codeforces 1105D (BFS)
题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
随机推荐
- 读书报告之《改动代码的艺术》 (I)
<改动代码的艺术>,英文名<Working Effectively with Legacy Code>,中文翻译的文笔上绝对谈不上"艺术"二字.愧对艺术二字 ...
- Hibernate二次学习一----------搭建Hibernate
目录 1. 项目结构 1.2 hibernate.cfg.xml 1.3 entity 1.4 entity.hbm.xml 2. 测试 3. 总结 © 版权声明:本文为博主原创文章,转载请注明出处 ...
- linux系统下实时监控进程以及定位杀死挂起的进程
一.实时监测进程 [root@instance-3lm099to ~]# top top - :: up days, min, users, load average: 0.01, 0.05, 0.0 ...
- Python读取文件文件夹并检索
import os import os.path f=open("Shouldlist.txt") ShouldList=[] while 1: line =f.readline( ...
- codeforces #364c They Are Everywhere 尺取法
C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- MIC中函数和变量的声明
c++/c使用 __declspec(target(mic))函数或变量声明 或 __attribute__((target(mic)))函数或变量声明 举例如下: __attribute__((ta ...
- ASIHTTPRequest-Cookie的使用
本文转载至 http://www.cocoachina.com/bbs/read.php?tid=93220&page=e&#a 持久化cookie ASIHTTPReques ...
- php修改密码
为了让页面更为好看一些,我一般会选择bootstrap,写起来虽然看着麻烦,但是我们真正需要的只有中间的内容 下面是html的内容 <div id="tbx"" ...
- POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- iOS和Android后台机制对比
转自:http://blog.csdn.net/zsch591488385/article/details/27232881 一.iOS的“伪后台”程序 首先,先了解一下ios 中所谓的「后台进程」到 ...