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

Quick write a program which lets you place n queens on an nxn

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 35
Thread images: 4

File: 416px-Chessboard480.svg (1).png (2KB, 416x416px) Image search: [Google]
416px-Chessboard480.svg (1).png
2KB, 416x416px
Quick write a program which lets you place n queens on an nxn chess board so they can't attack each other. Remember queens can attack in all eight directions.
>>
>>61100207
Hey, it's another >see if /g/ will do your homework thread
>>
summer school already?
>>
Do your homework annon it will help you succeed later on
>>
>>61100328
This
>>
>>61100207
bump
>>
var a=0
var b=0
var c=0
var d=0
var e=0
var f=0
var g=0
var h=0
var s=0;
//generate positions without vertical or horizontal conflict
function GenRows() {
a=Math.floor(Math.random()*7+1);
while(b==0||a==b){b=Math.floor(Math.random()*8+1);}
while(c==0||a==c||b==c){c=Math.floor(Math.random()*8+1);}
while(d==0||a==d||b==d||c==d){d=Math.floor(Math.random()*8+1);}
while(e==0||a==e||b==e||c==e||d==e){e=Math.floor(Math.random()*8+1);}
while(f==0||a==f||b==f||c==f||d==f||e==f){f=Math.floor(Math.random()*8+1);}
while(g==0||a==g||b==g||c==g||d==g||e==g||f==g){g=Math.floor(Math.random()*8+1);}
h=36-a-b-c-d-e-f-g
}
//check output of GenRows for diagonal conflict
function CkDiag() {
if(b==a-1||b==a+1||c==a-2||c==a+2||d==a-3||d==a+3||e==a-4||e==a+4||f==a-5||f==a+5||g==a-6||g==a+6||h==a-7||h==a+7||c==b-1||c==b+1||d==b-2||d==b+2||e==b-3||e==b+3||f==b-4||f==b+4||g==b-5||g==b+5||h==b-6||h==b+6||d==c-1||d==c+1||e==c-2||e==c+2||f==c-3||f==c+3||g==c-4||g==c+4||h==c-5||h==c+5||e==d-1||e==d+1||f==d-2||f==d+2||g==d-3||g==d+3||h==d-4||h==d+4||f==e-1||f==e+1||g==e-2||g==e+2||h==e-3||h==e+3||g==f-1||g==f+1||h==f-2||h==f+2||h==g-1||h==g+1){s=0}else{s=1}
}
//repeat GenRows and CkDiag until valid solution is obtained
while(s==0){
GenRows()
CkDiag()
}
//print answer on page
if(s==1){document.write('HORIZONTAL POSITIONS OF THE QUEENS IN EACH ROW<br>'+a+'&nbsp;'+b+'&nbsp;'+c+'&nbsp;'+d+'&nbsp;'+e+'&nbsp;'+f+'&nbsp;'+g+'&nbsp;'+h+'<br><br>');}
>>
File: 1414435179405.jpg (115KB, 706x546px) Image search: [Google]
1414435179405.jpg
115KB, 706x546px
>>61100656
>if(b==a-1||b==a+1||c==a-2||c==a+2||d==a-3||d==a+3||e==a-4||e==a+4||f==a-5||f==a+5||g==a-6||g==a+6||h==a-7||h==a+7||c==b-1||c==b+1||d==b-2||d==b+2||e==b-3||e==b+3||f==b-4||f==b+4||g==b-5||g==b+5||h==b-6||h==b+6||d==c-1||d==c+1||e==c-2||e==c+2||f==c-3||f==c+3||g==c-4||g==c+4||h==c-5||h==c+5||e==d-1||e==d+1||f==d-2||f==d+2||g==d-3||g==d+3||h==d-4||h==d+4||f==e-1||f==e+1||g==e-2||g==e+2||h==e-3||h==e+3||g==f-1||g==f+1||h==f-2||h==f+2||h==g-1||h==g+1){s=0}else{s=1}
>>
>>61100656
This doesn't do n x n boards

Sorry
>>
File: 1498366439417-pol.jpg (58KB, 640x632px) Image search: [Google]
1498366439417-pol.jpg
58KB, 640x632px
>>61100207
>>61100207
>Quick write a program which lets you place n queens on an nxn chess board so they can't attack each other. Remember queens can attack in all eight directions.

Technically we only have to print an N by N board. You said "that lets you place" you never said the "program should place" in regards to placing the queens.

Lrn2ritespecsgudfgt
>>
>>61100656
kys idiot
>>
Brotip, there can only be 8 queens.
>>
>>61101011
It's good to be the king.
>>
>>61101149
Correction, >>61100936
There can only be n queens.

My current line of thought is to add queens on varying ends by placing them at L's (the movement of the knight) from each other and then to turn the board around and do the same on the other side. At an L, they're at each other's inaccessible positions.

