Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #define die(...) exit((printf(__VA_ARGS__), puts(""), 1))
- #define H 48
- #define W 40
- #define N 18
- char maze[H][W] = {
- "#################",
- "########## ####",
- "########## ####",
- "########## ########",
- "########## ########",
- "########## ########",
- "########## ############",
- "########## ### #######",
- "########## ### #######",
- "##### ### ### ###########",
- "##### ### ### ###########",
- "##### ### ### ###########",
- "##### ###########",
- "##### ###########",
- " E ##########",
- " #########",
- " ################",
- " #########",
- " #########",
- " #########",
- " #########",
- " ###########",
- " ###########",
- " ###########",
- " #####",
- " #",
- " #",
- " #",
- " #",
- " #",
- " #",
- " #",
- " ##########",
- " ##########",
- " ##########",
- " ##########",
- " ##########",
- " ##########",
- " ##########",
- " ############",
- " ############",
- " ############",
- " ############",
- " ############S",
- };
- const char *words[N] = {
- "AMENDMENT",
- "BACON",
- "BASKETBALL",
- "BRANDNEW",
- "CARBONARA",
- "DRACONIAN",
- "ENTOURAGE",
- "FORENSICS",
- "GRAY",
- "HARPO",
- "INSTANCE",
- "LACED",
- "LAXSECURITY",
- "LOGAN",
- "RENOVATED",
- "SCARS",
- "SONOROUS",
- "WORKAHOLIC",
- };
- enum {
- Down, Across
- };
- enum {
- Plain, Postscript
- };
- void print(int mode)
- {
- if (mode == Plain) {
- for (int j = 0; j < H; j++) {
- for (int i = 0; i < W; i++) {
- if (isalpha((unsigned char) maze[j][i])) {
- putchar(maze[j][i]);
- } else {
- putchar(maze[j][i] == '#' ? '.' : ' ');
- }
- }
- putchar('\n');
- }
- } else if (mode == Postscript) {
- puts("%!PS-Adobe 3.0");
- puts("/W 12 def");
- puts("/M { W mul } bind def");
- puts("/S { newpath moveto W 0 rlineto");
- puts(" 0 W rlineto W neg 0 rlineto closepath stroke");
- puts("} bind def");
- puts("/L { moveto");
- puts(" dup stringwidth pop -0.5 mul 0 rmoveto show");
- puts("} bind def");
- puts("/Helvetica findfont W 0.9 mul scalefont setfont");
- puts("0.5 setlinewidth");
- for (int j = 0; j < H; j++) {
- for (int i = 0; i < W; i++) {
- int al = isalpha((unsigned char) maze[j][i]);
- if (al || maze[j][i] == '#') {
- int x = i + 2;
- int y = H + 2 - j;
- printf("%d M %d M S\n", x, y);
- if (al) printf("(%c) %d.5 M %d.15 M L\n", maze[j][i], x, y);
- }
- }
- }
- puts("showpage");
- }
- }
- void put(const char *str, char *p, int y, int x, int dir)
- {
- while (*str) {
- if (p) *p++ = maze[y][x];
- maze[y][x] = *str++;
- if (dir == Across) x++; else y++;
- }
- if (p) *p = '\0';
- }
- int test(const char *str, int y, int x, int dir)
- {
- if (x < 0 || y < 0) return 0;
- while (*str) {
- if (y >= H) return 0;
- if (x >= W) return 0;
- if (maze[y][x] != '#' && maze[y][x] != *str) return 0;
- str++;
- if (dir == Across) x++; else y++;
- }
- return 1;
- }
- int final(const char *w, int y, int x, int dir)
- {
- int len = strlen(w);
- if (dir == Down) y += len; else x += len;
- if (y == 15 && x == 2) return 1;
- if (y == 43 && x == 34) return 1;
- return 0;
- }
- void try(int y, int x, int n, int k, int curr, int dir)
- {
- const char *ww = words[k];
- if (test(ww, y, x, dir)) {
- char save[20];
- words[k] = words[--n];
- put(ww, save, y, x, dir);
- if (n == 0) {
- if (final(ww, y, x, dir)) {
- print(Plain);
- }
- } else {
- for (int l = 0; ww[l]; l++) { // letter in this word
- if (abs(l - curr) < 2) continue;
- for (int i = 0; i < n; i++) { // possible next word
- const char *s = words[i];
- for (int j = 0; s[j]; j++) { // letter in next word
- if (ww[l] == s[j]) {
- if (dir == Across) {
- try(y - j, x + l, n, i, j, Down);
- } else {
- try(y + l, x - j, n, i, j, Across);
- }
- }
- }
- }
- }
- }
- put(save, NULL, y, x, dir);
- words[k] = ww;
- n++;
- }
- }
- void tryword(const char *w, int y, int x, int dir)
- {
- for (int i = 0; i < N; i++) {
- if (strcmp(w, words[i]) == 0) {
- int len = strlen(w);
- if (dir == Down) {
- try(y - len, x, N, i, len, dir);
- } else {
- try(y, x - len, N, i, len, dir);
- }
- return;
- }
- }
- printf("%s not found", w);
- }
- int main()
- {
- #if 0
- tryword("INSTANCE", 15, 2, Down);
- tryword("ENTOURAGE", 15, 2, Down);
- #else
- tryword("SCARS", 43, 34, Across);
- tryword("SONOROUS", 43, 34, Across);
- tryword("FORENSICS", 43, 34, Across);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement