Welcome to HQ's Blog

Hello World! If you've visited this blog before, welcome back. Please help me what article do you like by give it a comment. But if you're new with this blog, please feel free to stay ahead for a shortwhile, to read my articles. I assure you will get something by read it carefully. Haha...

Ok ok... By the way, this blog belongs to me, Haqqi. Yeah, that is my nickname. If you want to know about me, please read the top sidebar titled "About Me". If you want to know MORE about me, please feel free to add my Messenger (xp_guitarist) and chat with me. I am Indonesian. My English is not so good, so please don't laugh if my 'syntax' is wrong. But sometimes I will post my blog in Bahasa Indonesia (I think most of them will).

The reason I create this blog is, I want to share everything I can share. From my experience, computer program I used, my algorithms and source codes, and some useful infos and tips. So, if you want to request something I can, feel free to ask. And if you also want to share me something, I always open my hand, haha. Anyway, WELCOME TO MY BLOG!

This site is best viewed with Mozilla Firefox with resolution 1024x800 or more

Page copy protected against web site content infringement by Copyscape

ANNOUNCEMENT!!!

This blog is dead!!! I moved it to my new blog in http://haqqi.net. Thank you for reading this blog. Hope you will read my new blog too.


N-Queen Problem by Backtracking

Dec 12, 2008

One of my pressures is already solved

Ok, one of my big pressure, or maybe just my obligation as a student, is already solved. Yeah, as you can see in the title of this post, it's about the task of subject "Algorithm and Programming 2" in my study. Yes, I have to create an algorithm to solve N-Queen problem, and then make a presentation to get highest score in my big quiz.

What is N-Queen problem? As you can see in another search result in search engine, in general, N-Queen problem is how can you get a sollution to set n number of Queen on n x n size of chessboard, where no one can attack each other. In my task, I have to use backtracking algorithm, in order to get the closest sollution, rather than all sollution. It means the output is, there is any sollution or not.

I won't give any source code yet, because when I post this posting, another student maybe still not present it yet. But I will give the algorithm, here it is:

We need 2 arrays to use this algorithm. First is the location of the Queens. We can use 1 dimension array with Integer type to save their coordinates. Why just 1 dimension array? Because we know that just one Queen for one row, so we can save the coordinate like this: queen[y]=x; It means that queen at row y is settled at column x. It will save with less capacity rather than 2 dimension array. The second array has to be 2 dimension array with Boolean type. We use it to save the location where Queens cannot take place based on the coordinate. After one Queen is settled, we change the value of second array, where is the row, column, and all diagonals of the position of the Queen, to be "cannot take place". So in the program's loop, it will be checked and no queens will try that place. So easy, isn't it?

I will give explanation about the variables and methods before the pseudocode. Variable queens means the position of the Queens, variable place means the status of free place (it is initialized with 'true' value), variable y means row, variable x means column, and variable size means board size and the number of Queens. Method isAllSolved() will return whether the problem is already solved or not (true if already solved, false is not solved yet), method createCopyOf(array) will create new array with the same value as the parameter, method setCannotTakePlace(x, y) means set the row, column, and diagonals of [x, y] to be 'false', so no queens can take that place. And method isRowSolved(place, y) return whether the row y is already solved or not (return true if there is any queen, and false if no queen at that row). Here is the pseudocode:
Download here

As I tell before, I won't give any source code yet. But I will give the demo of my program as executable jar file. I use GUI to display the sollution. Once you run the program, an input dialog box will be displayed. You have to type the board size at any size but negative (because the program won't run anything). After that, a window is opened and you will see the first sollution. You can see another sollution by press right/left arrow on your keyboard. I tell you again, it is 'another sollution', not the next sollution. You will see how can it happen after you get the source code soon.

Ok, here is the jar file download link:
Download Here
As usual, I set the password at my file:
fauzilhaqqi.blogspot.com

Ok, I hope you will enjoy waiting the source code. If you can't wait any longer, please be free to contact me at my email, fauzil.haqqi@gmail.com. I will send to you privately as you give your contact too. One more thing, I use Java SE to create the program.

5 comments:

constantine said...

iya deh boss,, percaya deh,, expert nieh udah,, hue he5....

kapan nieh mo bagi2 ilmunye?! hue he5.... tp serius keren bgt program km...,, keren...keren... smangat!!!!

Haqqi said...

Itu masih belom ada apa-apanya. Saia masih pemula. Kebetulan baca literatur yang pas.

Hehe....

god name goes online said...

pake bahasa Indonesia donk
*kyknya gw dulu gk dapet pelajaran n begini

Haqqi said...

@callMeOnce: Yah, bahasa inggris biar bisa dibaca orang luar juga. Hehe... sekalian belajar english lah...

Unknown said...

Salam kenal,

Saya lagi dapet masalah untuk 8 queen problem.
Tapi inginnya initial state,misal
1. dalam satu kolom paling kiri terssusun 8 queen
2. acak posisi queen nya
Goal state nya sperti yang boss bikin.
Bisa bantu ga boss???