版本比较
1-27总结
正文
增加 12 行删除 12 行
40 行未改动41424344454647484950515253545556575859606162636465666768697071727374757632 行未改动109110111112113114115116117118119120121122123169 行未改动全部展开40 行未改动cout << 0 << endl;return;}memset(vis,0,sizeof(vis));//清空数组,避免错误的标记while(q.size()) {//同上q.pop();}q.push({x,0});vis[x] = 1;while(q.size()){node t = q.front();q.pop();if(t.x == y) {//这里为了避免后面的输出,直接结束函数cout << t.k << endl;return;}for(int i = 1;i <= 3;i ++) {int tx;if(i == 1) {//选择+1tx = t.x + 1;}if(i == 2) {//选择-1tx = t.x - 1;}if(i == 3) {//选择*2tx = t.x * 2;}if(tx >= 0 && tx < N && vis[tx] == 0) {q.push({tx, t.k + 1});vis[tx] = 1;}}}}int main(){32 行未改动node t = q.front();q.pop();for(int i = 1;i <= 2;i ++) {int tx;if(i == 1) {//操作1tx = t.id + k[t.id];}if(i == 2) {//操作2tx = t.id - k[t.id];}if(tx == y) {//是终点cout << t.k + 1 << endl;return ;}if(!vis[tx] && tx >= 1 && tx <= n) {169 行未改动全部展开40 行未改动41424344454647484950515253545556575859606162636465666768697071727374757632 行未改动109110111112113114115116117118119120121122123169 行未改动全部展开40 行未改动cout << 0 << endl;return;}memset(vis,0,sizeof(vis));//清空数组,避免错误的标记while(q.size()) {//同上q.pop();}q.push({x,0});vis[x] = 1;while(q.size()){node t = q.front();q.pop();if(t.x == y) {//这里为了避免后面的输出,直接结束函数cout << t.k << endl;return;}for(int i = 1;i <= 3;i ++) {int tx;if(i == 1) {//选择+1tx = t.x + 1;}if(i == 2) {//选择-1tx = t.x - 1;}if(i == 3) {//选择*2tx = t.x * 2;}if(tx >= 0 && tx < N && vis[tx] == 0) {q.push({tx, t.k + 1});vis[tx] = 1;}}}}int main(){32 行未改动node t = q.front();q.pop();for(int i = 1;i <= 2;i ++) {int tx;if(i == 1) {//操作1tx = t.id + k[t.id];}if(i == 2) {//操作2tx = t.id - k[t.id];}if(tx == y) {//是终点cout << t.k + 1 << endl;return ;}if(!vis[tx] && tx >= 1 && tx <= n) {169 行未改动全部展开