60B - Serial Time - CodeForces Solution


dfs and similar dsu *1400

Please click on ads to support us..

Python Code:

z, n, m = list(map(int, input().split()))

N = 12
adj = [[['' for _ in range(N)] for _ in range(N)] for _ in range(N)]
visited = [[[False for _ in range(N)] for _ in range(N)] for _ in range(N)]

for i in range(1, z+1):
  input()
  for j in range(1, n+1):
    chars = input()
    for k in range(1, m+1):
      adj[i][j][k] = chars[k-1]

input()
x, y = list(map(int, input().split()))

dx = [ 1,-1,0,0,0,0 ]
dy = [ 0,0,1,-1,0,0 ]
dz = [ 0,0,0,0,1,-1 ]

def dfs(l, x, y):
  if adj[l][x][y] == '#':
    return
  
  visited[l][x][y] = True
  for i in range(6):
    a = x + dx[i]
    b = y + dy[i]
    c = l + dz[i]

    if a > 0 and b > 0 and c > 0 and c <= z and b <= m and a <= n and not visited[c][a][b]:
      dfs(c, a, b)

dfs(1, x, y)

ans = 0
for i in range(1, z+1):
  for j in range(1, n+1):
    for k in range(1, m+1):
      if visited[i][j][k]:
        ans += 1

print(ans)

C++ Code:

#include<bits/stdc++.h> 
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,j,a,b) rep(i,0,a-1)rep(j,0,b-1)
#define rep3(i,j,k,a,b,c) rep2(i,j,a,b)rep(k,0,c-1)
int a,b,c,m,n,ans;
int vis[15][15][15];
char s[15][15][15];
int dx[]={-1,1,0,0,0,0};
int dy[]={0,0,-1,1,0,0};
int dz[]={0,0,0,0,-1,1};
struct M{int x,y,z;}p[15][15][15];
queue<M>q;
bool check(int x,int y,int z){return x>=0&y>=0&z>=0&x<a&y<b&z<c;}
int main(){
	cin>>a>>b>>c;
	rep2(i,j,a,b)cin>>s[i][j];
	cin>>m>>n;
	q.push({0,m-1,n-1});
	while(!q.empty()){
		M k=q.front();
//		cout<<k.x<<k.y<<k.z<<endl;
		q.pop();
		vis[k.x][k.y][k.z]=1;
		rep(i,0,5)
			if(check(k.x+dx[i],k.y+dy[i],k.z+dz[i]))
				if(s[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]]=='.')
					if(!vis[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]])
						q.push({k.x+dx[i],k.y+dy[i],k.z+dz[i]}),
						vis[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]]=1;
	}
	rep3(i,j,k,a,b,c)ans+=vis[i][j][k];
	cout<<ans;
	return 0;
}
			 	  	 	  	     				 					 	


Comments

Submit
0 Comments
More Questions

844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction