CLOCK - Chỉnh đồng hồ

Tác giả: khuc_tuan

Ngôn ngữ: C++

#include "iostream"
#include "stdio.h"
#include "string"

using namespace std;

int a[3][3];
int ht, result;
int bit[9] = { 
	432, 448, 216, 292, 186, 73, 54, 7, 27
};

void duyet(int vt) {
	if(ht>result) return;
	if(vt==9) {
		bool ok = true;
		for(int i=0;i<3;++i) for(int j=0;j<3;++j) if(a[i][j]!=0) {
			ok = false;
			break;
		}
		if(ok) result = ht;
		return;
	}
	for(int sl=0;sl<4;++sl) {
		for(int i=0;i<3;++i) for(int j=0;j<3;++j) if( (bit[vt] & (1<<(i*3+j))) !=0  ) {
			a[i][j] = (a[i][j]+sl) % 4;
		}
		ht += sl;
		duyet(vt+1);
		ht -= sl;
		for(int i=0;i<3;++i) for(int j=0;j<3;++j) if( (bit[vt] & (1<<(i*3+j))) !=0  ) {
			a[i][j] = (a[i][j]+4-sl) % 4;
		}
	}
}

int main() {
	for(int i=0;i<3;++i) {
		string s;
		getline(cin,s);
		for(int j=0;j<3;++j) a[i][j] = s[j] - '0';
	}
	ht = 0;
	result = 100000;
	duyet(0);
	printf("%d\n",result);
	return 0;
}

Download