#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdint.h> #include <math.h> #include <stdlib.h> #define TRUE (1 == 1) #define FALSE (1 != 1) #define DEBUG if (FALSE) #define MAX_CASES 5 #define MAX_BOARDS 514 /* * Author: * Date: * Purpose: * Problem: */ /* * This template reads data until a terminating value is reached. */ int board[MAX_CASES][MAX_CASES]; int boards[MAX_BOARDS]; int steps[MAX_BOARDS]; int opposite[2]; void init() { /* FUNCTION init */ int i; int k; for (i=0; i<MAX_CASES; i++) { /* each row */ for (k=0; k<MAX_CASES; k++) { /* each column */ board[i][k] = 0; } /* each column */ } /* each row */ for (i=0; i<MAX_BOARDS; i++) { steps[i] = 0; } } /* FUNCTION init */ void dump() { /* FUNCTION dump */ int i; int k; printf("steps:\n"); for (i=0; 512>i; i++) { /* for */ if (0 != steps[i]) { printf("%d: step: %d \n", i, steps[i]); } } /* for */ } /* FUNCTION dump */ int boardToNum() { /* FUNCTION boardToNum */ int i; int j; int ret = 0; ret = board[1][1] + board[1][2] * 2 + board[1][3] * 4 + board[2][1] * 8 + board[2][2] * 16 + board[2][3] * 32 + board[3][1] * 64 + board[3][2] * 128 + board[3][3] * 256; return ret; } /* FUNCTION boardToNum */ void numToBoard(int num) { /* FUNCTION numToBoard */ if ( 0 == (num % 2)) { board[1][1] = 0; } else { board[1][1] = 1; } if ( 0 == ((num / 2) % 2)) { board[1][2] = 0; } else { board[1][2] = 1; } if ( 0 == ((num / 4) % 2)) { board[1][3] = 0; } else { board[1][3] = 1; } if ( 0 == ((num / 8) % 2)) { board[2][1] = 0; } else { board[2][1] = 1; } if ( 0 == ((num / 16) % 2)) { board[2][2] = 0; } else { board[2][2] = 1; } if ( 0 == ((num / 32) % 2)) { board[2][3] = 0; } else { board[2][3] = 1; } if ( 0 == ((num / 64) % 2)) { board[3][1] = 0; } else { board[3][1] = 1; } if ( 0 == ((num / 128) % 2)) { board[3][2] = 0; } else { board[3][2] = 1; } if ( 0 == ((num / 256) % 2)) { board[3][3] = 0; } else { board[3][3] = 1; } } /* FUNCTION numToBoard */ void getInput() { /* FUNCTION getInput */ int i; int k; char tmp; for (i=1; 4>i; i++) { /* each row */ for (k=1; 4>k; k++) { /* for each column */ scanf("%c", &tmp); if ('*' == tmp) { /* found a queen */ board[i][k] = 1; } /* found a queen */ } /* for each column */ scanf("%c", &tmp); } /* each row */ } /* FUNCTION getInput */ void flip(int x, int y) { /* FUNCTION flip */ board[x][y] = opposite[board[x][y]]; } /* FUNCTION flip */ void makeMove(int x, int y) { /* FUNCTION makeMove */ flip(x-1,y); flip(x,y); flip(x+1,y); flip(x,y-1); flip(x,y+1); } /* FUNCTION makeMove */ void tryIt(int x, int y, int level) { /* FUNCTION tryIt */ int t; makeMove(x,y); t = boardToNum(); DEBUG printf("trying (%d, %d) - %d (%d)\n", x, y, level, steps[t]); if (0 == steps[t]) { /* new board */ steps[t] = level; } /* new board */ makeMove(x,y); } /* FUNCTION tryIt */ void process() { /* FUNCTION process */ int t; int i; int level = 1; t = boardToNum(); steps[t] = level; while (0 == steps[0]) { /* do each move */ for (i=1; 512>i; i++) { /* do each new step */ if (level == steps[i]) { /* try all moves from here */ level++; numToBoard(i); DEBUG printf("level %d: step: %d\n", level, i); tryIt(1,1,level); tryIt(1,2,level); tryIt(1,3,level); tryIt(2,1,level); tryIt(2,2,level); tryIt(2,3,level); tryIt(3,1,level); tryIt(3,2,level); tryIt(3,3,level); level--; } /* try all moves from here */ } /* do each new step */ level++; } /* do each move */ DEBUG dump(); DEBUG printf("Answer: "); printf("%d\n", steps[0] - 1); } /* FUNCTION process */ int main () { /* main */ int i; int cases; scanf(" %d ", &cases); opposite[0] = 1; opposite[1] = 0; for (i=0; i<cases; i++) { DEBUG printf("case %d\n", i); init(); getInput(); process(); } return EXIT_SUCCESS; } /* main */