fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define uniq(x) x.erase(unique(x.begin(),x.end()), x.end()) //Unique value find from vector
  4. #define upper(arr,n,fixed) upper_bound(arr,arr+n,fixed)-arr //Upper value search;
  5. #define lower(arr,n,fixed) upper_bound(arr,arr+n,fixed)-arr //Lower value search;
  6. #define max3(a,b,c) max(max(a,b),c)//maximum value find three value;
  7. #define min3(a,b,c) min(min(a,b),c)//minimum value find three value;
  8. #define rep(i,intial,n) for(int i=intial; i<(n) ; i++)
  9. #define REP(i,intial,n) for(int i=intial; i<=(n) ; i++)
  10. #define PI acos(-1.0)//PI Calculation
  11. #define LL long long
  12. #define MP make_pair
  13. #define INF_MAX 2147483647
  14. #define INF_MIN -2147483647
  15. #define MX 1000005
  16. #define MOD 1000000007
  17. template<typename T> T POW(T b,T p) //Pow calculation
  18. {
  19. T r=1;
  20. while(p)
  21. {
  22. if(p&1)r=(r*b);
  23. b=(b*b);
  24. p>>=1;
  25. }
  26. return r;
  27. }
  28.  
  29. LL big_mod(LL n, LL p)
  30. {
  31. if(p==0) return 1;
  32. if(!(p&1))
  33. {
  34. LL r = big_mod(n,p/2) % MOD;
  35. return ( (r%MOD) * (r%MOD) ) % MOD;
  36. }
  37. else return ( ( n%MOD) * (big_mod(n,p-1) %MOD)) % MOD;
  38. }
  39. long long gcd(long long a, long long b)
  40. {
  41. while (b != 0)
  42. {
  43. long long t = a % b;
  44. a = b;
  45. b = t;
  46. }
  47. return a;
  48. }
  49.  
  50. //||--------------------------->||Main_Code_Start_From_Here||<---------------------------------||
  51. const int dx[4]= {1,1,-1,-1};
  52. const int dy[4]= {1,-1,1,-1};
  53.  
  54. int main()
  55. {
  56. //freopen("a.in", "r", stdin);
  57. //freopen("a.out", "w", stdout);
  58. int r1,r2,c1,c2,dis[10][10];
  59. while(cin>>r1>>c1>>r2>>c2)
  60. {
  61. rep(i,1,9)
  62. {
  63. rep(j,1,9)
  64. {
  65. dis[i][j]=100;
  66. }
  67. }
  68. queue<pair<int,int>> Q;
  69. Q.push({r1,c1});
  70. dis[r1][c1]=0;
  71. while(!Q.empty())
  72. {
  73. auto current=Q.front();
  74. Q.pop();
  75. int x=current.first;
  76. int y=current.second;
  77. int c=dis[x][y]+1;
  78. //cout<<"x = "<<x<<" y = "<<y<<" c = "<<c<<endl<<endl<<endl<<endl;
  79. rep(i,0,4)
  80. {
  81. int xx=x;
  82. int yy=y;
  83. while(true)
  84. {
  85. xx+=dx[i];
  86. yy+=dy[i];
  87. if(!(xx>=1 and xx<=8 and yy>=1 and yy<=8)) break;
  88. //cout<<"xx = "<<xx<<" yy = "<<yy<<" i = "<<i<<endl;
  89. if(dis[xx][yy]==100)
  90. {
  91. dis[xx][yy]=c;
  92. Q.push({xx,yy});
  93. }
  94. }
  95. }
  96. }
  97.  
  98. /* rep(i,1,9)
  99.   {
  100.   rep(j,1,9)
  101.   {
  102.   cout<<dis[i][j]<<" ";
  103.   }
  104.   cout<<endl;
  105.   }*/
  106.  
  107. if(dis[r2][c2]==100) cout<<"-1"<<endl;
  108. else cout<<dis[r2][c2]<<endl;
  109. }
  110.  
  111. }
  112.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty