FreeRTOS的任務優先順序測試
阿新 • • 發佈:2022-03-24
題目連結
題目思路
還是經典的bfs模板題,只是多了四個方向
題目程式碼
#include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int, int> PII; const int N = 510; int dist[N][N]; bool st[N][N]; int bfs(int x, int y) { memset(dist, -1, sizeof dist); memset(st, false, sizeof st); queue<PII> q; q.push({x, y}); dist[x][y] = 0; st[x][y] = true; while(q.size()) { auto t = q.front(); q.pop(); int dx[12] = {-1, -2, -2, -1, 1, 2, 2, 1, 2, -2, 2, -2}, dy[12] = {2, 1, -1, -2, 2, 1, -1, -2, 2, -2, -2, 2}; for(int i = 0; i < 12; i ++ ) { int a = t.first + dx[i], b = t.second + dy[i]; if(a >= 1 && a < N && b >= 1 && b < N && !st[a][b]) { dist[a][b] = dist[t.first][t.second] + 1; if(a == 1 && b == 1) return dist[a][b]; q.push({a, b}); st[a][b] = true; } } } } int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cout << bfs(x1, y1) << endl << bfs(x2, y2) << endl; return 0; }