fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. template <typename T>
  7. using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
  8.  
  9. #define endl '\n'
  10. #define fi first
  11. #define se second
  12. #define pb push_back
  13. #define mpr make_pair
  14. #define sz(a) a.size()
  15. #define all(a) a.begin(),a.end()
  16. #define ms(a,n) memset(a , n , sizeof(a))
  17. #define FOR(i,a,b) for(int i = a ; i <= b ;i++)
  18. #define RFOR(i,a,b) for(int i = b ; i >= a ; i--)
  19. #define fact_io() ios::sync_with_stdio(NULL);cout.tie(NULL);
  20. #define sz(a) a.size()
  21.  
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef long double ld;
  25. typedef pair<ll,ll>pl;
  26. typedef vector<ll>vll;
  27. typedef vector<int>vii;
  28. typedef pair<int,int>pi;
  29. const int MOD = 1e9 + 7;
  30.  
  31. inline ll gcd(ll a , ll b) {return b == 0 ? a : gcd(b , a % b);}
  32. inline ll lcm(ll a , ll b) {return a / gcd(a , b) * b ;}
  33. struct node
  34. {
  35. int x , y , cnt;
  36. };
  37. int a[15][15];
  38. int dx[] = {1 , 0 , 0};
  39. int dy[] = {0 , 1 , -1};
  40. int BFS(int s , int t, int x , int y)
  41. {
  42. queue<node>q;
  43. ms(a , 0);
  44. q.push({s , t , 0});
  45. a[s][t] = 1;
  46. while(!q.empty())
  47. {
  48. node tmp = q.front();
  49. q.pop();
  50. int i = tmp.x , j = tmp.y , cnt = tmp.cnt;
  51. if(i == x && j == y)
  52. {
  53. return cnt;
  54. }
  55. if(i >= 5)
  56. {
  57. for(int k = 0 ; k < 3 ; k++)
  58. {
  59. int i1 = i + dx[k];
  60. int j1 = j + dy[k];
  61. if(i1 < 0 || j1 < 0 || i1 > 9 || j1 > 8 || a[i1][j1] == 1) continue;
  62. a[i1][j1] = 1;
  63. q.push({i1 , j1 , cnt + 1});
  64. }
  65. }
  66. else
  67. {
  68. q.push({i + 1 , j , cnt + 1});
  69. }
  70. }
  71. return -1;
  72. }
  73. int main()
  74. {
  75. ios::sync_with_stdio(NULL);cout.tie(NULL);cin.tie(NULL);
  76. int x , y ; cin >> x>> y;
  77. int b[6];
  78. b[1] = BFS(3 , 0 , x , y);
  79. b[2] = BFS(3 , 2 , x , y);
  80. b[3] = BFS(3 , 4 , x , y);
  81. b[4] = BFS(3 , 6 , x , y);
  82. b[5] = BFS(3 , 8 , x , y);
  83. int ans = 100;
  84. char c;
  85. for(int i = 1 ; i <= 5 ; i++)
  86. {
  87. if(b[i] < ans && b[i] != -1)
  88. {
  89. ans = b[i];
  90. c = 'A' + i - 1;
  91. }
  92. }
  93. if(ans == 100) cout << -1;
  94. else
  95. {
  96. cout << ans << endl;
  97. cout << c;
  98. }
  99. return 0;
  100. }
Success #stdin #stdout 0.01s 5360KB
stdin
Standard input is empty
stdout
-1