fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF = 9999999;
  4. int dist[11][11];
  5. bool vis[11][11];
  6. int dir[8][2] = {{-2,-1},{2,-1},{-2,1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};
  7. #define mp make_pair
  8. void bfs(int x, int y)
  9. {
  10. memset(vis, false, sizeof(vis));
  11. queue<pair<int, int> >q;
  12. q.push(mp(x, y));
  13. dist[x][y] = 0;
  14. while(!q.empty()){
  15. pair<int, int> t = q.front();
  16. int u = t.first, v = t.second;
  17. q.pop();
  18. if (vis[u][v]) continue;
  19. vis[u][v] = true;
  20. for(int i = 0; i<8; i++){
  21. int xx = u + dir[i][0];
  22. int yy = v + dir[i][1];
  23. if (xx>0 && xx < 11 && yy>0 && yy<11){
  24. dist[xx][yy] = min(dist[xx][yy], dist[u][v]+3);
  25. q.push(mp(xx, yy));
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int t;
  34. cin >> t;
  35. while (t--){
  36. int x,y,r,s;
  37. for (int i = 0; i<11; i++)
  38. for (int j = 0; j<11; j++)
  39. dist[i][j] = INF;
  40. cin >> x >> y >> r >> s;
  41. ++x, ++y, ++r, ++s;
  42. bfs(x, y);
  43. cout << dist[r][s] << endl;
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 3280KB
stdin
3
5 6 6 2
8 5 2 3
9 8 0 9
stdout
9
12
18