与经典的骑士问题不同之处在于:因为要求字典序最小,所以搜索时我们要保证下一个可以遍历到的点是字典序最小的即可
#include <cstdio>
#include <cstring>
int M, N; //M is row(1,2,...), N is col(A,B,...)
bool canBeStart[26][26];//record whether [i][j] can be start
int total, vis[26][26];//total = M * N, record [i][j]'s visit time(0 ~ total-1)
char path[26 * 2 + 1]; //total path
const int MOVE[8][2] = {
{-1, -2}, {1, -2},
{-2, -1}, {2, -1},
{-2, 1}, {2, 1},
{-1, 2}, {1, 2},
};
bool isOutOfBounds(int r, int c)
{
return r < 0 || r >= M || c < 0 || c >= N;
}
bool dfs(int row, int col, int time)
{
vis[row][col] = time++;
if(time == total) retur