All pastes #793462 Raw Edit

clocks

public text v1 · immutable
·published 2007-11-22 08:38 UTC
rendered paste body

/*
ID: yian.sh1
LANG: C++
TASK: clocks
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int clocks[9];
int turned[9];
int possibilities[36];
vector<int*> answers;

const int moves[10][5]  = {{-1,-1,-1,-1,-1}, {0,1,3,4,-1},{0,1,2,-1,-1},
{1,2,4,5,-1},{0,3,6,-1,-1},{1,3,4,5,7},{2,5,8,-1,-1},
{3,4,6,7,-1},{6,7,8,-1,-1},{4,5,7,8,-1}};

int ans;

bool works(){
    for(int i=0; i<9; i++){
        if(clocks[i]!=12)
            return false;
    }
    return true;
}

void print() {
     for(int i=0; i<9; i++) {
          if(i%3 == 0)
               cout << "\n";
          cout << clocks[i] << " ";
     }
}

void check(){
    if(works()==true) {
        ans = history;
        cout << "ans: " << ans;
        return;
    }
    if(height == 9) {
		return;
	}
	print();
	
	//does the move on the clock
    for(int i=0; i<5; i++){
        int m = moves[state][i];
		if(m>=0) {
		    if(turned[m]<4) {
				if(clocks[m] == 12)
            		clocks[m] = 3;
  				else
      				clocks[m] += 3;
          		turned[m]++;
            } else {
				return;
            }
		}
	}
	
	cout << "history (0): " << history;
	history = history*10+state;
	cout << "history: " << history;
	cout << " state: " << state;
	
	for(int i=1; i<=9; i++){
		check(height+1, i, history);
	}
}

/*void check(int height, int history){
    if(works()==true) {
		ans = history;
        cout << "ans: " << ans;
        return;
    }
    if(height == 9) {
        cout << "hisotry: " << history;
    	return;
    }
	
	history++;
	
	for(int move=0; move<9; move++){
        /*for(int i=0; i<5; i++){
            int m = moves[move][i];
    		if(m>=0) {
    		    if(turned[m]<3) {
    				if(clocks[m] == 12)
                		clocks[m] = 3;
      				else
          				clocks[m] += 3;
              		turned[m]++;
                } else {
    				break;
                }
    		}
    	}
    	//cout << "\n\nmove: " << move;
    	//cout << " height: " << height;
	    //print();
       // history = history*10+move;
        //cout << move;
		check(height+1, history);
    }

}*/

int main() {
    FILE *fin  = fopen ("clocks.in", "r");
    FILE *fout = fopen ("clocks.out", "w");

    for(int i=0; i<9; i++) {
        fscanf (fin, "%d", &clocks[i]);
    }
	
	for(int i=1; i<=9; i++)
		check(0, i, 0);
	
	cout << "size: " << answers.size();
	
    for(int i=0; i<answers.size(); i++) {
        int* ans;
        ans = answers[i];
        while(*ans)
            fprintf (fout, "%d\n", *ans++);
    }
    
    return 0;
}