It's an array indexing problem, inb4 some cunt in Haskell one lines this.
>>
>>61100207
code won't work for certain values of n
>>
The fastest algorithm for this is backtracking. It stops the search immediately as soon as there is a collision. So for example...
Q on A8
QC7
QE6
QG5
QB4
QD3
QF2
Then it as no more valid moves (QH1 is illegal).
So it back traces to the last change-able move. Basically it's like brute force searching, except you instantly eliminate the start of a wrong path.
>>
>>61100328
>fun programming thread
or
>bitter autistic gpu fighting thread no. 99999

hmm
>>
>>61100207

import Data.List

mapIx f= (flip $ zipWith f) [1..]
unique xs=nub xs == xs
diagonal xs = any (not.unique)
[mapIx (+) xs,mapIx (-) xs]
all' n= permutations [1..n]
Queens n=filter (not.diagonal) $ all' n
>>
>>61102163
Brainlet.

First, make a lookup table for every square. Uint64 will suffice for boards not greater than 64 squares, emulate an arbitrary width bitfield for anything else.
Then, it will take n! quick iterations to explore every possibility.
>>
>>61100207
But... there's nothing dynamic about this. You literally can solve this on paper and just have your program output hardcoded data.
>>
Find a permutation s such that for all i and j, |s(i) - s(j)| != |i - j|. Trivial and fast enough for 8 × 8 boards with backtracking.

Now back to the homework board.
>>>/hm/
>>
>>61102375
What you're suggesting is just implementation improvement, not an actual change to the algorithm. Having a lookup table doesn't change which comparisons are done, it just better optimizes the computer. Same for using a bitfield, like here: http://www.cl.cam.ac.uk/~mr10/backtrk.pdf.

All I'm talking about is the optimal algorithm, not the optimal implementation. Hope you enjoyed my autism.
>>
>>61101194
>n queens
No, it's n-1 queens.
It's impossible to place 2 queens in 2x2, 3 queens in 3x3 and 4 in 4x4.
So the solution is (n-1) queens can be placed on an nxn board, which makes things very simple. >>61100207 doesn't have a solution for n queens, thus the task is b8 or retarded.
>>
>>61104366
http://dl.acm.org/citation.cfm?id=122322
Search hill climbing n queens
>>
>>61102547
The same way you can sum all primes under 200000
>>61104954
an 8x8 board can hold 8 queens???

Obviously certain n it wont work.
>>
something something bruteforce sucks something something bitboards and genetic algorithm

do your own homework
>>
File: 1496954787525.jpg (74KB, 960x926px) Image search: [Google]
1496954787525.jpg
74KB, 960x926px
>>61100656
>if(b==a-1||b==a+1||c==a-2||c==a+2||d==a-3||d==a+3||e==a-4||e==a+4||f==a-5||f==a+5||g==a-6||g==a+6||h==a-7||h==a+7||c==b-1||c==b+1||d==b-2||d==b+2||e==b-3||e==b+3||f==b-4||f==b+4||g==b-5||g==b+5||h==b-6||h==b+6||d==c-1||d==c+1||e==c-2||e==c+2||f==c-3||f==c+3||g==c-4||g==c+4||h==c-5||h==c+5||e==d-1||e==d+1||f==d-2||f==d+2||g==d-3||g==d+3||h==d-4||h==d+4||f==e-1||f==e+1||g==e-2||g==e+2||h==e-3||h==e+3||g==f-1||g==f+1||h==f-2||h==f+2||h==g-1||h==g+1){s=0}else{s=1}
>>
>>61105463
Show me a 8x8 board with 8 queens. I can't seem to find the positions of queens on anything other than 5x5.
>>
>>61105597
https://youtu.be/jPcBU0Z2Hj8?t=4m10s
>>
Just bruteforce it, dude : ^)
>>
>>61105632
>using the smiley with a carat nose
>>
>>61106045
>commenting on other peoples usage of the smiley with a carat nose
>>
>>61105545
genetic algorithms are dogshit for this problem
>>
>>61106208
If you seek legitimate potty training service, contact [email protected] ...dude's a pooing guru, involved with pooing in loos, hacked into my ex's loo and scrotum, what let me knowing she was streetshitter and also gave my nephew some really outstanding poo scores which he upgraded himself, cool way to have pooing freedom as well. Get your loo-pooing atm cards which could debit money from any a.t.m machine. Make $20,000 and more in a couple loo visits. Poo transfers and loo transfers as well as poo-in-loo jobs, hes that good, had to make him my personal pooer. You could mail him as well if you got issues, he's as discreet and professional too. He's kinda picky though so make mention of the reference.........
>>
cout<<"Go away regard, C++ is superior";
Thread posts: 35
Thread images: 4


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.