Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct axis{
  4. int x,y,z;
  5. };
  6. queue <axis> q;
  7. int n;
  8. int x[] = {-1,0,1,0};
  9. int y[] = {0,-1,0,1};
  10. int m[1005][1005][4],X,Y,Z;
  11. int w[1005][1005];
  12. bool check(int x,int y){
  13. if(x < 0 || y < 0 || x >= n || y >= n) return 0;
  14. return 1;
  15. }
  16. int main()
  17. {
  18. // freopen("k.txt","r",stdin);
  19. scanf("%d",&n);
  20. int i,j;
  21. for(i=0;i<n;i++)
  22. {
  23. for(j=0;j<n;j++)
  24. {
  25. scanf("%d",&w[i][j]);
  26. }
  27. }
  28. q.push({0,0,2});
  29. q.push({0,0,3});
  30. while(!q.empty()){
  31.  
  32. axis t = q.front(); q.pop();
  33. if(t.x==n-1&&t.y==n-1) {printf("%d",m[t.x][t.y][t.z]);return 0;}
  34. X = t.x+x[t.z]; Y = t.y+y[t.z];
  35. if(check(X,Y) && !m[X][Y][t.z] && !w[X][Y])
  36. {
  37. q.push({X,Y,t.z});
  38. m[X][Y][t.z] = m[t.x][t.y][t.z]+1;
  39. }
  40. else {
  41. for(i=0;i<4;i++){
  42. X = t.x+x[i]; Y = t.y+y[i];
  43. if(check(X,Y) && !m[X][Y][i] && !w[X][Y]){
  44. q.push({X,Y,i});
  45. m[X][Y][i] = m[t.x][t.y][t.z]+1;
  46. }
  47. }
  48. }
  49. // printf("m[%d][%d][%d] = %d\n",t.x,t.y,t.z, m[t.x][t.y][t.z]);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement