现在的位置: 首页 > 综合 > 正文

POJ1222

2018年04月05日 ⁄ 综合 ⁄ 共 866字 ⁄ 字号 评论关闭

熄灯问题,枚举问题

只需枚举第一行

#include "iostream"
using namespace std;

const int N = 64;

int Bubble[7][8];
int Action[7][8];

bool GetAnswer(void){
	for(int Row = 1 ; Row != 5 ; Row ++){
		for (int Col = 1 ; Col != 7 ; Col ++){
			Action[Row + 1][Col] = ( Action[Row][Col] + Action[Row][Col - 1] + Action[Row - 1][Col] + 
				Action[Row][Col + 1] + Bubble[Row][Col] ) % 2;
		}
	}
	for ( int Col = 1 ; Col != 7 ; Col ++ ){
		if ( ( Action[5][Col] + Action[5][Col - 1] + Action[4][Col] + 
			Action[5][Col + 1] + Bubble[5][Col] ) % 2 != 0 ){
				return false;
		}
	}
	return true;
}

void SetFirstLine(void){
	for (int i = 1 ; i != 7 ; i ++){
		Action[1][i] = 0;
	}
	for (int i = 0 ; i != N ; i ++ ){
		int Test = i;
		int Bita = 1;
		while(Test != 0){
			Action[1][Bita ++] = Test % 2;
			Test /= 2;
		}
		if(GetAnswer() == true)break;
	}
}

int main(void){
	int Case,Count = 0;
	cin >> Case;
	while(Case --){
		for ( int i = 1 ; i != 6 ; i ++ ){
			for ( int j = 1 ; j != 7 ; j ++ ){
				cin >> Bubble[i][j];
			}
		}
		SetFirstLine();
		Count ++;
		cout << "PUZZLE #" << Count << endl;
		for ( int i = 1 ; i != 6 ; i ++ ){
			for ( int j = 1 ; j != 7 ; j ++ ){
				cout << Action[i][j] << " ";
			}
			cout << endl;
		}
	}
	return 0;
}

 

【上篇】
【下篇】

抱歉!评论已关闭.