fork download
  1. #include<bits/stdc++.h> //Mr_Mandal
  2.  
  3. #define READ(f) freopen(f, "r", stdin)
  4. #define WRITE(f) freopen(f, "w", stdout)
  5. #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)
  6.  
  7. using namespace std;
  8.  
  9. //Extracting floor number from room number
  10. int floorNumber(string roomNumber)
  11. {
  12. return int(roomNumber[4]) - 48;
  13. }
  14.  
  15. //Calculating minimum needed time between two rooms
  16. int calculateLiftTime(string roomNum1, string roomNum2)
  17. {
  18. int ans = 0;
  19.  
  20. int floorNum1 = floorNumber(roomNum1);
  21. int floorNum2 = floorNumber(roomNum2);
  22.  
  23. //When both rooms are in the same Building
  24. if(roomNum1[0] == roomNum2[0])
  25. {
  26. ans = abs(floorNum1 - floorNum2);
  27. }
  28.  
  29. //When rooms are in different buildings
  30. else
  31. {
  32. int timeWithBridge = 0;
  33. int timeWithPlaaza = 0;
  34.  
  35. //Calculation if Baba takes the bridge on 5th floor
  36. timeWithBridge += abs(5-floorNum1);
  37. timeWithBridge += abs(5-floorNum2);
  38.  
  39. //Calculation if Baba takes the plaaza on 1st floor
  40. timeWithPlaaza += abs(1-floorNum1);
  41. timeWithPlaaza += abs(1-floorNum2);
  42.  
  43. //Taking the minimum time as Baba is Efficient
  44. ans = min(timeWithBridge, timeWithPlaaza);
  45. }
  46.  
  47. return ans;
  48. }
  49.  
  50. void solve()
  51. {
  52. int n;
  53. cin >> n;
  54. cin.ignore(); //flushing End Of Line
  55.  
  56. string firstRoom;
  57. getline(cin, firstRoom);
  58. n--; //Decreasing total class number by 1
  59.  
  60. int ans = 0; //Total Time
  61.  
  62. //Adding the time to getting into the first classroom
  63. ans += abs(1 - floorNumber(firstRoom));
  64.  
  65. string prevRoom = firstRoom;
  66.  
  67. while(n--)
  68. {
  69. string currRoom;
  70. getline(cin, currRoom);
  71.  
  72. ans += calculateLiftTime(prevRoom, currRoom);
  73.  
  74. //Keeping a track of previous classroom number
  75. prevRoom = currRoom;
  76. }
  77.  
  78. //Adding the time to getting down first floor
  79. ans += abs(1 - floorNumber(prevRoom));
  80.  
  81. cout<<ans<<"\n";
  82. }
  83.  
  84. int main()
  85. {
  86. int t;
  87. cin >> t;
  88.  
  89. while(t--)
  90. solve();
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.01s 5360KB
stdin
2
3
SAC 205
SAC 401
SAC 302
5
SAC 305
NAC 401
NAC 205
SAC 303
SAC 401
stdout
6
14