杭电acm1548 一直wrong answer
#include<iostream>usingnamespacestd;intb[201],A,B,n,d[2]={-1,1};structnode{intx,y,t;}...
#include <iostream>
using namespace std;
int b[201],A,B,n,d[2]={-1,1};
struct node
{
int x,y,t;
}a[10000];
int fbs()
{
if(A==B)
return 0;
a[0].x=A; a[0].t=0; a[0].y=b[A];
b[A]=0;
int h=0,q=1;
while(h<q)
{
for(int i=0;i<=1;i++)
{
int xx=a[h].x+d[i]*a[h].y;
if(xx<1||xx>n)
continue;
if(b[xx]==0)
continue;
a[q].x=xx; a[q].y=b[xx]; a[q].t=a[h].t+1;
if(xx==B)
return a[q].t;
b[xx]=0;
q++;
}
h++;
}
return -1;
}
void main()
{
while(cin>>n,n)
{
cin>>A>>B;
for(int i=1;i<=n;i++)
cin>>b[i];
cout<<fbs()<<endl;
}
}
Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3 展开
using namespace std;
int b[201],A,B,n,d[2]={-1,1};
struct node
{
int x,y,t;
}a[10000];
int fbs()
{
if(A==B)
return 0;
a[0].x=A; a[0].t=0; a[0].y=b[A];
b[A]=0;
int h=0,q=1;
while(h<q)
{
for(int i=0;i<=1;i++)
{
int xx=a[h].x+d[i]*a[h].y;
if(xx<1||xx>n)
continue;
if(b[xx]==0)
continue;
a[q].x=xx; a[q].y=b[xx]; a[q].t=a[h].t+1;
if(xx==B)
return a[q].t;
b[xx]=0;
q++;
}
h++;
}
return -1;
}
void main()
{
while(cin>>n,n)
{
cin>>A>>B;
for(int i=1;i<=n;i++)
cin>>b[i];
cout<<fbs()<<endl;
}
}
Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3 展开
展开全部
程序不符合题目要求。
Input
The input consists of several test cases.,
指输入包括多个测试用例。
Output
For each case of the input output a interger,
指每个测试用例输出一个数字。
这从它的输入样例上可以看出:
Sample Input
5 1 5
3 3 1 2 5
0 // 这个 0 就是输入的第二组数据
也就是说,一次输入全部测试用例,然后一次输出多个结果。
你的程序是输入一个测试用例输出一个结果,不合题意,结果肯定不对。
Input
The input consists of several test cases.,
指输入包括多个测试用例。
Output
For each case of the input output a interger,
指每个测试用例输出一个数字。
这从它的输入样例上可以看出:
Sample Input
5 1 5
3 3 1 2 5
0 // 这个 0 就是输入的第二组数据
也就是说,一次输入全部测试用例,然后一次输出多个结果。
你的程序是输入一个测试用例输出一个结果,不合题意,结果肯定不对。
更多追问追答
追问
应该不是这个问题吧,我试了一下别人的AC代码,也是输入一个测试用例输出一个结果
追答
能否把别人的AC代码发上来,我比较一下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询