Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <cstdio>
  2. #include <queue>
  3. #define MAXN 1500
  4.  
  5. typedef struct elem{
  6. int i,j,d;
  7. } elem;
  8.  
  9. using namespace std;
  10.  
  11. bool used[MAXN][MAXN];
  12.  
  13. int main() {
  14. freopen("input.txt","r",stdin);
  15. freopen("output.txt","w",stdout);
  16. int n;
  17. scanf("%d \n", &n);
  18. bool g[n][n];
  19. for (int i = 0; i < n; ++i){
  20. for (int j = 0; j < n; ++j){
  21. char c;
  22. scanf("%c", &c);
  23. g[i][j] = (c == '.');
  24. }
  25. scanf("\n");
  26. }
  27. queue <elem> q;
  28. q.push({0,0,0});
  29. used[0][0] = true;
  30. while (!q.empty()) {
  31. elem u = q.front();
  32. q.pop();
  33. if ((u.i == n - 1) && (u.j == n - 1)) {
  34. printf("%d", u.d);
  35. return 0;
  36. }
  37. if ((u.i > 0) && (g[u.i - 1][u.j]) && !(used[u.i - 1][u.j]) && (g[u.i - 1][u.j])) {
  38. used[u.i - 1][u.j] = true;
  39. q.push({u.i - 1, u.j, u.d + 1});
  40. }
  41. if ((u.i < n-1) && (g[u.i + 1][u.j]) && !(used[u.i + 1][u.j]) && (g[u.i + 1][u.j])) {
  42. used[u.i + 1][u.j] = true;
  43. q.push({u.i + 1, u.j, u.d + 1});
  44. }
  45. if ((u.j > 0) && (g[u.i][u.j - 1]) && !(used[u.i][u.j - 1]) && (g[u.i][u.j - 1])) {
  46. used[u.i][u.j - 1] = true;
  47. q.push({u.i, u.j - 1, u.d + 1});
  48. }
  49. if ((u.j < n-1) && (g[u.i][u.j + 1]) && !(used[u.i][u.j + 1]) && (g[u.i][u.j + 1])) {
  50. used[u.i][u.j + 1] = true;
  51. q.push({u.i, u.j + 1, u.d + 1});
  52. }
  53. }
  54. printf("-1");
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement