fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <assert.h>
  7. #include <map>
  8. #include <queue>
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <string>
  13. #include <sstream>
  14. #include <math.h>
  15. #include <bitset>
  16. using namespace std;
  17.  
  18. typedef long long ll;
  19. #define next dsifhsdkjf
  20. #define REP(i,a,b) for(int i=(a);i<(b); ++i)
  21. #define REPD(i,a,b)for(int i=(b)-1;i>=a;--i)
  22. #define _(a,b) memset((a), (b), sizeof(a))
  23. #define size(a) ((int)a.size())
  24. #define all(a) a.begin(), a.end()
  25. #define pb push_back
  26. #define mp make_pair
  27. #define sqr(a) ((a)*(a))
  28.  
  29.  
  30. double d[5001][10][10];
  31.  
  32.  
  33. int main() {
  34. #ifdef air
  35. freopen("input.txt", "r", stdin);
  36. freopen("output.txt","w", stdout);
  37. #endif
  38. int dx[4] = {+1, -1, 0, 0};
  39. int dy[4] = {0, 0, +1, -1};
  40. int test;
  41. scanf("%d", &test);
  42. REP(t, 0, test) {
  43. int n, m, x1, y1, x2, y2, P;
  44. cin >> n >> m >> x1 >> y1 >> x2 >> y2 >> P;
  45. _(d, 0);
  46. d[0][x1][y1]=1;
  47. double res=0;
  48. REP(it,0,P) REP(i,0,n) REP(j,0,m){
  49. double c=d[it][i][j];
  50. if (c < 1e-10) continue;
  51. int cnt = 0;
  52. if (i==x2 && y2==j) {
  53. res += c;
  54. continue;
  55. }
  56. REP(dir, 0, 4) {
  57. int nx = i + dx[dir];
  58. int ny = j + dy[dir];
  59. if (nx < 0 || nx >= n || ny < 0 || ny >= m)
  60. continue;
  61. ++cnt;
  62. }
  63. double pr = 1.0 / cnt;
  64. REP(dir, 0, 4) {
  65. int nx = i + dx[dir];
  66. int ny = j + dy[dir];
  67. if (nx < 0 || nx >= n || ny < 0 || ny >= m)
  68. continue;
  69. d[it+1][nx][ny] += pr*c;
  70. ++cnt;
  71. }
  72. }
  73. res += d[P][x2][y2];
  74. printf("%.10lf\n",res);
  75.  
  76. }
  77. #ifdef air
  78. printf("\n\n%.10lf\n", clock()*1e-3);
  79. #endif
  80.  
  81. }
  82.  
Success #stdin #stdout 0.02s 6628KB
stdin
Standard input is empty
stdout
Standard output is empty