Batman has defeated Hugo Strange and now he is getting bored as there is too much peace in Gotham City.
To pass his time he used to play 4 X 4 field Tick-Tac-Toe. One Day While playing, Commissioner Gordan Called Batman and he had to leave immediately so he didn't finish the game. It was Batman's turn in the game when they left it. Find Out whether Batman could have won the game by making a single turn or not.
The rules of tic-tac-toe are as follows.
Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first gets three of his signs in a row next to each other (horizontal, vertical or diagonal).
Example:
Input: board = {{"xx.."},{".oo."},{"x..."},{"oox."}}
Output: YES
Approach
C++
#include <bits/stdc++.h>using namespace std;vector<string> board;bool row(){bool flag = false;for (int i = 0; i < 4; i++){for (int j = 0; j <= 1; j++){if (board[i][j] == 'x' &&board[i][j + 1] == 'x' &&board[i][j + 2] == '.'){flag = 1;break;}if (board[i][j] == '.' &&board[i][j + 1] == 'x' &&board[i][j + 2] == 'x'){flag = 1;break;}if (board[i][j] == 'x' &&board[i][j + 1] == '.' &&board[i][j + 2] == 'x'){flag = 1;break;}}}if (flag == 1)return true;return false;}bool col(){bool flag = false;for (int j = 0; j < 4; j++){for (int i = 0; i <= 1; i++){if (board[i][j] == 'x' &&board[i + 1][j] == 'x' &&board[i + 2][j] == '.'){flag = 1;break;}if (board[i][j] == 'x' &&board[i + 1][j] == '.' &&board[i + 2][j] == 'x'){flag = 1;break;}if (board[i][j] == '.' &&board[i + 1][j] == 'x' &&board[i + 2][j] == 'x'){flag = 1;break;}}}if (flag == 1)return true;return false;}bool diagonal(){bool flag = false;for (int i = 0; i <= 1; i++){for (int j = 0; j <= 1; j++){if (board[i][j] == 'x' &&board[i + 1][j + 1] == 'x' &&board[i + 2][j + 2] == '.'){flag = 1;break;}if (board[i][j] == '.' &&board[i + 1][j + 1] == 'x' &&board[i + 2][j + 2] == 'x'){flag = 1;break;}if (board[i][j] == 'x' &&board[i + 1][j + 1] == '.' &&board[i + 2][j + 2] == 'x'){flag = 1;break;}if (board[i + 2][j] == 'x' &&board[i + 1][j + 1] == 'x' &&board[i][j + 2] == '.'){flag = 1;break;}if (board[i + 2][j] == 'x' &&board[i + 1][j + 1] == '.' &&board[i][j + 2] == 'x'){flag = 1;break;}if (board[i + 2][j] == '.' &&board[i + 1][j + 1] == 'x' &&board[i][j + 2] == 'x'){flag = 1;break;}}}if (flag == 1)return true;return false;}int main(){board = {{"xx.."},{".oo."},{"x..."},{"oox."}};bool ans1 = row(), ans2 = col(), ans3 = diagonal();if (ans1 == true || ans2 == true || ans3 == true)cout << "YES\n";elsecout << "NO\n";return 0;}
No comments:
Post a Comment