Test Method of the minimum number of referers in a knight's Travel problem

Source: Internet
Author: User

Package homework;

 

Import java. AWT. container;

Import java. AWT. flowlayout;

 

Import javax. Swing. japplet;

Import javax. Swing. jscrollpane;

Import javax. Swing. jtextarea;

 

Public class knighttravel extends japplet {// server guard travel 7.22

Private Static final long serialversionuid = 1l;

 

Int horizontal [] = {,-1,-2,-2,-, 2}; // indicates the moving direction and number of steps. The upward and downward directions are negative.

Int vertical [] = {-1,-2,-2,-, 1 };

Int movenumber; // The moving method 0--7

Int Board [] [] = new int [8] [8]; // Board

Int xboard [] [] = {2, 3, 4, 4, 4, 3, 2}, {3, 4, 6, 6, 4, 3}, {4, 6, 8, 8, 8, 6, 4 }, },

{, 8, 8, 8, 6, 4}, {, 6, 6, 6, 4, 3}, {, 4, 4, 4, 3, 2}; // record the ease of square access, the smaller the value, the harder it is to access

Int moverecord; // record the current selection

Int footsteps; // records the number of server guard moves

Int currentrow = 0;

Int currentcol = 0;

Int S = 1; // number of successful records

Int record = 0; // records the maximum number of steps currently moved by the server guard.

Int row, Col; // records the rows and columns to be moved.

Int xchangerow, xchangecol; // records the rows and columns whose number of shards is reduced.

 

Boolean R; // indicates whether the data is successfully moved.

Boolean movejudge; // determines whether it can be moved.

 

Jtextarea outputextarea;

String outputstring = "";

Jscrollpane scrollpane;

 

Public void Init (){

Container = getcontentpane ();

Container. setlayout (New flowlayout ());

 

Outputextarea = new jtextarea (17,20 );

Scrollpane = new jscrollpane (outputextarea );

Container. Add (scrollpane );

 

For (currentrow = 0; currentrow <8; currentrow ++)

For (currentcol = 0; currentcol <8; currentcol ++ ){

R = move (board, currentrow, currentcol); // Mobile Server guard

Print (board, R); // print the result

}

}

 

// Server guard mobile Process

Public Boolean move (INT array [] [], int currentrow, int currentcol ){

// Int currentrow = 0; // The current row

// Int currentcol = 0; // current column

// Initialization

For (int row = 0; row <8; row ++)

For (INT Col = 0; Col <8; Col ++)

Board [row] [col] = 0;

 

Int xboard [] [] = {2, 3, 4, 4, 4, 3, 2}, {3, 4, 6, 6, 4, 3}, {4, 6, 8, 8, 8, 6, 4 }, },

}};

 

Record = 0;

 

Footsteps = 1;

Int tempcr, tempcc; // keep the current row at the forefront

 

Board [currentrow] [currentcol] = footsteps ++;

 

While (FOOTSTEPS <= 64 ){

Movenumber = 0;

Moverecord = 9;

Movejudge = false;

 

While (movenumber <8) {// find the current minimum number of workers

// If (movenumber> 7)

// Return false;

Tempcr = currentrow;

Tempcc = currentcol;

 

Currentrow + = vertical [movenumber];

Currentcol + = horizontal [movenumber];

 

If (currentrow <8 & currentrow> = 0 & currentcol> = 0 & currentcol <8 // whether to cross the chessboard

& Board [currentrow] [currentcol] = 0) {// whether the location has passed

Movejudge = true;

If (xboard [currentrow] [currentcol] <moverecord ){

Moverecord = xboard [currentrow] [currentcol];

Row = currentrow;

Col = currentcol;

}

}

// Board [row] [col] = footsteps;

// Record ++;

// Break;

Currentrow = tempcr; // returns the original location

Currentcol = tempcc;

Movenumber ++;

}

 

If (movejudge ){

Currentrow = row; // Save the current location

Currentcol = Col;

Board [row] [col] = footsteps;

Record ++;

Footsteps ++;

 

Movenumber = 0;

Xchangerow = row;

Xchangecol = Col;

While (movenumber <8 ){

Tempcr = xchangerow;

Tempcc = xchangecol;

 

Xchangerow + = vertical [movenumber];

Xchangecol + = horizontal [movenumber];

 

If (xchangerow <8 & xchangerow> = 0 & xchangecol> = 0 & xchangecol <8 // whether to cross the chessboard

& Board [xchangerow] [xchangecol] = 0) // whether the location has passed

Xboard [xchangerow] [xchangecol] --;

Xchangerow = tempcr;

Xchangecol = tempcc;

Movenumber ++;

}

}

Else

Return false;

}

Return true;

}

 

// Print the moving result

Public void print (INT parray [] [], boolean result ){

If (result ){

For (int row = 0; row <8; row ++ ){

For (INT Col = 0; Col <8; Col ++)

Outputstring + = Board [row] [col] + "";

Outputstring + = "/N ";

}

Outputstring + = "success! "+ S +"/N ";

S ++;

}

Else {

For (int row = 0; row <8; row ++ ){

For (INT Col = 0; Col <8; Col ++)

Outputstring + = Board [row] [col] + "";

Outputstring + = "/N ";

}

Outputstring + = "Move false! "+" "+ Record +"/N ";

}

Outputextarea. settext (outputstring );

}

}

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.