fork download
  1. #include <cstdio>
  2. #include <vector>
  3. #include <deque>
  4. #include <queue>
  5. #include <map>
  6. #include <set>
  7. #include <algorithm>
  8. #include <functional>
  9. #include <numeric>
  10. #include <iostream>
  11. #include <string>
  12. #include <cmath>
  13.  
  14. using namespace std;
  15. typedef long long ll;
  16.  
  17. const int SIZE = 4;
  18.  
  19.  
  20. char readNext(int i, int j, char ch, vector<string> &cell){
  21. if (i < 0 || i >= SIZE || j < 0 || j >= SIZE){
  22. return 'z'; // fail
  23. }
  24. if (ch == 'T'){
  25. return cell[j][i];
  26. }
  27. else if (ch == cell[j][i] || cell[j][i] == 'T'){
  28. return ch;
  29. }
  30. else{
  31. return 'z';
  32. }
  33. }
  34.  
  35. string getAns(vector<string> &cell){
  36. string ret;
  37.  
  38. bool isFull = true;
  39. for(int i = 0; i < SIZE; ++i){
  40. for(int j = 0; j < SIZE; ++j){
  41. if (cell[j][i] == '.') isFull = false;
  42. }
  43. }
  44.  
  45. int di[] = {0, 1, 1, -1};
  46. int dj[] = {1, 0, 1, 1};
  47. for(int i = 0; i < SIZE; ++i){
  48. for(int j = 0; j < SIZE; ++j){
  49. for(int d = 0; d < 4; ++d){
  50. char ch = cell[j][i];
  51. // printf("i, j, d = %d, %d, %d\n", i, j, d);
  52. for(int rep = 0; rep < SIZE; ++rep){
  53. ch = readNext(i+di[d]*rep, j+dj[d]*rep, ch, cell);
  54. // printf(" ch = %c\n", ch);
  55. }
  56. if (ch == 'X'){
  57. return "X won";
  58. }
  59. else if (ch == 'O'){
  60. return "O won";
  61. }
  62. }
  63. }
  64. }
  65.  
  66. return isFull ? "Draw" : "Game has not completed";
  67. }
  68.  
  69. int main(void)
  70. {
  71. int T;
  72. cin >> T;
  73.  
  74.  
  75. for(int t = 0; t < T; ++t){
  76. vector<string> cell;
  77. for(int i = 0; i < SIZE; ++i){
  78. string str;
  79. cin >> str;
  80. cell.push_back(str);
  81. }
  82.  
  83. cout << "Case #" << (t+1) << ": " << getAns(cell) << endl;
  84. }
  85.  
  86. return 0;
  87. }
  88.  
Success #stdin #stdout 0s 3036KB
stdin
6
XXXT
....
OO..
....

XOXT
XXOO
OXOX
XXOO

XOX.
OX..
....
....

OOXX
OXXX
OX.T
O..O

XXXO
..O.
.O..
T...

OXXX
XO..
..O.
...O
stdout
Case #1: X won
Case #2: Draw
Case #3: Game has not completed
Case #4: O won
Case #5: O won
Case #6: O won