题目链接:
题目大意:
有一天桐桐做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字K;(0≤Ki≤N)。电梯只有四 个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki (K1=3,K2=3,…),从一楼开始。在一楼,按“上,”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮 呢?
解题思路:
直接BFS即可
这里dijkstra也可以
1 #include2 using namespace std; 3 const int maxn = 1000 + 10; 4 const int INF = 0x3f3f3f3f; 5 int c[maxn]; 6 bool vis[maxn]; 7 struct node 8 { 9 int x, step;10 node(int x, int step):x(x), step(step){}11 };12 int n;13 void BFS(int s, int t)14 {15 memset(vis, 0, sizeof(vis));16 vis[s] = 1;17 queue q;18 q.push(node(s, 0));19 while(!q.empty())20 {21 node now = q.front();22 //cout< <<" "<< now.step< 0 && !vis[x])q.push(node(x, now.step + 1)), vis[x] =1;33 }34 cout<<"-1"< > n && n)39 {40 int a, b;41 cin >> a >> b;42 for(int i = 1; i <= n; i++)cin >> c[i];43 BFS(a, b);44 }45 return 0;46 }