1 条题解
-
0
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1005, M = 2e6 + 5, P = 1e9 + 7; const int inf = 0x3f3f3f3f; const long long inF = 0x3f3f3f3f3f3f3f3f; int n = 100, m, ans, st[N][N]; int d[][2] = {-2, -2, -2, -1, -2, 1, -2, 2, -1, -2, -1, 2, 1, -2, 1, 2, 2, -2, 2, -1, 2, 1, 2, 2}; int bfs(int x, int y) { memset(st, 0x00, sizeof st); queue<pair<int, int> > q; q.push({x, y}), st[x][y] = 1; while (q.size()) { auto u = q.front(); q.pop(); x = u.first, y = u.second; if (x == 1 && y == 1) return st[x][y] - 1; for (int i = 0; i < 12; i++) { int a = x + d[i][0], b = y + d[i][1]; if (a < 1 || a > n || b < 1 || b > n) continue; if (st[a][b]) continue; q.push({a, b}), st[a][b] = st[x][y] + 1; } } return -1; } void solve() { int x, y; cin >> x >> y; cout << bfs(x, y) << "\n"; } signed main() { int _t = 2; // cin>>_t; while (_t--) solve(); return 0; }
- 1
信息
- ID
- 542
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 258
- 已通过
- 101
- 上传者