fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int noway(int na[][2],int num,int nox,int noy){
  5. //this function returns true if position given by (nox,noy) is impassable else returns false.
  6. int i;
  7. if(nox<0 || noy<0)
  8. return 1;
  9. for(i=0;i<num;i++){
  10. if(na[i][0]==nox && na[i][1]==noy){
  11. return 1;
  12. }
  13. }
  14. return 0;
  15. }
  16.  
  17. char who(char p,int x, int y,int na[][2],int num){
  18. // first parameter is the person and next two parameters tells about his position.
  19.  
  20. //base case below :
  21. if(noway(na,num,x-1,y) && noway(na,num,x,y-1)){
  22. return p=='a'?'b':'a';
  23. }
  24. //base case ends.
  25.  
  26. int i,j;
  27. char t='.';
  28.  
  29. // this loop fixes 'x' and varies 'y' and check if there is any such cell for which the player p(first parameter in who function) wins. As soon as answer is received. It returns indicating the win of player p.
  30. for(j=y;!noway(na,num,x,j);j--){
  31. if((t=who(p=='a'?'b':'a',x,j,na,num))==p){
  32. return p;
  33. }
  34. }
  35.  
  36. //this second loop fixes 'y' and varies 'x' to check for the same as specified in above loop documentation.
  37. for(i=x;!noway(na,num,i,y);i--){
  38. if((t=who(p=='a'?'b':'a',i,y,na,num))==p){
  39. return p;
  40. }
  41. }
  42.  
  43.  
  44. // if above two loops returns nothing i.e there is no block if for which the player starts can win then it tells the function that this player p cannot win and hence returns other player as returned below.
  45. return p=='a'?'b':'a';
  46.  
  47. }
  48.  
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52. int n;
  53. cin >> n;
  54. while(n--){
  55. int na;
  56. cin >> na;
  57. int naa[na][2];
  58. int i,j;
  59.  
  60. //inputting impassable cells here.
  61. for(i=0;i<na;i++)
  62. cin >> naa[i][0] >> naa[i][1];
  63. //finish inputting impassable cells.
  64.  
  65.  
  66. int q;
  67. cin >> q;
  68.  
  69. while(q--){
  70. int q1,q2;
  71. // q1,q2 stores the position from where the game starts.
  72. cin >> q1 >> q2;
  73.  
  74. char res=who('a',q1,q2,naa,na);
  75. //see above for method 'who' parameter definition. 'a' and 'b' refers here to alice and bob ( their initials).
  76. if(res=='a')
  77. cout << "Alice" <<endl;
  78. else
  79. cout << "Bob" << endl;
  80. }
  81.  
  82. }
  83. return 0;
  84. }
Runtime error #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
Standard output is empty