|
The C++ SourceCode:
#include <iostream>
#include <memory.h>
#include <cstdlib>
#include <typeinfo.h>
using namespace std;
#define SIZE 5
#define ROTATE 3
template <class T>
void click(T &data, const int i, const int j, const int count){
data[j] = (data[j] + count) % ROTATE;
if (j > 0) data[j - 1] = (data[j - 1] + count) % ROTATE;
if (j < SIZE - 1) data[j + 1] = (data[j + 1] + count) % ROTATE;
if (i < SIZE - 1) data[i + 1][j] = (data[i + 1][j] + count) % ROTATE;
}
template <class T>
bool check(T &data){
for (int i = 1; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
click(data, i, j, ROTATE - data[j]);
for (int j = 0; j < SIZE; j++)
if (data[SIZE - 1][j] != 0)
return false;
return true;
}
int main(){
int status[SIZE][SIZE];
int test[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
cin>>status[j];
int searchSum = 1;
for (int i = 0; i < SIZE; i++) searchSum *= ROTATE;
int temp;
for (int i = 0; i < searchSum; i++){
memcpy(test, status, sizeof(status));
temp = i;
for (int j = 0; j < SIZE; j++){
click(test, 0, j, temp % ROTATE);
temp /= ROTATE;
}
if (check(test)){
cout<<"The answer is:"<<endl;
temp = i;
for (int j = 0; j < SIZE; j++){
cout<<(temp % ROTATE)<<" ";
temp /= ROTATE;
}
cout<<endl;
break;
}
}
system("pause");
return 0;
}
Input is a 5 * 5Matrix
0 Means Blue
1 Means Green
2 Means Red
Such as
0 1 2 0 0
1 2 0 0 1
1 1 1 1 1
2 2 2 2 2
0 0 0 0 0
Output is a 1 * 5Vector, Means the number to click in the first row.
Such as
2 1 0 0 0
Means the top left need to click twice
then is once
The rows below first row is related with the row just up them.
|