solve/wall-follower
沿墙走
选一种语言就能看到代码
TURN_ORDER = (1, 0, 3, 2) def wall_follower(start, goal, facing): cell = start path = [start] while cell != goal: for turn in TURN_ORDER: way = (facing + turn) % 4 other = neighbor(cell, way) if other >= 0: facing = way cell = other break path.append(cell) return path
const TURN_ORDER = [1, 0, 3, 2]; function wallFollower(start, goal, facing) { const path = [start]; let cell = start; while (cell !== goal) { for (const turn of TURN_ORDER) { const way = (facing + turn) % 4; const other = neighbor(cell, way); if (other < 0) continue; facing = way; cell = other; break; } path.push(cell); } return path; }
static const int TURN_ORDER[4] = {1, 0, 3, 2}; int wall_follower(int start, int goal, int facing, int path[]) { int cell = start; int count = 0; path[count++] = start; while (cell != goal) { for (int i = 0; i < 4; i++) { int way = (facing + TURN_ORDER[i]) % 4; int other = neighbor(cell, way); if (other < 0) continue; facing = way; cell = other; break; } path[count++] = cell; } return count; }
constexpr int TURN_ORDER[4] = {1, 0, 3, 2}; std::vector<int> wall_follower(int start, int goal, int facing) { std::vector<int> path{start}; int cell = start; while (cell != goal) { for (int turn : TURN_ORDER) { int way = (facing + turn) % 4; int other = neighbor(cell, way); if (other < 0) continue; facing = way; cell = other; break; } path.push_back(cell); } return path; }
static readonly int[] TurnOrder = { 1, 0, 3, 2 }; static List<int> WallFollower(int start, int goal, int facing) { var path = new List<int> { start }; int cell = start; while (cell != goal) { foreach (int turn in TurnOrder) { int way = (facing + turn) % 4; int other = Neighbor(cell, way); if (other < 0) continue; facing = way; cell = other; break; } path.Add(cell); } return path; }
static final int[] TURN_ORDER = {1, 0, 3, 2}; static List<Integer> wallFollower(int start, int goal, int facing) { List<Integer> path = new ArrayList<>(); path.add(start); int cell = start; while (cell != goal) { for (int turn : TURN_ORDER) { int way = (facing + turn) % 4; int other = neighbor(cell, way); if (other < 0) continue; facing = way; cell = other; break; } path.add(cell); } return path; }