Language: C++

#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>

#define MAX 12
#define NIL (-1)

using namespace std;

inline bool inRange(int t, int min, int max){
	return (min <= t && t < max);
}

class CMap{
public:
	int m[MAX][MAX];
	vector< pair<int, int > > vHist;

	CMap()
	{
		for(int i = 0; i < MAX; ++i)
			for(int j = 0; j < MAX; ++j)
				m[i][j] = 0;
	}

	void Reset()
	{
		for(int i = 0; i < MAX; ++i)
			for(int j = 0; j < MAX; ++j)
				m[i][j] = 0;
	}
	int TryPassParticle(int i, int j)
	{
		int cnt1 = 0;
		const int di[] = {0,-1,0,1,0};
		const int dj[] = {0,0,1,0,-1};

		for(int k = 0; k < sizeof(di)/sizeof(*di); ++k)
			if( inRange( i+di[k],1,MAX-1 ) && inRange( j+dj[k],1,MAX-1 ) )
				if( m[i+di[k]][j+dj[k]] == 1 )
					cnt1++;

		return cnt1;
	}
	bool PassParticle(int i, int j)
	{
		int cnt0 = 0;
		int cnt1 = 0;
		const int di[] = {0,-1,0,1,0};
		const int dj[] = {0,0,1,0,-1};

		if( !(inRange(i,1,MAX-1) && inRange(j,1,MAX-1)) )
			return false;

		if( find( vHist.begin(), vHist.end(), make_pair(i,j) ) != vHist.end() )
			return false;

		for(int k = 0; k < sizeof(di)/sizeof(*di); ++k)
			if( inRange( i+di[k],1,MAX-1 ) && inRange( j+dj[k],1,MAX-1 ) )
				if( m[i+di[k]][j+dj[k]] == 1 )
					cnt1++;
				else
					cnt0++;

		if( cnt1 < cnt0 )
			return false;

		for(int k = 0; k < sizeof(di)/sizeof(*di); ++k)
			if( inRange( i+di[k],1,MAX-1 ) && inRange( j+dj[k],1,MAX-1 ) )
				m[i+di[k]][j+dj[k]] ^= 1;

		vHist.push_back( make_pair( i, j ) );
		return true;
	}
	bool NoLight(int range = MAX - 1) const
	{
		for(int i = 1; i < range; ++i)
			for(int j = 1; j < range; ++j)
				if( m[i][j] == 1 ) return false;
		return true;
	}
	void Print() const
	{
		for(int i = 1; i < MAX - 1; ++i){
			cout << m[i][1];
			for(int j = 2; j < MAX - 1; ++j)
				cout << ' ' << m[i][j];
			cout << '\n';
		}
	}
	void PrintPassHist(){
		Reset();

		for(unsigned int i = 0; i < vHist.size(); ++i)
			m[ vHist[i].first ][ vHist[i].second ] = 1;

		for(int i = 1; i < MAX - 1; ++i){
			cout << m[i][1];
			for(int j = 2; j < MAX - 1; ++j)
				cout << ' ' << m[i][j];
			cout << '\n';
		}
	}
};

void EraseBlocks(CMap &M){
	bool bCont;
	do{
		bCont = false;
		for(unsigned int i = 1; i < MAX-1; ++i){
			for(unsigned int j = 1; j < MAX-1; ++j){
				if( M.m[i][j] == 1 ){
					int ci = NIL, cj = NIL, max1 = 0;
					const int di[] = {-1,-1,0,1,1, 1, 0,-1};
					const int dj[] = { 0, 1,1,1,0,-1,-1,-1};
					const int sz = sizeof(di)/sizeof(*di);

					for(int k = 0; k < sz; k+=2){
						if( M.m[i+di[k]][j+dj[k]] == 0 && inRange(i+di[k],1,MAX-1) && inRange(j+dj[k],1,MAX-1) &&
							M.m[i+di[(k+1)%sz]][j+dj[(k+1)%sz]] == 0 && inRange(i+di[(k+1)%sz],1,MAX-1) && inRange(j+dj[(k+1)%sz],1,MAX-1) && 
							M.m[i+di[(k+2)%sz]][j+dj[(k+2)%sz]] == 0 && inRange(i+di[(k+2)%sz],1,MAX-1) && inRange(j+dj[(k+2)%sz],1,MAX-1) &&
							M.m[i+di[(k+3)%sz]][j+dj[(k+3)%sz]] == 0 && inRange(i+di[(k+3)%sz],1,MAX-1) && inRange(j+dj[(k+3)%sz],1,MAX-1) &&
							M.m[i+di[(k+4)%sz]][j+dj[(k+4)%sz]] == 0 && inRange(i+di[(k+4)%sz],1,MAX-1) && inRange(j+dj[(k+4)%sz],1,MAX-1)){
								int d = M.TryPassParticle( i+di[(k+6)%sz],j+dj[(k+6)%sz] );
								if( d > max1 )
								{
									max1 = d;
									ci = i+di[(k+6)%sz];
									cj = j+dj[(k+6)%sz];
								}
						}
					}
					if( M.PassParticle( ci, cj ) ){
						bCont = true;
						break;
					}
				}
			}
		}
	}while( bCont );
}

void BFS(CMap &stm, CMap &ret){
	int n = 0;
	bool bFailed = true;
	queue< CMap > q;

	q.push( stm );
	while( !q.empty() ){
		bool bCont = true;
		CMap Tmp, M = q.front();
		
		q.pop();

		EraseBlocks( V, M );
		/*cout << endl;
		M.Print();*/
		
		if( M.NoLight() )
		{
			ret = M;
			bFailed = false;
			break;
		}
		Tmp = M;

		for(unsigned int i = 1; i < MAX-1 && bCont; ++i){
			for(unsigned int j = 1; j < MAX-1 && bCont; ++j){
				if( M.m[i][j] == 1 ){
					int max1 = 0;
					int ci = NIL, cj = NIL;
					const int di[] = {-1,0,1,0};
					const int dj[] = {0,1,0,-1};
					const int sz = sizeof(di)/sizeof(*di);

					for(int k = 0; k < sz; ++k){
						if( M.m[i+di[k]][j+dj[k]] == 0 && inRange(i+di[k],1,MAX-1) && inRange(j+dj[k],1,MAX-1) &&
							M.m[i+di[(k+1)%sz]][j+dj[(k+1)%sz]] == 0 && inRange(i+di[(k+1)%sz],1,MAX-1) && inRange(j+dj[(k+1)%sz],1,MAX-1)){
								if( M.PassParticle(i+di[(k+2)%sz], j+dj[(k+2)%sz]) ){
									q.push( M );
									M = Tmp;
									bCont = false;
								}
								if( M.PassParticle(i+di[(k+3)%sz], j+dj[(k+3)%sz]) ){
									q.push( M );
									M = Tmp;
									bCont = false;
								}
						}
					}
				}
			}
		}
	}
}
int main(void){
	int n;

	cin >> n;
	for( int D = 0; D < n; ++D )
	{
		CMap M;
		CMap Answer;

		for(int i = 1; i < MAX - 1; ++i)
			for(int j = 1; j < MAX - 1 ; ++j)
				cin >> M.m[i][j];

		BFS( M, Answer );
		Answer.PrintPassHist();
	}
	return 0;
}

Compiler output:
a.cc: In function 'void BFS(CMap&, CMap&)':
a.cc:157:30: error: 'V' was not declared in this scope
  157 |                 EraseBlocks( V, M );
      |                              ^