oslon Posted Monday at 03:05 AM Share Posted Monday at 03:05 AM https://www.cs.drexel.edu/~johnsojr/2017-18/fall/CS270/Lectures/8/sat.pdf How'd you proceed? Language doesn't matter technique matters. You can as well write that into SQL. Quote Link to comment Share on other sites More sharing options...
requinix Posted Monday at 10:51 AM Share Posted Monday at 10:51 AM Easy: brute force it. Quote Link to comment Share on other sites More sharing options...
oslon Posted Monday at 12:55 PM Author Share Posted Monday at 12:55 PM Yeah that's my problem. I am not getting ideas to brute force it.(How to convert it to code)... Â I know how to solve, but converting it to code is something crazy tough for this problem. Quote Link to comment Share on other sites More sharing options...
jodunno Posted Monday at 05:27 PM Share Posted Monday at 05:27 PM no arrays, recursion/backtracking? I had to research what is meant by "4 queens problem". I am not a chess player and i'm a self taught coder. If i am understanding the rules correctly: one per diagonal per row. m4 x n4 and four queens. I would convert the vertices using pythagorean theorem to find the diagonals. Place your marks and store the row and column in a variable. It would be much faster than recursion and arrays anyway. One solution is what you mentioned. Quote Link to comment Share on other sites More sharing options...
requinix Posted Monday at 11:02 PM Share Posted Monday at 11:02 PM Pythagorean theorem? What? Â 10 hours ago, oslon said: Yeah that's my problem. I am not getting ideas to brute force it.(How to convert it to code)... Describe the solution you have in mind and we'll figure out how to convert it into code. Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 02:17 AM Author Share Posted yesterday at 02:17 AM 3 hours ago, requinix said: Pythagorean theorem? What? Â Describe the solution you have in mind and we'll figure out how to convert it into code. Here is my solution. (I only need to find 1 such solution) Place a queen on board when there is no conflict with the other queen(diagonalwise, row-wise, col-wise) Do this till you reach the last column. If there is conflict, reset and start again. Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 02:20 AM Author Share Posted yesterday at 02:20 AM public class Example { public static void main(String[] args) { int rows = 4; int cols = 4; int[] queens = {-1, -1, -1, -1, -1, -1, -1, -1}; displayBoard(rows, cols); } public static void displayBoard(int r, int c) { for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { System.out.print("| "); } System.out.print("|"); System.out.println(); } } } So far I've got upto here but I am unable to process further code. Please don't provide chatgpt solution as my goal is to learn not to solve this problem. Quote Link to comment Share on other sites More sharing options...
requinix Posted yesterday at 02:34 AM Share Posted yesterday at 02:34 AM 13 minutes ago, oslon said: Place a queen on board when there is no conflict with the other queen(diagonalwise, row-wise, col-wise) But where? How do you decide where to make each move? And how do you make sure you don't make the same set of moves in future attempts? 15 minutes ago, oslon said: If there is conflict, reset and start again. Doesn't that count as backtracking? Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 03:08 AM Author Share Posted yesterday at 03:08 AM Backtracking allowed with 1d array(no 2d array allowed). Quote Link to comment Share on other sites More sharing options...
jodunno Posted yesterday at 03:31 AM Share Posted yesterday at 03:31 AM one mark (queen if you want to play chess) per row per column is not rocket science and not an exercise that is worthy of programming exercise. add vision to your code and logic. If you asked me to find, quote, one solution, then let us prey: <?php for ($i = 1; $i <= 4; $i++) { echo '<div>'; for ($j = 1; $j <= 4; $j++) { echo '<div style="display: inline-block; padding: 8px; width: 40px;">' . pow($i,2) + pow($j,2) . '</div>'; } echo '</div>'; } //instructions: find the matching numbers around the perimiters //example: find the 5 and opposite 10. find the 25 and the 20 //queens are set one per column per row. finished. what? ?> Â Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 10:07 AM Author Share Posted yesterday at 10:07 AM I've no idea what beer they serve in germany. Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 10:14 AM Author Share Posted yesterday at 10:14 AM I've written a FSM as well. However, having hard time to translate to code: if(!solved) - place queen on 1st col->2nd col->3rd col->4th col else - quit  Quote Link to comment Share on other sites More sharing options...
oslon Posted yesterday at 01:10 PM Author Share Posted yesterday at 01:10 PM How to solve the 4 queens puzzle in Java ? – Memoirs of a Software developer (ajayiyengar.com) I even see a solution lol but can't formulate my own. Quote Link to comment Share on other sites More sharing options...
requinix Posted yesterday at 04:00 PM Share Posted yesterday at 04:00 PM 12 hours ago, jodunno said: one mark (queen if you want to play chess) per row per column is not rocket science That's right, it wouldn't be - if that was what the Queens Problem actually was. But it isn't, as you missed an important restriction in the puzzle: at most one queen per diagonal. 12 hours ago, oslon said: Backtracking allowed with 1d array(no 2d array allowed). ...but the title says no 2D arrays (a trivial restriction to get around) or recursion or backtracking? If you can do recursion and backtracking then do recursion and backtracking. That's the straightforward solution. Which means you need to think about how to do recursion. Recursion is about breaking the problem down into a tiny example, solving that, and then figuring out how to take that tiny solution and expand it a little bit more. So you should be thinking like this: 1. How can I solve this for a 1x1 grid? 2. How can I take a NxN grid that is valid and place another queen in a valid position using either the N+1 row or N+1 column? 5 hours ago, oslon said: I've written a FSM as well. However, having hard time to translate to code: if(!solved) - place queen on 1st col->2nd col->3rd col->4th col else - quit You're having a hard time because you're not describing your solution in enough detail. Programming is about telling the computer how to do what you have in mind. But that means you have to have something in mind first. Don't have that and you won't be able to tell the computer anything. Quote Link to comment Share on other sites More sharing options...
jodunno Posted yesterday at 04:33 PM Share Posted yesterday at 04:33 PM 27 minutes ago, requinix said: That's right, it wouldn't be - if that was what the Queens Problem actually was. But it isn't, as you missed an important restriction in the puzzle: at most one queen per diagonal. Hello? you disappoint me. The code that i posted must be far over your heads. Two solutions are revealed in the code but two people here are unable to see it. LOL. Pythagorean theorem reveals diagonals (id est right angle triangles), which will place the queens in different diagonals, LOL, in different rows and columns. LOL. good lord! I feel like i'm talking to a wall. Nevermind. The concept is beyond the both of you. Carry on.... 🙂 Quote Link to comment Share on other sites More sharing options...
Barand Posted yesterday at 05:40 PM Share Posted yesterday at 05:40 PM <!DOCTYPE html> <html lang="en"> <head> <title>4 Queens</title> <meta charset="utf-8"> <style type='text/css'> table { border-collapse: collapse; } td { width: 40px; height: 40px; text-align: center; font-size: 16pt; } tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) { background-color: lightgreen; } </style> </head> <body> <?php echo '<table border="1">'; for ($i = 1; $i <= 4; $i++) { echo "<tr>"; for ($j = 1; $j <= 4; $j++) { echo '<td>' . pow($i,2) + pow($j,2) . '</td>'; } echo '</tr>'; } echo "</table>\n"; ?> </body> </html> Dear Supreme Being, Can you put aside your arrogance for a moment and assume that we are 5 year olds (not a wall) and explain the theory behind your Pythagorean solution to the problem and why it "works" explain how the above square then tells us where the four queens should be positioned? I've found the 5s but there are no "opposite 10s", only 20s opposite the 5s, so the description is somewhat vague.. Having found the 5s, 10s, 20s and 25s (why those values in particular?), I now have 8 squares. Given that these are the candidates, the code then needs to find which 4 of these 8 fit the bill. Quote Link to comment Share on other sites More sharing options...
jodunno Posted yesterday at 05:56 PM Share Posted yesterday at 05:56 PM 14 minutes ago, Barand said: <!DOCTYPE html> <html lang="en"> <head> <title>4 Queens</title> <meta charset="utf-8"> <style type='text/css'> table { border-collapse: collapse; } td { width: 40px; height: 40px; text-align: center; font-size: 16pt; } tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) { background-color: lightgreen; } </style> </head> <body> <?php echo '<table border="1">'; for ($i = 1; $i <= 4; $i++) { echo "<tr>"; for ($j = 1; $j <= 4; $j++) { echo '<td>' . pow($i,2) + pow($j,2) . '</td>'; } echo '</tr>'; } echo "</table>\n"; ?> </body> </html> Dear Supreme Being, Can you put aside your arrogance for a moment and assume that we are 5 year olds (not a wall) and explain the theory behind your Pythagorean solution to the problem and why it "works" explain how the above square then tells us where the four queens should be positioned? I've found the 5s but there are no "opposite 10s", only 20s opposite the 5s, so the description is somewhat vague.. Having found the 5s, 10s, 20s and 25s (why those values in particular?), I now have 8 squares. Given that these are the candidates, the code then needs to find which 4 of these 8 fit the bill. explain nothing. you figure it out. And i'm the guy that only had remedial math classes. And some of my code is already posted in the grid graph thread. pointer: perimeters. Nevermind. Make your mountain out of a mole hill. Â Quote Link to comment Share on other sites More sharing options...
Barand Posted 5 hours ago Share Posted 5 hours ago FYI - 8 queens solution... The Pythagorean numbers were as much use as a chocolate teapot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.