我必須從[0,0]出發訪問,那麼所有方從我的單板返回/坐在[0,0]騎士之旅發現路徑算法
圖片
這是我試圖代碼:
public class chess_F
{
// save my current position
struct position
{
public int row;
public int col;
};
const int NOT_VISITED = 0;
// my board
private int[,] cols = new int[8, 8];
// main function
public void Do()
{
position start;
start.row = 0;
start.col = 0;
initializeBoard(ref cols);
move(start, cols);
}
private void initializeBoard(ref int[,] board)
{
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
board[i, j] = NOT_VISITED;
}
private bool visited(int[,] board, position square)
{
return board[square.row, square.col] != NOT_VISITED;
}
/* |---|---|-0-|---|-1-|---|---|---|
* |---|-7-|---|---|---|-2-|---|---|
* |---|---|---|-X-|---|---|---|---|
* |---|-6-|---|---|---|-3-|---|---|
* |---|---|-5-|---|-4-|---|---|---|
* |---|---|---|---|---|---|---|---|
* |---|---|---|---|---|---|---|---|
* |---|---|---|---|---|---|---|---|
*/
// find all cols for my knight - can sit on
private position[] findPath(position present, int[,] cols)
{
position[] temp = new position[8];
position mytemp;
for (int i = 0; i < 8; i++)
{
try
{
//path 0
switch (i)
{
case 0:
{
if (cols[present.row - 1, present.col - 2] == 0)
{
mytemp.row = present.row - 1;
mytemp.col = present.col - 2;
temp[i] = mytemp;
}
break;
}
//path 1
case 1:
{
if (cols[present.row + 1, present.col - 2] == 0)
{
mytemp.row = present.row + 1;
mytemp.col = present.col - 2;
temp[i] = mytemp;
} break;
}
//path 2
case 2:
{
if (cols[present.row + 2, present.col - 1] == 0)
{
mytemp.row = present.row + 2;
mytemp.col = present.col - 1;
temp[i] = mytemp;
} break;
}
//path 3
case 3:
{
if (cols[present.row + 2, present.col + 1] == 0)
{
mytemp.row = present.row + 2;
mytemp.col = present.col + 1;
temp[i] = mytemp;
} break;
}
//path 4
case 4:
{
if (cols[present.row + 1, present.col + 2] == 0)
{
mytemp.row = present.row + 1;
mytemp.col = present.col + 2;
temp[i] = mytemp;
} break;
}
//path 5
case 5:
{
if (cols[present.row - 1, present.col + 2] == 0)
{
mytemp.row = present.row - 1;
mytemp.col = present.col + 2;
temp[i] = mytemp;
} break;
}
//path 6
case 6:
{
if (cols[present.row - 2, present.col - 1] == 0)
{
mytemp.row = present.row - 2;
mytemp.col = present.col - 1;
temp[i] = mytemp;
} break;
}
//path 7
case 7:
{
if (cols[present.row - 2, present.col - 1] == 0)
{
mytemp.row = present.row - 2;
mytemp.col = present.col - 1;
temp[i] = mytemp;
} break;
}
}
}
catch
{
mytemp.row = -1;
mytemp.col = -1;
temp[i] = mytemp;
}
}
return temp;
}
// check all cols and row to check ...
private bool allVisited(int[,] cols)
{
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (cols[i, j] != 1)
return false;
return true;
}
// save true path
int[,] truepath;
private void move(position present, int[,] cols)
{
int[,] tempCols = cols;
tempCols[present.row, present.col] = 1;
position[] avaliable = findPath(present, tempCols);
if (avaliable.Count() < 1)
return;
for (int i = 0; i < avaliable.Count(); i++)
{
if (allVisited(tempCols))
{
truepath = tempCols;
}
else
{
if (avaliable[i].row != -1 && avaliable[i].row != 0)
move(avaliable[i], tempCols);
}
}
}
}
我的問題是truepath變量總是爲空,我不能給triped方
什麼是你的策略是什麼?我會用這個8x8電路板回溯。不要對8x8或更大的板子使用暴力。 http://www.geeksforgeeks.org/archives/12916 – Kunukn
我想找到真正的路徑automaticaly /使用任何算法 - 但你的程序使用定義的路徑。 – warrior
您是否先嚐試過5x5大小?,根據您的算法,8x8可能會變慢。 – Kunukn