哈理工oj(acm.hrbust.edu.cn) 1498【bfs】
View Code
#include<stdio.h> #include<string.h> #include<limits.h> const int INF = INT_MAX; int que[10000000]; int mem[10000000]; int main() { int f, s, g, u, d; while(scanf("%d %d %d %d %d", &f, &s, &g, &u, &d) != EOF) { int *fr = que, *ta = que; for(int i = 0; i <= f; i++) mem[i] = INF; mem[s] = 0; *ta++ = s; while(fr < ta) { int c = *fr++, t, sp; sp = mem[c]+1; t = c + u; if(t<=f && mem[t]>sp) { mem[t] = sp; *ta++ = t; } t = c - d; if(t>0 && mem[t]>sp) { mem[t] = sp; *ta++ = t; } if(mem[g] < INF) break; } if(mem[g] < INF) printf("%d\n", mem[g]); else printf("use the stairs\n"); } return 0; }