Advertisement
HjHimansh

Kavita ma'am - 1st question

Sep 13th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. unordered_map<string, int> memo;
  11.  
  12. // returns {0, 1} as per if A or B wins.
  13. int solve(int n, int currPlayer) {
  14. string key = to_string(n) + "|" + to_string(currPlayer);
  15.  
  16. if(memo.count(key)) return memo[key];
  17.  
  18. if(n==0) {
  19. // currPlayer loses
  20. memo[key] = !currPlayer;
  21. return !currPlayer;
  22. }
  23. int option1 = !currPlayer, option2 = !currPlayer, option3 = !currPlayer;
  24. if (n>=1) {
  25. // currPlayer can take 1 item
  26. option1 = solve(n-1, !currPlayer);
  27. }
  28.  
  29. if (n>=2) {
  30. option2 = solve(n-2, !currPlayer);
  31. }
  32.  
  33. if (n>=4) {
  34. option3 = solve(n-4, !currPlayer);
  35. }
  36.  
  37. if(option1 == currPlayer || option2 == currPlayer || option3 == currPlayer) {
  38. memo[key] = currPlayer;
  39. }
  40. else {
  41. memo[key] = !currPlayer;
  42. return !currPlayer;
  43. }
  44.  
  45. return memo[key];
  46. }
  47.  
  48. int main() {
  49. int T; cin >> T;
  50.  
  51. while(T--) {
  52. int n; cin >> n;
  53. int answer = solve(n, 0);
  54. if(answer == 0) cout << "A" << endl;
  55. else cout << "B" << endl;
  56. }
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement