JavaScript design mode communication (2)

Source: Internet
Author: User

Finally, the finish function can be modified on the basis of beam. We can determine a workable solution, ask for all solutions, or count the solutions by the finish check, the following finish is to find and print all the solutions.

Code:

function Finishqueen () {
if (this.depth<this.size) return false;
X=this.pos;y=this.depth-1;
while (--x>=0&&--y>=0)
if (this[y][x]!=0) return false;
X=this.pos;y=this.depth-1;
while (--y>=0)
if (this[y][x]!=0) return false;
X=this.pos;y=this.depth-1;
while (--y>=0&&++x<this.size)
{
if (this[y][x]!=0) return false;
}
document.write (this+ "\ n");
return false;
}

With these three functions, you can define a Bfsqueen class that inherits the Queen class and implements the Breadthfirstsearch interface definition as follows

Code:

function Bfsqueen (n)
{
var ret=new Queen (n);
var bfs=new breadthfirstsearch (Extendqueen,beamqueen,finishqueen);
Bfs.apply (ret);
return ret;
}

Here is the complete eight Queens problem code, on my computer about 30 seconds to use FF words only need half the time

<pre style= "font-family: Song Body;" >
<script>
function Queen (n) {
var ret=new Array ();
Ret.size=n; The size of the queen question
ret.depth=0; Depth of search
ret.pos=0; The level position of the new queen
for (Var y=0;y<n;y++)
{
Ret.push ([]);
for (Var x=0;x<n;x++)
Ret[ret.length-1].push (0);
}
function Objectprototypeclone ()
{
var tmp=function () {};
Tmp.prototype=this;
return new TMP;
}
Ret.clone=function () {
var r=objectprototypeclone.call (this);
for (Var i=0;i<n;i++)
{
R[i]=objectprototypeclone.call (This[i])
}
return R;
}
Ret.tostring=function () {
var str= "";
for (Var y=0;y<n;y++)
{
for (Var x=0;x<n;x++)
Str+=this[y][x]==0? " 0 ":" ★ ";
str+= "\ n";
}
return str;
}
return ret;
}
function Extendqueen ()
{
var ret=new Array ();
if (this.depth==this.size) return ret;
for (Var i=0;i<this.size;i++)
{
var current=this.clone ();
alert (current.depth);
Current[current.depth][i]=1;
Current.pos=i;
current.depth++;
Ret.push (current);
}
return ret;
}
function Beamqueen ()
{
var x,y;
if (this.depth==0) return false;
if (this.depth==this.size) return true;
X=this.pos;y=this.depth-1;
while (--x>=0&&--y>=0)
if (this[y][x]!=0) return true;
X=this.pos;y=this.depth-1;
while (--y>=0)
if (this[y][x]!=0) return true;
X=this.pos;y=this.depth-1;
while (--y>=0&&++x<this.size)
{
if (this[y][x]!=0) return true;
}
return false;
}
function Finishqueen () {
if (this.depth<this.size) return false;
X=this.pos;y=this.depth-1;
while (--x>=0&&--y>=0)
if (this[y][x]!=0) return false;
X=this.pos;y=this.depth-1;
while (--y>=0)
if (this[y][x]!=0) return false;
X=this.pos;y=this.depth-1;
while (--y>=0&&++x<this.size)
{
if (this[y][x]!=0) return false;
}
document.write (++count+ ". \ n" +this);
return false;
}
function Breadthfirstsearch (extend,beam,finish)
{
return function () {
This.finish=finish;
This.extend=extend;
This.beam=beam;
This.search=function () {
var queue=[this];
while (Queue.length)
{
var current=queue.shift ();
if (!current.beam ()) {
var extended=current.extend ();
for (Var i=0;i<extended.length;i++)
{
if (Extended[i].finish ()) return extended[i];
Queue.push (Extended[i]);
}
}
}
return null;
}
}
}
function Bfsqueen (n)
{
var ret=new Queen (n);
var bfs=new breadthfirstsearch (Extendqueen,beamqueen,finishqueen);
Bfs.apply (ret);
return ret;
}
var queen=new bfsqueen (8);
var count=0;
Queen.search ();
</script>
</pre>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.