fork(2) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int N, M, K, s,e;
  5. char str[100 + 1][100 + 1];
  6.  
  7. int rc[4] = {1, -1, 0, 0 };
  8. int cc[4] = {0, 0, 1, -1 };
  9. bool vis[101][101];
  10. bool possible = false;
  11.  
  12. bool valid(int x, int y, int ix)
  13. {
  14. if(x + rc[ix] < 0 || x + rc[ix] >= N || y + cc[ix] < 0 || y + cc[ix] >= M || str[x + rc[ix] ][y + cc[ix]] == '*') return false;
  15. return true;
  16. }
  17.  
  18. void dfs(int i, int j, int K)
  19. {
  20. if(possible) return;
  21. if(i == s && j == e && K <= 0) { possible = true; return;}
  22. if(vis[i][j]) return;
  23.  
  24. vis[i][j] = true;
  25. for(int m = 0; m < 4; m++)
  26. if(valid(i, j, m)) dfs(i + rc[m], j + cc[m], K - 1);
  27. vis[i][j] = false;
  28. return;
  29. }
  30.  
  31. int main()
  32. {
  33. scanf("%d %d",&N,&M);
  34. scanf("%d",&K);
  35. scanf("%d %d\n",&s,&e);
  36.  
  37. for(int i = 0; i < N; i++)
  38. {
  39. for(int j = 0; j < M; j++)
  40. {
  41. int c ;
  42. c = getchar();
  43. if(c == ' ') c = getchar();
  44. str[i][j] = c;
  45. }
  46. str[i][M] = '\0';
  47. getchar();
  48. }
  49. s--; e--;
  50. if(str[s][e] == '*' || K > N * M) {printf("NO\n"); return 0;}
  51. if(K == 0) {printf("YES\n"); return 0;}
  52. dfs(s, e, K);
  53. if(possible) printf("YES\n");
  54. else printf("NO\n");
  55. }
Success #stdin #stdout 0s 16080KB
stdin
5 5
14
1 2
. . . * *
* . . . *
* . . . .
. * . . .
. * . . *
stdout
YES