Exemple #1
0
void
eight_queen(int c)
{
        int r = 0;

        if (c == col) {
                if (c < 8)
                        eight_queen(c + 1);
                if (c == 8)
                        print();
        } else {
                /* (r, c) identify a position
                 */
                for (r = 1; r <= 8; r++) {
                        int i = 0, j = 0, is_ok = 1;

                        for (i = 1; i <= 8; i++) /* i is row number */
                                if (i != r && board[i][c] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;
                        for (i = 1; i <= 8; i++) /* i is col number */
                                if (i != c && board[r][i] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;
                        for (i = r - 1, j = c - 1; i >= 1 && j >= 1; i--, j--)
                                if (board[i][j] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;
                        for (i = r + 1, j = c + 1; i <= 8 && j <= 8; i++, j++)
                                if (board[i][j] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;
                        for (i = r - 1, j = c + 1; i >= 1 && j <= 8; i--, j++)
                                if (board[i][j] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;
                        for (i = r + 1, j = c - 1; i <=8 && j >= 1; i++, j--)
                                if (board[i][j] == 1)
                                        is_ok = 0;
                        if (is_ok == 0)
                                continue;

                        cols[c] = r;
                        board[r][c] = 1;
                        if (c < 8)
                                eight_queen(c + 1);
                        if (c == 8)
                                print();
                        board[r][c] = 0;
                }
        }
}
Exemple #2
0
int main(int argc, char *argv[])
{
    int row = 0; // row
    eight_queen(row);

    return 0;
}
Exemple #3
0
// test
int main(){
	int i;
	write(-1000);
	errorCount = 0;
	write(paraTest(10));
	eight_queen(0);
	write(gCount);
	structTest();
	breakContinueTest();
	for(i = 0; i < 25; ++i){
		write(callTest(i));
	}
	for(i = 0; i < 25; ++i){
		write(feb2(i));
	}
	logicArithmaticTest();
	arrayTest();
	scopeVariableTest();
	arithmaticTest();
	loopTest();
	relopTest();
	dfs(0,0);
	write(errorCount);
	write(-2000);
	return 0;
}
Exemple #4
0
int
main(int argc, char **argv)
{
        int ncases = 0;

        scanf("%d\n", &ncases);

        while (ncases--) {
                int i = 0;

                memset(board, 0, sizeof(board));
                num = 0;

                scanf("%d %d\n", &row, &col);
                board[row][col] = 1;
                cols[col] = row;

                printf("SOLN       COLUMN\n");
                printf(" #      1 2 3 4 5 6 7 8\n\n");
                eight_queen(1);
                if (ncases != 0)
                        putchar('\n');
        }

        return 0;
}
Exemple #5
0
int eight_queen(int index)
{
	int loop;
	for(loop = 0; loop < 8; ++loop){
		if(check_pos_valid(index, loop)){
			gEightQueen[index] = loop;
			if(7 == index){
				++gCount;
				print();
			    gEightQueen[index] = 0;
				return 0;
			}
			eight_queen(index + 1);
			gEightQueen[index] = 0;
		}
	}
	return 0;
}
Exemple #6
0
void eight_queen(int row)  
{  
    for(int col = 0; col < 8; col++){  
        if(check_pos_valid(row, col)){  
            gEightQueen[row] = col;  
  
            if(7 == row){  
                gCount ++;
                print();  
                //gEightQueen[row] = 0;  
                return;  
            }  
              
            eight_queen(row + 1);  
            //gEightQueen[row] = 0;
            //print();
            //printf("xx%dxx.\n", gTest++);
        }  
    }  
}