Problem Link: UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves. basic level exercises, written in C + + language program.
Test instructions: Give the two points in the chess board, and find the minimum number of steps the horse jumps from one point to another.
Problem Analysis: A typical BFS problem. In the BFS search process, the horse skipped the point no longer to jump, because this time it is not possible to jump more than the last step of less.
program, a queue is used to hold the middle node, but it needs to be emptied each time it runs out.
Here are two versions of the program, one with the border set.
The AC C + + language program is as follows:
/* UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves * * #include <cstdio> #include <cstring> #include <queue> using namespace std; #define MAXN 8#define directsize 8struct Direct {int drow; int Dcol;} Direct[directsize] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2,-1}, {-1,-2}, {-1 Startcol, StartRow, Endcol, endrow;int ans;struct node {int row; int col; int level;}; Queue<node> q;void BFs () {memset (grid, ', sizeof (GRID)); Ans = 0; node start; Start.row = StartRow; Start.col = Startcol; Start.level = 0; Q.push (start); while (!q.empty ()) {node front = Q.front (); Q.pop (); if (Front.row = = Endrow && Front.col = = Endcol) {ans = front.level; Break } for (int i=0; i<directsize; i++) {int nextrow = Front.row + direct[i].drow; int nextcol = Front.col + direct[i].dcol; if (0 <= nextrow && nExtrow < MAXN && 0 <= nextcol && nextcol < MAXN) if (grid[nextrow][nextcol] = = ") {node V; V.row = NextRow; V.col = Nextcol; V.level = Front.level + 1; Q.push (v); }} Grid[front.row][front.col] = ' * '; }}int Main (void) {char startc, ENDC; while (scanf ("%c%d%c%d", &STARTC, &startrow, &ENDC, &endrow)! = EOF) {GetChar (); while (!q.empty ()) Q.pop (); Startcol = STARTC-' a '; StartRow-= 1; Endcol = ENDC-' a '; Endrow-= 1; BFS (); printf ("To get from%c%d to%c%d takes%d Knight moves.\n", STARTC, Startrow+1, Endc, endrow+1, ans); } return 0;}
Another version of the AC C + + language program is as follows:
/* UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves * * #include <cstdio> #include <cstring> #include <queue> using namespace std; #define MAXN 8#define directsize 8struct Direct {int drow; int Dcol;} Direct[directsize] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2,-1}, {-1,-2}, {-1,-+ +--+--+--+--] -1}};char grid[maxn+4][maxn+4] ; int Startcol, StartRow, Endcol, endrow;int ans;struct node {int row; int col; int level;}; Queue<node> q;void BFs () {ans = 0; node start; Start.row = StartRow; Start.col = Startcol; Start.level = 0; Q.push (start); while (!q.empty ()) {node front = Q.front (); Q.pop (); if (Front.row = = Endrow && Front.col = = Endcol) {ans = front.level; Break } for (int i=0; i<directsize; i++) {int nextrow = Front.row + direct[i].drow; int nextcol = Front.col + direct[i].dcol; if (grid[nextrow][nextcol] = = ") {node V; V.row = NextRow; V.col = Nextcol; V.level = Front.level + 1; Q.push (v); }} Grid[front.row][front.col] = ' * '; }}int Main (void) {int i; Char STARTC, ENDC; while (scanf ("%c%d%c%d", &STARTC, &startrow, &ENDC, &endrow)! = EOF) {GetChar (); while (!q.empty ()) Q.pop (); Startcol = STARTC-' a ' + 2; StartRow + = 1; Endcol = ENDC-' a ' + 2; Endrow + = 1; memset (grid, ', sizeof (GRID)); for (i=0; i<maxn+4; i++) {Grid[0][i] = ' * '; Grid[1][i] = ' * '; Grid[maxn+2][i] = ' * '; Grid[maxn+3][i] = ' * '; GRID[I][0] = ' * '; GRID[I][1] = ' * '; GRID[I][MAXN+2] = ' * '; GRID[I][MAXN+3] = ' * '; } BFS (); printf ("To get from%c%d to%c%d takes%d Knight moves.\n", STARTC, Startrow-1, Endc, endrow-1, ans); } return 0;}
UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves