CF 60B BFS to be refined

Source: Internet
Author: User


The cereal guy's friend Serial Guy likes to watch soap operas. An episode are about-to-start, and he hasn ' t washed his plate yet. But he decided to at least put in under the tap is filled with water. The plate can represented by a parallelepiped k x n x m , that's, it has k layers (th E first layer is the upper one), each of which are a rectangle n x m with empty sq Uares ('. ') and obstacles (' # '). The water can only is present in the empty squares. The tap is positioned above the square ( x , y ) of the first layer, it's guarantee D that this square is empty. Every minute a cubical unit of water falls into the plate. Find out on how many minutes the Serial guy should unglue himself from the soap opera and turn the water off for it don't to Overfill the plate. That's, you should find the moment of time when the plate are absolutely full and are going to being overfilled in the next MO ment.


Note:the water fills all the area within reach (see Sample 4). Water flows in each of the 6 directions, through faces of 1x1x1 cubes.



Input


The first line contains three numbersk,N,m(1≤k,N,m≤10) which is the sizes of the plate. Then followkRectangles consisting of n lines each containing m characters '. ' or ' # ', which represents the ' layers ' of the plate in T The He order from the top to the bottom. The rectangles is separated by empty lines (see the samples). The last line contains x and y (1≤ xn, 1≤ ym column. Lines of each layer is numbered from the left to right by the integers from 1 to n, columns of each Laye R is numbered from top to bottom by the integers from 1 to M.


Output



The answer should contain a single number, showing on how many minutes the plate would be filled.



The main idea is to give a hexahedral divided into K-layer, each layer n rows m column, each small cube has '. ' (empty) with the ' # ' (barrier) state, the first layer has an empty position with a faucet, and the current flows in six directions each time (as far as the teammates say, I accept incompetence). Although the topic did not understand very clearly simple BFS is over, although this BFS is not elegant ...


#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
Struct Nod {
Int x;
Int y;
Int Z;
}
Char node [11] [11] [11]; / / layers, lines, columns
Int main ()
{
Int k, N, m;
while(~scanf("%d%d%d",&amp;k,&amp;n,&amp;m)) {
Int sum=0;
Getchar ();
Getchar ();
for(int i=1;i<=k;i++) {
for(int j=1;j<=n;j++) {
for(int p=1;p<=m;p++) {
scanf("%c",&amp;node[i][j][p]);
}
Getchar ();
}
Getchar ();
}
queue<Nod> q;
int sx,sy,nx,ny,nz;
scanf("%d%d",&amp;sx,&amp;sy);
Nod nod;
Nod.x=sx;
Nod.y=sy;
Nod.z=1;
Q.push (NOD);
Sum++;
node[1][sx][sy]=‘#‘;
while(!q.empty()) {
nx=q.front().x;
ny=q.front().y;
nz=q.front().z;
Q.pop ();
if(ny+1<=m &amp;&amp; node[nz][nx][ny+1]==‘.‘) {
nod.x=nx;nod.y=ny+1;nod.z=nz;
Q.push (NOD);
Sum++;
node[nz][nx][ny+1]=‘#‘;
}
if(ny-1>=1 &amp;&amp; node[nz][nx][ny-1]==‘.‘) {
nod.x=nx;nod.y=ny-1;nod.z=nz;
Q.push (NOD);
Sum++;
node[nz][nx][ny-1]=‘#‘;
}
if(nx+1<=n &amp;&amp; node[nz][nx+1][ny]==‘.‘) {
nod.x=nx+1;nod.y=ny;nod.z=nz;
Q.push (NOD);
Sum++;
node[nz][nx+1][ny]=‘#‘;
}
if(nx-1>=1 &amp;&amp; node[nz][nx-1][ny]==‘.‘) {
nod.x=nx-1;nod.y=ny;nod.z=nz;
Q.push (NOD);
Sum++;
node[nz][nx-1][ny]=‘#‘;
}
if(nz-1>=1 &amp;&amp; node[nz-1][nx][ny]==‘.‘) {
nod.x=nx;nod.y=ny;nod.z=nz-1;
Q.push (NOD);
Sum++;
node[nz-1][nx][ny]=‘#‘;
}
if(nz+1<=k &amp;&amp; node[nz+1][nx][ny]==‘.‘) {
nod.x=nx;nod.y=ny;nod.z=nz+1;
Q.push (NOD);
Sum++;
node[nz+1][nx][ny]=‘#‘;
}
}
cout<<sum<<endl;
}
Return 0;
} 


Thigh said too young to write so not elegant, to change very tired ... It's true that for the first time, one more +1wa has been written on a single statement. Waiting for the perfect



CF 60B BFS to be refined


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.