fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  6.  
  7.  
  8. typedef long long ll;
  9.  
  10. ll R,C;
  11. char g[21][21];
  12. ll dx[] = {1,0,-1,0};
  13. ll dy[] = {0,1,0,-1};
  14. ll STEP;
  15. double P,Q;
  16. ll cnt[21][21];
  17. double maxx,ans;
  18.  
  19.  
  20. bool check(ll x , ll y){
  21. if(x<0 || x>=R || y<0 || y>=C)
  22. return 0;
  23. return 1;
  24. }
  25.  
  26. void go(ll x, ll y , ll l){
  27. //cout << "here" << endl;
  28.  
  29. double prd = 1.0;
  30. double prob = P;
  31. if(g[x][y] == '.')
  32. prob = Q;
  33. ll i,j,k;
  34. for(i = 0 ; i < cnt[x][y] ; i++)
  35. prd *= (1.0-prob);
  36. prd *= prob;
  37. ans += prd;
  38. cnt[x][y]++;
  39.  
  40. if(l < STEP){
  41. for(i = 0 ; i < 4 ; i++){
  42. if(check(x+dx[i],y+dy[i]))
  43. go(x+dx[i],y+dy[i],l+1);
  44. }
  45. }else
  46. maxx = max(ans,maxx);
  47. cnt[x][y]--;
  48. ans -= prd;
  49. }
  50.  
  51.  
  52.  
  53. int main(){
  54. boost;
  55. ll T,N,i,j,k,Rs,Cs;
  56. cin >> T;
  57. ll ind;
  58. for(ind = 1 ; ind <= T ; ind++){
  59. cin >> R >> C >> Rs >> Cs >> STEP ;
  60. cin >> P >> Q;
  61. for(i = 0 ; i < R ; i++){
  62. for(j = 0 ; j < C ; j++)
  63. cnt[i][j] = 0;
  64. }
  65. for(i = 0 ; i < R ; i++){
  66. for(j = 0 ; j < C ; j++){
  67. cin >> g[i][j];
  68. }
  69. }
  70. // cout << "here" << endl;
  71. maxx = ans = 0.0;
  72. if(STEP > 0){
  73. for(i = 0 ; i < 4 ; i++){
  74. if(check(Rs+dx[i],Cs+dy[i]))
  75. go(Rs+dx[i],Cs+dy[i],1);
  76. }
  77. }
  78. printf("Case #%lld: %.10lf\n",ind,maxx);
  79. }
  80. }
Success #stdin #stdout 0s 3476KB
stdin
2
4 4 0 0 5
0.8000 0.2000
. . . .
. . . .
. . A .
. A . A
10 10 9 1 4
0.6121 0.1000
. . A A . . . . . .
A . . . . . . . . .
. . A . . . . A . .
. . . A A . . . . .
. A A A . . . . . A
A . A A . . . . A .
. A . . . . . A . .
. . . . A A . . . .
. . A . . . A . . A
. . . . A . . A . .
stdout
Case #1: 1.6000000000
Case #2: 1.0495335900