Problem: Example

Input

2 2 2 4
0 0 0
1 1 0
1 0 1
0 1 1


Output

4

Solution (in CPP):

Solution:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

struct point {
	int x,y,z;
	point() {}
	point(int x,int y,int z):x(x),y(y),z(z){}
};
vector<point> vs;
int a,b,c,n,x,y,z;
map<point,int> pt,del;
bool operator < (const point &a,const point &b) {
	return a.x<b.x||(a.x==b.x&&a.y<b.y)||(a.x==b.x&&a.y==b.y&&a.z<b.z);
}
bool valid(point p) {
	return p.x>=0&&p.x<a&&p.y>=0&&p.y<b&&p.z>=0&&p.z<c;
}
void dfs(point p) {
	vs.pb(p); del[p]=1;
	rep(dx,-1,2) rep(dy,-1,2) rep(dz,-1,2) {
		point q(p.x+dx,p.y+dy,p.z+dz);
		if (del.count(q)&&del[q]==0) dfs(q);
		if (valid(q)&&!del.count(q)) pt[q]=0;
	}
}
void dfs2(point p) {
	pt[p]=1;
	rep(dx,-1,2) rep(dy,-1,2) rep(dz,-1,2) if (abs(dx)+abs(dy)+abs(dz)==1) {
		point q(p.x+dx,p.y+dy,p.z+dz);
		if (pt.count(q)&&pt[q]==0) dfs2(q);
	}
}

int main() {
	scanf("%d%d%d%d",&a,&b,&c,&n);
	rep(i,0,n) {
		scanf("%d%d%d",&x,&y,&z);
		del[point(x,y,z)]=0;
	}
	vector<point> d;
	for (auto p:del) d.pb(p.fi);
	int ret=1;
	for (auto p:d) if (del[p]==0) {
		pt.clear();
		dfs(p);
		ret--;
		vector<point> vs;
		for (auto q:pt) vs.pb(q.fi);
		for (auto q:vs) if (pt[q]==0) {
			dfs2(q);
			ret++;
		}
	}
	printf("%d\n",ret);
}