Background: 1. Compilation error: Next and system naming conflicts. 2.wa: The starting point is greater than the condition of the floor is not considered. Wawawa the afternoon, I took the way to set the content to 1, instead of the flag array to mark whether access is right. Then experiment and put the visited mark as-100000, also ac!!! Indicates that Ki exists less than 0 in the title!!!!! Presence -1!!!!! and test instructions!! WA had an afternoon ...
My Code:
#include <cstdio> #include <iostream> #include <cstring> #include <queue>using namespace std; int floor[299],n,e;struct state{int X,count;} Now,next1;void BFS (state temp) {queue<state> q; temp.count=0; Q.push (temp); while (!q.empty ()) {Now=q.front (); if (now.x = = e) {printf ("%d\n", Now.count); Return }else{if (floor[now.x]) {next1.x=now.x+floor[now.x]; if (next1.x >= 1 && next1.x <= n && floor[next1.x]!= -1000000) {next1.count=now.c ount+1; Q.push (NEXT1); }} if (floor[now.x]) {next1.x=now.x-floor[now.x]; if (next1.x >= 1 && next1.x <= n && floor[next1.x]! = -1000000) {next1.count=now.c ount+1; Q.push (NEXT1); }} floor[now.x]=-1000000; Q.pop (); }} printf (" -1\n"); return;} int main (void) {while (scanf ("%d", &n), N) {scanf ("%d%d", &now.x,&e); memset (floor,-1000000,sizeof (floor)); for (int i=1;i <= n;i++) scanf ("%d", &floor[i]); if (now.x < 1 | | now.x > N) printf (" -1\n"); else BFS (now); } return 0;}
HDU 1458 BFS Template formulation