#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 10 /* * Author: * Date: * Purpose: * Problem: */ /* * This template reads data until a terminating value is reached. */ int board[MAX_CASES][MAX_CASES]; int where[9][2]; int cnt; void init() { /* FUNCTION init */ int i; int k; for (i=0; i<10; i++) { /* each row */ for (k=0; k<10; k++) { /* each column */ board[i][k] = 0; } /* each column */ } /* each row */ } /* FUNCTION init */ void dump() { /* FUNCTION dump */ int i; int k; for (i=1; i<9; i++) { /* each row */ for (k=1; k<9; k++) { /* each column */ printf("%d ", board[i][k]); } /* each column */ printf("\n"); } /* each row */ } /* FUNCTION dump */ void getInput() { /* FUNCTION getInput */ int i; int k; char tmp; cnt = 1; for (i=1; 9>i; i++) { /* each row */ for (k=1; 9>k; k++) { /* for each column */ scanf("%c", &tmp); if ('*' == tmp) { /* found a queen */ where[cnt][0] = i; where[cnt][1] = k; board[i][k] = cnt++; } /* found a queen */ } /* for each column */ scanf("%c", &tmp); } /* each row */ } /* FUNCTION getInput */ int checkRowCol(int q) { /* FUNCTION checkRowCol */ int i = 1; int j = 1; int k; int toReturn = TRUE; for (k=1; k<9; k++) { /* for each possible spot */ if (0 != board[where[q][0]][k]) { /* square is non empty */ toReturn = toReturn && (q == board[where[q][0]][k]); /* ok if non empty square is where current queen is */ } /* square is non empty */ if (0 != board[k][where[q][1]]) { /* square is non empty */ toReturn = toReturn && (q == board[k][where[q][1]]); /* ok if non empty square is where current queen is */ } /* square is non empty */ } /* for each possible spot */ return toReturn; } /* FUNCTION checkRowCol */ int check(int q, int x, int y) { /* FUNCTION check */ int toReturn = TRUE; DEBUG printf("check : %d %d \n", x, y); if ((0 < x) && (9 > x) && (0 < y) && (9 > y)) { /* got a valid spot */ DEBUG printf("check - really board[%d][%d] = %d == %d\n", x, y, board[x][y], q); toReturn = (0 == board[x][y]); } /* got a valid spot */ return toReturn; } /* FUNCTION check */ int checkDiag(int q) { /* FUNCTION checkDiag */ int i = 1; int j = 1; int k; int toReturn = TRUE; int x; int y; x = where[q][0]; y = where[q][1]; for (k=1; k<8; k++) { /* for each possible spot */ toReturn = toReturn && check(q, x + k, y + k) && check(q, x + k, y - k) && check(q, x - k, y + k) && check(q, x - k, y - k); } /* for each possible spot */ return toReturn; } /* FUNCTION checkDiag */ void process() { /* FUNCTION process */ int i; int k; int q = 1; int valid; valid = (9 == cnt); while ((valid) && (q < cnt)) { /* check each queen */ valid = checkRowCol(q) && checkDiag(q); q++; } /* check each queen */ if (valid) { printf("valid\n"); } else { printf("invalid\n"); } } /* FUNCTION process */ int main () { /* main */ init(); getInput(); DEBUG dump(); process(); return EXIT_SUCCESS; } /* main */