1 条题解
-
2
#include<bits/stdc++.h> using namespace std;
struct node{ int x, d; node(int xx, int dd) { x = xx; d = dd; } }; int n, a, b; int dir[3] = {-1, 1, 2}; bool vis[5010];
bool in(int x){ return x < n && x > 0; }
int bfs(int sx){ queue<node> q; q.push(node(sx, 0)); vis[sx] = true; while (!q.empty()) { node now = q.front(); q.pop(); for (int i = 0; i < 3; i ++) { int tx; if(i == 2) tx = now.x * dir[i]; else tx = now.x + dir[i]; if (in(tx) && !vis[tx]) { if (tx == b) { return now.d + 1; } else{ vis[tx] = true; q.push(node(tx, now.d + 1)); } } } } }
int main(){ freopen("move.in","r",stdin); freopen("move.out","w",stdout); cin >> n >> a >> b; cout << bfs(a); return 0; }
- 1
信息
- ID
- 556
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 143
- 已通过
- 27
- 上传者