-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from Kray-G/feature/issue#7/add-samples
fixed #7: added samples
- Loading branch information
Showing
7 changed files
with
244 additions
and
1 deletion.
There are no files selected for viewing
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
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,14 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(void) | ||
{ | ||
int i = 1; | ||
if (*((char *)&i)) | ||
printf("little-endian\n"); | ||
else if (*((char *)&i + (sizeof(int) - 1))) | ||
printf("big-endian\n"); | ||
else | ||
printf("unknown\n"); | ||
return 0; | ||
} |
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,44 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define N 5 | ||
|
||
int board[N + 4][N + 4], | ||
dx[8] = { 2, 1,-1,-2,-2,-1, 1, 2 }, | ||
dy[8] = { 1, 2, 2, 1,-1,-2,-2,-1 }; | ||
|
||
void printboard(void) | ||
{ | ||
int i, j; | ||
static solution = 0; | ||
|
||
printf("\nSolution #%d\n", ++solution); | ||
for (i = 2; i <= N + 1; i++) { | ||
for (j = 2; j <= N + 1; j++) printf("%4d", board[i][j]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
void try(int x, int y) | ||
{ | ||
int i; | ||
static int count = 0; | ||
|
||
if (board[x][y] != 0) return; | ||
board[x][y] = ++count; | ||
if (count == N * N) printboard(); | ||
else for (i = 0; i < 8; i++) try(x + dx[i], y + dy[i]); | ||
board[x][y] = 0; count--; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int i, j; | ||
|
||
for (i = 0; i <= N + 3; i++) | ||
for (j = 0; j <= N + 3; j++) board[i][j] = 1; | ||
for (i = 2; i <= N + 1; i++) | ||
for (j = 2; j <= N + 1; j++) board[i][j] = 0; | ||
try(2, 2); | ||
return 0; | ||
} |
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,35 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define N 22 | ||
#define M 78 | ||
#define ALLDISP 1 | ||
#define LAST 1000 | ||
char a[N + 2][M + 2], b[N + 2][M + 2]; | ||
|
||
int main(void) | ||
{ | ||
int i, j, g; | ||
|
||
a[N/2][M/2] = a[N/2-1][M/2] = a[N/2+1][M/2] | ||
= a[N/2][M/2-1] = a[N/2-1][M/2+1] = 1; | ||
for (g = 1; g <= LAST; g++) { | ||
if (ALLDISP || g == LAST) printf("Generation %4d\n", g); | ||
for (i = 1; i <= N; i++) { | ||
for (j = 1; j <= M; j++) | ||
if (a[i][j]) { | ||
if (ALLDISP || g == LAST) printf("*"); | ||
b[i-1][j-1]++; b[i-1][j]++; b[i-1][j+1]++; | ||
b[i ][j-1]++; b[i ][j+1]++; | ||
b[i+1][j-1]++; b[i+1][j]++; b[i+1][j+1]++; | ||
} else if (ALLDISP || g == LAST) printf("."); | ||
if (ALLDISP || g == LAST) printf("\n"); | ||
} | ||
for (i = 0; i <= N + 1; i++) | ||
for (j = 0; j <= M + 1; j++) { | ||
if (b[i][j] != 2) a[i][j] = (b[i][j] == 3); | ||
b[i][j] = 0; | ||
} | ||
} | ||
return 0; | ||
} |
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,35 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define B(x) if (ok[x]) { ok[x]=0; | ||
#define E(x) } ok[x]=1; | ||
#define forall(x) for (x = 1; x <= 16; x++) | ||
#define FORMAT \ | ||
"%4d%4d%4d%4d\n%4d%4d%4d%4d\n%4d%4d%4d%4d\n%4d%4d%4d%4d\n" | ||
int main(void) | ||
{ | ||
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, | ||
x, count = 0, | ||
ok11[40], *ok = ok11 + 11; /* ok[-11..28] */ | ||
|
||
for (x = -11; x <= 0; x++) ok[x] = 0; | ||
for (x = 1; x <= 16; x++) ok[x] = 1; | ||
for (x = 17; x <= 28; x++) ok[x] = 0; | ||
forall(a) { ok[a] = 0; | ||
for (d = a + 1; d <= 16; d++) { ok[d] = 0; | ||
for (m = d + 1; m <= 16; m++) { ok[m] = 0; | ||
p = 34 - a - d - m; | ||
if (ok[p] && a < p) { ok[p] = 0; | ||
forall(b) B(b) c = 34 - a - b - d; B(c) | ||
forall(f) B(f) k = 34 - a - f - p; B(k) | ||
forall(g) B(g) j = 34 - d - g - m; | ||
B(j) n = 34 - b - f - j; B(n) o = 34 - c - g - k; B(o) | ||
forall(e) B(e) i = 34 - a - e - m; | ||
B(i) h = 34 - e - f - g; B(h) l = 34 - i - j - k; | ||
if (ok[l]) { | ||
printf("Solution #%d\n", ++count); | ||
printf(FORMAT, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p); | ||
E(h) E(i) E(e) E(o) E(n) E(j) E(g) E(k) | ||
E(f) E(c) E(b) E(p) E(m) E(d) E(a) | ||
} | ||
return 0; | ||
} |
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,68 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
#define XMAX 80 | ||
#define YMAX 24 | ||
#define MAXSITE (XMAX * YMAX / 4) | ||
char map[XMAX + 1][YMAX + 1]; | ||
int nsite = 0; | ||
int xx[MAXSITE], yy[MAXSITE]; | ||
int dx[4] = { 2, 0, -2, 0 }; | ||
int dy[4] = { 0, 2, 0, -2 }; | ||
int dirtable[24][4] = { | ||
0,1,2,3, 0,1,3,2, 0,2,1,3, 0,2,3,1, 0,3,1,2, 0,3,2,1, | ||
1,0,2,3, 1,0,3,2, 1,2,0,3, 1,2,3,0, 1,3,0,2, 1,3,2,0, | ||
2,0,1,3, 2,0,3,1, 2,1,0,3, 2,1,3,0, 2,3,0,1, 2,3,1,0, | ||
3,0,1,2, 3,0,2,1, 3,1,0,2, 3,1,2,0, 3,2,0,1, 3,2,1,0 }; | ||
|
||
void add(int i, int j) | ||
{ | ||
xx[nsite] = i; yy[nsite] = j; nsite++; | ||
} | ||
|
||
int select(int *i, int *j) | ||
{ | ||
int r; | ||
|
||
if (nsite == 0) return 0; | ||
nsite--; r = (int)(nsite * (rand() / (RAND_MAX + 1.0))); | ||
*i = xx[r]; xx[r] = xx[nsite]; | ||
*j = yy[r]; yy[r] = yy[nsite]; return 1; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int i, j, i1, j1, d, t, *tt; | ||
|
||
srand((unsigned)time(NULL)); | ||
for (i = 0; i <= XMAX; i++) | ||
for (j = 0; j <= YMAX; j++) map[i][j] = 1; | ||
for (i = 3; i <= XMAX - 3; i++) | ||
for (j = 3; j <= YMAX - 3; j++) map[i][j] = 0; | ||
map[2][3] = 0; map[XMAX - 2][YMAX - 3] = 0; | ||
for (i = 4; i <= XMAX - 4; i += 2) { | ||
add(i, 2); add(i, YMAX - 2); | ||
} | ||
for (j = 4; j <= YMAX - 4; j += 2) { | ||
add(2, j); add(XMAX - 2, j); | ||
} | ||
while (select(&i, &j)) { | ||
for ( ; ; ) { | ||
tt = dirtable[(int)(24 * (rand() / (RAND_MAX + 1.0)))]; | ||
for (d = 3; d >= 0; d--) { | ||
t = tt[d]; i1 = i + dx[t]; j1 = j + dy[t]; | ||
if (map[i1][j1] == 0) break; | ||
} | ||
if (d < 0) break; | ||
map[(i + i1) / 2][(j + j1) / 2] = 1; | ||
i = i1; j = j1; map[i][j] = 1; add(i, j); | ||
} | ||
} | ||
for (j = 2; j <= YMAX - 2; j++) { | ||
for (i = 2;i <= XMAX - 2; i++) | ||
if (map[i][j]) putchar('X'); else putchar(' '); | ||
putchar('\n'); | ||
} | ||
return 0; | ||
} |
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,45 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define N 8 | ||
int a[N], b[2 * N - 1], c[2 * N - 1], x[N]; | ||
|
||
void found(void) | ||
{ | ||
int i, j; | ||
static solution = 0; | ||
|
||
printf("\nSolution #%d\n", ++solution); | ||
for (i = 0; i < N; i++) { | ||
for (j = 0; j < N; j++) | ||
if (x[i] == j) printf(" Q"); | ||
else printf(" ."); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
void try(int i) | ||
{ | ||
int j; | ||
|
||
for (j = 0; j < N; j++) | ||
if (a[j] && b[i + j] && c[i - j + N - 1]) { | ||
x[i] = j; | ||
if (i < N - 1) { | ||
a[j] = b[i + j] = c[i - j + N - 1] = 0; | ||
try(i + 1); | ||
a[j] = b[i + j] = c[i - j + N - 1] = 1; | ||
} else found(); | ||
} | ||
} | ||
|
||
int main(void) | ||
{ | ||
int i; | ||
|
||
for (i = 0; i < N; i++) a[i] = 1; | ||
for (i = 0; i < 2 * N - 1; i++) b[i] = 1; | ||
for (i = 0; i < 2 * N - 1; i++) c[i] = 1; | ||
try(0); | ||
return 0; | ||
} |