Advertisement
Guest User

Untitled

a guest
Jan 16th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <queue>
  9. #include <map>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. float d[2][12][12] = {0};
  14. int l[4] = {0, -1, 0, 1};
  15. int r[4] = {1, 0, -1, 0};
  16. float c[12][12];
  17.  
  18. int main(){
  19.  
  20.     int t;
  21.     scanf("%d", &t);
  22.  
  23.     for (int t0 = 0; t0 < t; t0++){
  24.         int n, m, a, b, e, f, p;
  25.        
  26.         scanf("%d%d%d%d%d%d%d", &n, &m, &a, &b, &e, &f, &p);
  27.         a++;
  28.         b++;
  29.         e++;
  30.         f++;
  31.  
  32.         for (int i = 1; i <= n; i++)
  33.             for (int j = 1; j <= m; j++)
  34.                 d[0][i][j] = 0;
  35.  
  36.         d[0][a][b] = 1;
  37.  
  38.         for (int i = 1; i <= n; i++)
  39.             for (int j = 1; j <= m; j++){
  40.                 float cnt = 4;
  41.                 if (i == 1 || i == n)
  42.                     cnt--;
  43.                 if (j == 1  || j == m)
  44.                     cnt--;
  45.                 c[i][j] = 1.0 / cnt;
  46.             }
  47.        
  48.         for (int k = 1; k <= p; k++){
  49.             int kk = k & 1, nk = kk ^ 1;
  50.             d[kk][e][f] = d[nk][e][f];
  51.             for (int i = 1; i <= n; i++){
  52.                 for (int j = 1; j <= m; j++){
  53.                     if (i == e && j == f){
  54.                         continue;
  55.                     }
  56.                     for (int q = 0; q < 4; q++){
  57.                         int ni = i + l[q],
  58.                             nj = j + r[q];
  59.                         d[kk][ni][nj] += d[nk][i][j] * c[i][j];
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.  
  65.         printf("%0.8f\n", d[p & 1][e][f]);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement