[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
with your favorite language
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 29
Thread images: 1
File: 8queens.jpg (18 KB, 254x258) Image search: [Google]
8queens.jpg
18 KB, 254x258
>>
>>55132138
The point of 8 queens isn't to be a language exercise, but an algorithm exercise.
>>
>>55132138
Doing this in Racket lisp with backtracking was pretty fun. When I get around to learning about constraint logic programming I'm going to try it again with their kanren module.
>>
>>55132213
You tell me that when you write recursive subroutines in assembly.
>>
>>55132138
Python, although I'm missing some stuff from Java like multiple constructors or overloaded methods.
>>
>>55132138
I start with C++ and that's the only language I know...
>>
>>55132138
So kill me, I like javascript. I love anon functions and simple asynchronous stuff. It's okay now I promise.
>>
>>55132138
solutions :: [[Int]]
solutions = helper []
where
helper xs = if length xs == 8
then return xs
else possibleChoices xs >>= (\x -> helper (x:xs))

possibleChoices :: [Int] -> [Int]
possibleChoices xs = filter (`notElem` notAllowed) [1..8]
where
notAllowed = xs
++ foldr (\x xs -> map (+1) (x:xs)) [] xs
++ foldr (\x xs -> map (flip (-) 1) (x:xs)) [] xs
>>
>>55132138
English
>>
>>55132138
French.
Is French really the language of love?
Of course! Have you ever planned a romantic holiday to Paris, or learnt how to say je t’aime? There’s no denying that French really is the language of love.
>>
>>55135838
>French
>writes in English
>>
>>55132259
At that level, is there even a difference between recursive and iterative routines?
>>
>>55133940
did u copy and paste this?
>>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int Dim, Solutions = 0;
int* Queens;

void solution ()
{
for (int j, i = 0; i < Dim; ++i) {
for (j = 0; j < Queens[i]; ++j) {
printf(" .");
}
printf(" Q");
for (j++; j < Dim; ++j) {
printf(" .");
}
printf("\n");
}
printf("\n");
Solutions++;
}

void fill_bad_loc (int* bad, int x, int y)
{
/* vertical */
for (int i = y; i < Dim; i++) {
bad[i] |= 1 << x;
}
/* diagonal */
for (int i = 0; i < Dim; i++) {
int dx = (i<x) ? (x-i) : (i-x);
bad[(y + dx) % Dim] |= 1 << i;
}
}

void search_queens (const int* bad_prev, int y)
{
if (y == Dim) {
solution();
return;
}
int bad[Dim];
for (int x = 0; x < Dim; ++x) {
if (!(bad_prev[y] & (1 << x))) {
Queens[y] = x;
/* copy array */
memcpy(bad, bad_prev, sizeof(bad));
fill_bad_loc(bad, x, y);
/* search next loc */
search_queens(bad, y + 1);
}
}
}

int main (int argc, char** argv)
{
if (argc > 1) {
Dim = strtol(argv[1], NULL, 0);
} else {
Dim = 8;
}
/* create board and initial map */
int queens[Dim];
Queens = queens;
int bad[Dim];
memset(bad, 0, sizeof(bad));
search_queens(bad, 0);
printf("%d solutions for %dx%d board\n",
Solutions, Dim, Dim);
return 0;
}


C, < 100 LOC
>>
>>55137173
Yes, recursive means you have to call it recursively and you are adding to the stack frame.
>>
>>55137364
Nope. I wrote it in Haskell.
>>
>>55137491
looks elegant, too bad I don't understand any of it
>>
>>55137398
So basically it's being needlessly inefficient for the sake of a pedantic difference?
>>
>>55137546
>what is tail call optimization
>>
>>55137500
The format that I used is a list of ints, where each int describes the position of a queen on a row. I chose to number the position from 1 to 8, so the solution show in OP would be represented as: [4,7,3,8,2,5,1,6].

possibleChoices takes in a partial solution and outputs the list of valid positions for a queen to be placed in the next row up. For example: possibleChoices [1,6] = [3,5,7]

Then, the solutions helper function basically bruteforces solutions using that.
>>
>>55137593
i kind of understand.
But don't columns matter too?
and how many solutions does n queens give anyways?
>>
>>55132138
C. Used to really enjoy Java but haven't worked with it in a long while.
>>
>>55137715
The int itself is the column number and its position in the list is the row number.

There's no formula for working out how many solutions exist: https://oeis.org/A000170
>>
>>55137752
I see.
Like I said, the solution looks interesting.
And btw, why did you
flip (-) 1
instead of (-1)?
>>
>>55137782
Haskell interprets (-1) as the number negative one.
>>
>>55137790
oh yes, I forgot that little caveat
any projects you've done in haskell, or just anything really
>>
>>55137803
I've been working on a deterministic password generator. Similar to https://www.pwdhash.com/ but with bcrypt instead of MD5.
>>
>>55137833
good luck desu
I'm still a newbie, so I do pajeet tier code, hopefully I can get to your level before long.
Good luck senpai
>>
>>55137855
Thanks, and good luck to you too.
Thread replies: 29
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.