Here is a possible solution in C++:\n\ncpp\n#include \<iostream>\n#include \<vector>\n#include \<string>\n\nusing namespace std;\n\nbool checkLine(const vector\<string> &board, int r, int c, int dr, int dc) {\n char symbol = board[r][c];\n for (int i = 0; i < 3; i++) {\n if (board[r][c] != symbol) {\n return false;\n }\n r += dr;\n c += dc;\n }\n return true;\n}\n\nbool checkWin(const vector\<string> &board) {\n // Check rows\n for (int r = 0; r < 3; r++) {\n if (checkLine(board, r, 0, 0, 1)) {\n return true;\n }\n }\n // Check columns\n for (int c = 0; c < 3; c++) {\n if (checkLine(board, 0, c, 1, 0)) {\n return true;\n }\n }\n // Check diagonals\n if (checkLine(board, 0, 0, 1, 1) || checkLine(board, 0, 2, 1, -1)) {\n return true;\n }\n return false;\n}\n\nint main() {\n int t;\n cin >> t;\n while (t--) {\n vector\<string> board(3);\n for (int i = 0; i < 3; i++) {\n cin >> board[i];\n }\n bool hasWin = checkWin(board);\n if (hasWin) {\n cout << "yes" << endl;\n } else {\n cout << "no" << endl;\n }\n }\n return 0;\n}\n\n\nExplanation:\n1. The checkLine function checks if there are three consecutive symbols in a row, column, or diagonal starting from a given position.\n2. The checkWin function checks if any of the rows, columns, or diagonals have three consecutive symbols of the same player.\n3. In the main function, we read the number of test cases t. For each test case, we read the game board and check if the second player (O) has won. We output "yes" if they have won, and "no" otherwise.


原文地址: https://www.cveoy.top/t/topic/pDLa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录