Advertisement
royalsflush

GCJ13 A-small-large

Apr 14th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int n;
  6. char b[6][6];
  7.  
  8. bool checkWinInLine(char player, int init[], int mv[]) {
  9.     int i=init[0], j=init[1];
  10.  
  11.     for (int k=0; k<4; k++) {
  12.         if (b[i][j]!='T' && b[i][j]!=player)
  13.             return false;
  14.    
  15.         i+=mv[0], j+=mv[1];
  16.     }
  17.  
  18.     return true;
  19. }
  20.  
  21. bool checkComplete() {
  22.     for (int i=0; i<4; i++)
  23.         for (int j=0; j<4; j++)
  24.             if (b[i][j]=='.')
  25.                 return false;
  26.     return true;
  27. }
  28.  
  29. bool checkWin(char player) {
  30.     int init[][2] = {{0,0}, {0,1}, {0,2}, {0,3},
  31.             {0,0}, {1,0}, {2,0}, {3,0},
  32.             {0,0},{3,0}};
  33.     int mv[][2] = {{1,0},{1,0},{1,0},{1,0},
  34.             {0,1},{0,1},{0,1},{0,1},
  35.             {1,1},{-1,1}};
  36.  
  37.     for (int i=0; i<10; i++)
  38.         if (checkWinInLine(player,init[i],mv[i]))
  39.             return true;
  40.     return false;
  41. }
  42.  
  43.  
  44. int main() {
  45.     scanf("%d", &n);
  46.  
  47.     for (int i=1; i<=n; i++) {
  48.         for (int j=0; j<4; j++)
  49.             scanf(" %s", b[j]);
  50.  
  51.         printf("Case #%d: ", i);
  52.        
  53.         if (checkWin('X'))
  54.             printf("X won");
  55.         else if (checkWin('O'))
  56.             printf("O won");
  57.         else if (checkComplete())
  58.             printf("Draw");
  59.         else
  60.             printf("Game has not completed");
  61.  
  62.         printf("\n");
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement