Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
3.7 kB
0
Indexable
Never
Turn Over Game
As in , there is a 4×4 sized table. In a grid of the table, there are white or black stones. When you choose a position of stone randomly, the stone and four stones adjacent to the up, down, left and right sides of the stone will turn to the opposite color like turning a white stone to a black & a black stone to a white. Let’s suppose this process as a calculation.


Using such a calculation, you want to change all the stones on the table into all whites or all blacks. Find out the minimum operation count at this time.

Time limit: 1 second (java: 2 seconds)

[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
Table info is given without blank over four rows per each test case. Colors are indicated like white for ‘w’ and black for ‘b’.

[Output]
Output the minimum operation count to change all colors as white or black on the first row per each test case. If not possible, output "impossible" .

[I/O Example]
Input
2
bwwb
bbwb
bwwb
bwww
bwbw
wwww
bbwb
bwwb

Output
Case #1 

4 

Case #2
impossible


50
wbwb
wbbw
wbww
wbww
bbwb
bbbw
wbww
bwwb
bbbb
wwbw
bwbb
bbbb
bwwb
bwww
wwbb
wbbb
wbbb
bbww
bbwb
wwbw
wbwb
bwww
bwbw
wwbw
wbww
wbww
bbbw
bwwb
wbbw
wbwb
bbbb
wbww
wbww
wbwb
wwww
bwwb
bwwb
bbww
bwww
bbbw
bwbw
wwbw
wwww
wbww
bwbb
bwww
bwbb
wwww
wbbw
bbbb
bwww
bbww
wbwb
bwbw
bwbw
bwbw
bbww
bbwb
bbbw
wwwb
bwbb
wbbw
wwww
bwbb
bbww
wbbw
bbbw
wbbw
bwbb
bbww
wbww
wbbb
bwwb
bbww
wwww
wwbw
bbbb
bwbb
wwww
bwww
bwbw
wwbw
wwbb
wwww
bwww
bwbb
bbwb
wwwb
wwww
bwbw
bbbb
wbww
bwbb
wwwb
wbww
bwwb
bwww
bbwb
wwbb
bbwb
wbww
bwww
wbww
bwwb
bwbb
wbbw
bwwb
wbww
bbbw
wwww
wwbw
wbbw
wwbw
bbbw
bwwb
wbbb
bwbw
bwbb
bbwb
bwbw
bwww
wwww
bbbw
wwbw
wbww
wbww
wbbb
wbbw
bbbb
wwww
bbbb
bwwb
wwww
wbwb
wbwb
bwwb
bbwb
bbww
bbbb
bwwb
wwww
wwbb
wbbw
wbww
bbwb
wwww
wwww
wwww
wwww
wwww
bwbb
bbww
bwbb
wwww
wbbb
bwww
bwbw
wwbb
wbww
bbbb
bwww
wbwb
wwbb
wbbb
wbww
wwbb
bbbw
wbwb
wwwb
bbbw
bwww
wwwb
bwbb
bwbb
wwww
wbwb
wbbw
bwwb
bbbw
bbww
wbwb
bwbw
wwbb
wbwb
bwbb
bbbb
wbwb
bwwb
bwbw
bwwb
bwbw
bwww
bwbw
wwww
bbwb
bwwb
bbbb
bbbb
bbbb
bbbb


#include <iostream>;
using namespace std;
int dx[5] = {0, -1, 0, 1, 0},
	dy[5] = {0, 0, 1, 0, -1};
int A[4][4];
int oden,otrang,minL,dem;
char S[4][4];

void latCo(int x, int y){
	for(int i=0;i<5;i++)
	{
		int newx=x+dx[i],
			newy=y+dy[i];
		if(newx>=0 && newx<4 && newy>=0 && newy<4)
		{
			if(S[newx][newy]=='b')
			{
				S[newx][newy]='w';
				oden--;
				otrang++;
			}else
			{
				S[newx][newy]='b';
				oden++;
				otrang--;
			}
		}
	}
}

void backTrack(int index)
{
	if(oden==16 || otrang==16){
		if(minL>dem){
			minL=dem;
		}
	}
	if (index==16)
	{
		return;
	}else
	{
			int r = index/4;
			int c = index%4;
			A[r][c]=1;
			latCo(r,c);
			dem++;
			backTrack(index+1);
			A[r][c]=0;
			latCo(r,c);
			dem--;
			backTrack(index+1);
		
	}
	
}
int main(){
	//freopen("Text.txt","r",stdin);
	int T;
	cin>>T;
	for(int tc=1;tc<=T;tc++)
	{
		otrang = 0; oden = 0;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				cin>>S[i][j];
				if(S[i][j]=='b')
				{
					oden++;
				}else
				{
					otrang++;
				}
			}
		}
		dem = 0;
		minL = 9999999;
		backTrack(0);
		cout<<"Case #"<<tc<<endl;
		if(minL == 9999999)
		{
			cout<<"impossible"<<endl;
		}else
		{
			cout<<minL<<endl;
		}
		
	}
	

	return 0;
}
Leave a Comment