HDU 4576 Robot 概率DP 水题

                      Robot

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3851    Accepted Submission(s): 1246


Problem Description
Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise.

HDU 4576 Robot 概率DP 水题


At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the robot. A command will make the robot walk some distance. Unfortunately the direction part on the remote control is broken, so for every command the robot will chose a direction(clockwise or anticlockwise) randomly with equal possibility, and then walk w cells forward.
Michael wants to know the possibility of the robot stopping in the cell that cell number >= l and <= r after m commands.
 

 

Input
There are multiple test cases. 
Each test case contains several lines.
The first line contains four integers: above mentioned n(1≤n≤200) ,m(0≤m≤1,000,000),l,r(1≤l≤r≤n).
Then m lines follow, each representing a command. A command is a integer w(1≤w≤100) representing the cell length the robot will walk for this command.  
The input end with n=0,m=0,l=0,r=0. You should not process this test case.
 

 

Output
For each test case in the input, you should output a line with the expected possibility. Output should be round to 4 digits after decimal points.
 

 

Sample Input
3 1 1 2
1
5 2 4 4
1
2
0 0 0 0
 

 

Sample Output
0.5000
0.2500
 

 

Source

 

 

 

 

 

题意:给出一个环,上面分成n个区域,分别编号为1~n,有一个指针,刚开始的时候指向1的位置

现在有m个操作,每一个操作输入一个w,指针转动w个单位,顺时针转,和逆时针转的概率都是0.5,

现在问m个操作后,指针指向区间[l,r]的概率是多少

 

dp[i][j]表示第i个命令后,指针指向区域j的概率

这里命令数m很大,而我们递推的时候只需要保存前面一个的状态,所以i%2,节省空间。

 

 

HDU 4576 Robot 概率DP 水题
 1 #include<cstdio>

 2 #include<cstring>

 3 

 4 using namespace std;

 5 

 6 double dp[2][210];

 7 

 8 int main()

 9 {

10     int n,m,l,r;

11     while(scanf("%d%d%d%d",&n,&m,&l,&r))

12     {

13         if(!n&&!m&&!l&&!r)

14             break;

15         memset(dp,0,sizeof dp);

16         dp[0][1]=1.0;

17         int w;

18         int a,b;

19         for(int i=1;i<=m;i++)

20         {

21             scanf("%d",&w);

22             for(int j=1;j<=n;j++)

23             {

24                 a=j-w;

25                 if(a<1)

26                     a+=n;

27                 b=j+w;

28                 if(b>n)

29                     b-=n;

30                 dp[i%2][j]=dp[(i-1)%2][a]*0.5+dp[(i-1)%2][b]*0.5;

31             }

32         }

33         double ans=0.0;

34         int mm=m%2;

35         for(int i=l;i<=r;i++)

36             ans+=dp[mm][i];

37 

38         printf("%.4f\n",ans);

39     }

40     return 0;

41 }
View Code

 

你可能感兴趣的:(robot)