forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
c988ffc
commit 0a8d29d
Showing
17 changed files
with
129 additions
and
129 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2015, Leo Ma <[email protected]> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
|
||
static int N; | ||
|
||
static inline int conflict(int *stack, int i, int j) | ||
{ | ||
int k; | ||
for (k = 0; k < i; k++) { | ||
if (j == stack[k] || abs(i - k) == abs(j - stack[k])) { | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static inline void push(int *stack, int i, int j) | ||
{ | ||
stack[i] = j; | ||
// printf("push %d %d\n", i, j); | ||
} | ||
|
||
static inline int pop(int *stack, int i) | ||
{ | ||
int j = stack[i]; | ||
stack[i] = -1; | ||
// printf("pop %d %d\n", i, j); | ||
return j; | ||
} | ||
|
||
static inline int top(int *stack) | ||
{ | ||
int i; | ||
for (i = N - 1; i >= 0; i--) { | ||
if (stack[i] != -1) { | ||
return i; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void show(int *stack) | ||
{ | ||
int i, j; | ||
for (i = 0; i < N; i++) { | ||
for (j = 0; j < N; j++) { | ||
if (j == stack[i]) { | ||
printf("Q"); | ||
} else { | ||
printf("."); | ||
} | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, j, sum = 0; | ||
int *stack; | ||
|
||
if (argc != 2) { | ||
printf("Usage: ./queen 8\n"); | ||
exit(-1); | ||
} | ||
|
||
N = atoi(argv[1]); | ||
if (N <= 0) { | ||
exit(0); | ||
} | ||
|
||
stack = malloc(N * sizeof(*stack)); | ||
if (stack == NULL) { | ||
exit(-1); | ||
} | ||
|
||
if (N == 1) { | ||
stack[0] = 0; | ||
show(stack); | ||
printf("This is the %dth solution.\n", ++sum); | ||
exit(0); | ||
} | ||
|
||
for (i = 0; i < N; i++) { | ||
stack[i] = -1; | ||
} | ||
|
||
i = j = 0; | ||
for (; ;) { | ||
for (; i < N; i++) { | ||
while (j < N) { | ||
if (conflict(stack, i, j)) { | ||
while (j == N - 1) { | ||
/* No space for row[i] so backtracking is needed */ | ||
if (--i < 0) { | ||
free(stack); | ||
exit(0); | ||
} | ||
j = pop(stack, i); | ||
} | ||
j++; | ||
} else { | ||
push(stack, i, j); | ||
break; | ||
} | ||
} | ||
j = 0; | ||
} | ||
|
||
i = top(stack); | ||
if (i == N - 1) { | ||
show(stack); | ||
printf("This is the %dth solution.\n", ++sum); | ||
} | ||
|
||
j = pop(stack, i); | ||
j++; | ||
} | ||
|
||
assert(0); | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.