回复 8楼 xzlxzlxzl
还没完呢~这题其实是老鼠走迷宫求最短路径的变种~上楼用了深度搜索求解~但深度搜索求最优解效率不大~有兴趣的可以做个广度搜索试试~
[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
#include <stdio.h> #include <limits.h> #include <stdbool.h> #define M 120 int main() { int l, s, t, m, tmp, min; int bridge[M]; // which means the minimal stones at the position bool stone[M]; // initialize for (int i = 0; i < M; ++i) { stone[i] = false; bridge[i] = INT_MAX; } // read data scanf("%d", &l); scanf("%d%d%d", &s, &t, &m); for (int i = 0; i < m; ++i) { scanf("%d", &tmp); stone[tmp] = true; } // move first step for (int i = s; i <= t; ++i) { bridge[i] = stone[i] ? 1 : 0; } // next step is determined by all the previous step for (int i = t + 1; i <= l; ++i) { min = INT_MAX; for (int j = s; j <= t; ++j) { if (i - j < 0) continue; if (min > bridge[i - j]) min = bridge[i - j]; } if (stone[i] && min != INT_MAX) min += 1; bridge[i] = min; } // output the final step which determined by all the previous step min = INT_MAX; for (int i = l - t + 1; i <= l ; ++i) { if (min > bridge[i]) min = bridge[i]; } printf("%d\n", min); return 0; }
#include<stdio.h> int min=110; //最多跳110步 int b[111]={0}; //每一次成功走过桥头, 走过的索引下标值为1 int L, S, T, M; int i=0, j=0; int a[111]={0}; //如果有石子对应索引值为1 void travel( int L, int present, int S, int T) //S最小跳跃距离,T最大跳跃距离 { int s=S, t=T; b[present]=1; // if(present >= L) //present当前位置, L最终位置 { int count=0, i=0; for(i=0; i <= L; i++) { if(b[i]==a[i]) { count++; } } if(present == L) count--; if(min > count) { min=count; } return ; } while(s <= t) //改变跳跃的距离 { travel( L ,present+s, S, T); s++; } b[present] = 0; return ; } int main(void) { scanf("%d%d%d%d", &L, &S, &T, &M); while(M--) { scanf("%d", &i); //如果有石子对应索引值为1 a[i-1]=1; //第n个石子下标为n-1 } travel( L, 0, S, T); printf("\n%d", min-1); return 0; }