騎士問題 特殊的bfs

2021-08-25 20:33:55 字數 1355 閱讀 9859

這個是來自 國際大學生程式設計競賽例題解(1)的題目

很簡單的一道搜尋題目,但是沒有使用dfs,

使用的是特殊的bfs,非常巧妙 **

/*

qi shi wen ti

similar to eight queens

use bfs() to get the min step

*/#include #include #include int queue[1000][2];

int matrix[8][8];

int startx,starty,endx,endy;

int head = -1,tail = 0;

int dir[8][2] = ,,,,,,,};

void put(int x,int y)

void get(int * x,int * y)

*x = queue[head][0];

*y = queue[head][1];

}int queueempty()

/* the problem is how to calculate the step

it seems like it's hard to find out one step's next step in the bfs search

while it is easy in the dfs search;

standard bfs

*/void bfs(int startx,int starty,int endx,int endy)

else if( 1)

else

} }}

/* special bfs

it visited all nodes in a layer (just a layer's node int bfs tree) in a round

*/int specialbfs(int startx,int starty,int endx,int endy)

}} head = tail;

tail = tmptail;

} return -1;

}int main()

scanf("%c%c\n",&c1,&c2);

startx = c1 - 'a';

starty = c2 - '1';

scanf("%c%c\n",&c1,&c2);

endx = c1 - 'a';

endy = c2 - '1';

printf("%d\n",specialbfs(startx,starty,endx,endy));

fclose(stdin);

return 0;

}

騎士旅行 BFS

description 在乙個n m 格仔的棋盤上,有乙隻西洋棋的騎士在棋盤的左下角 1 1 如圖1 騎士只能根據象棋的規則進行移動,要麼橫向跳動一格縱向跳動兩格,要麼縱向跳動一格橫向跳動兩格。例如,n 4,m 3 時,若騎士在格仔 2 1 如圖2 則騎士只能移入下面格仔 1 3 3 3 或 4 2...

騎士旅行 bfs

題目描述 description 在乙個n m 格仔的棋盤上,有乙隻西洋棋的騎士在棋盤的左下角 1 1 如圖1 騎士只能根據象棋的規則進行移動,要麼橫向跳動一格縱向跳動兩格,要麼縱向跳動一格橫向跳動兩格。例如,n 4,m 3 時,若騎士在格仔 2 1 如圖2 則騎士只能移入下面格仔 1 3 3 3 ...

騎士旅行(BFS)

description 在乙個n m 格仔的棋盤上,有乙隻西洋棋的騎士在棋盤的左下角 1 1 如圖1 騎士只能根據象棋的規則進行移動,要麼橫向跳動一格縱向跳動兩格,要麼縱向跳動一格橫向跳動兩格。例如,n 4,m 3 時,若騎士在格仔 2 1 如圖2 則騎士只能移入下面格仔 1 3 3 3 或 4 2...