Advertisement
NHumme

Untitled

Nov 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. /*
  14. #define TASK "linkedmap"
  15.  
  16. int main() {
  17.  
  18. #ifdef _DEBUG
  19.     freopen("debug.in", "r", stdin);
  20.     freopen("debug.out", "w", stdout);
  21. #else
  22.     freopen(TASK".in", "r", stdin);
  23.     freopen(TASK".out", "w", stdout);
  24. #endif // _DEBUG
  25.  
  26.     ios_base::sync_with_stdio(0);
  27.     cin.tie(0);
  28.     cout.tie(0);
  29.  
  30.  
  31.  
  32.     return 0;
  33. }
  34. */
  35.  
  36. char a[12][12];
  37. bool used[12][12];
  38. int kol = 0, n, m;
  39.  
  40. void dfs(int x, int y)
  41. {
  42.     used[x][y] = true;
  43.     if (a[x][y] == 'F')
  44.     {
  45.         kol++;
  46.         return;
  47.     }
  48.  
  49.     if (!used[x + 1][y] && a[x + 1][y] == '.' || a[x + 1][y] == 'F')
  50.         dfs(x + 1, y);
  51.  
  52.     if (!used[x - 1][y] && a[x - 1][y] == '.' || a[x - 1][y] == 'F')
  53.         dfs(x - 1, y);
  54.  
  55.     if (!used[x][y - 1] && a[x][y - 1] == '.' || a[x][y - 1] == 'F')
  56.         dfs(x, y - 1);
  57.  
  58.     if (!used[x][y + 1] && a[x][y + 1] == '.' || a[x][y + 1] == 'F')
  59.         dfs(x, y + 1);
  60.  
  61.     used[x][y] = false;
  62.     return;
  63. }
  64.  
  65. int main()
  66. {
  67.  
  68. #ifdef _DEBUG
  69.     freopen("debug.in", "r", stdin);
  70.     freopen("debug.out", "w", stdout);
  71. #else
  72.     freopen(TASK".in", "r", stdin);
  73.     freopen(TASK".out", "w", stdout);
  74. #endif // _DEBUG
  75.  
  76.     int i, k, c1, c2;
  77.     cin >> n >> m;
  78.     c1 = c2 = 0;
  79.     for (i = 1; i <= n; i++) {
  80.         for (k = 1; k <= m; k++)
  81.         {
  82.             cin >> a[i][k];
  83.             if (a[i][k] == 'S')
  84.             {
  85.                 c1 = i;
  86.                 c2 = k;
  87.             }
  88.         }
  89.     }
  90.     for (i = 0; i <= 11; i++) {
  91.         for (k = 1; k <= 11; k++)
  92.         {
  93.             if (i == 0 || i == n + 1 || k == 0 || k == m + 1) {
  94.                 used[i][k] = true;
  95.             }
  96.         }
  97.     }
  98.     if (c1 != 0)
  99.         dfs(c1, c2);
  100.     cout << kol;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement