注意,需要提交的是一个整数,不要填写任何无关内容(比如:解释说明等)
PS:直接用bfs的话可能是跑不出来的,用set标志一下取得数剪枝即可
#include<bits/stdc++.h> #include<queue> #include<set> using namespace std; int way[2]={97,-127}; struct node{ int x; int step; }; void bfs() { node cur; queue<node>q; set<int>s; s.insert(0); cur.x = 0; cur.step = 0; q.push(cur); while(!q.empty()){ cur = q.front(); q.pop(); // cout << cur.step << " " << cur.x << endl; if (cur.x == 1){ cout << cur.step; return ; } for(int i = 0; i < 2; i++){ node next; next.x = cur.x + way[i]; next.step = cur.step + 1; if (s.count(next.x) == 0){ s.insert(next.x); q.push(next); } } } } int main() { bfs(); return 0; }