Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1304
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int tc;
- cin >> tc;
- for(int tr = 0; tr < tc; tr++){
- int n = 3;
- char m[n][n];
- string input[n];
- for(int i = 0; i < n; i++) cin >> input[i];
- for(int r = 0; r < n; r++){
- for(int c = 0; c < n; c++){
- m[r][c] = input[r][c];
- }
- }
- //COUNTING X, Y;
- int countX = 0;
- int countO = 0;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- if(m[i][j] == 'X') countX++;
- else if(m[i][j] == 'O') countO++;
- }
- }
- //ROW WIN CHECK X;
- int x_win = 0;
- int cnt_x = 0;
- for(int r = 0; r < n; r++){
- cnt_x = 0;
- for(int c = 0; c < n; c++){
- if(m[r][c] == 'X'){
- while(c < n){
- if(m[r][c] == 'X'){
- cnt_x++;
- }
- else{
- break;
- }
- if(cnt_x == 3){
- x_win++;
- }
- c++;
- }
- c--;
- }
- }
- cnt_x = 0;
- }
- //ROW WIN CHECK O;
- int O_win = 0;
- int cnt_O = 0;
- for(int r = 0; r < n; r++){
- for(int c = 0; c < n; c++){
- if(m[r][c] == 'O'){
- while(c < n){
- if(m[r][c] == 'O'){
- cnt_O++;
- }
- else{
- break;
- }
- if(cnt_O == 3){
- O_win++;
- }
- c++;
- }
- c--;
- }
- }
- cnt_O = 0;
- }
- //COLUMN WIN CHECK X;
- cnt_x = 0;
- for(int c = 0; c < n; c++){
- for(int r = 0; r < n; r++){
- if(m[r][c] == 'X'){
- while(c < n){
- if(m[r][c] == 'X'){
- cnt_x++;
- }
- else{
- break;
- }
- if(cnt_x == 3){
- x_win++;
- }
- r++;
- }
- r--;
- }
- }
- cnt_x = 0;
- }
- //COLUMN WIN CHECK O;
- cnt_O = 0;
- for(int c = 0; c < n; c++){
- for(int r = 0; r < n; r++){
- if(m[r][c] == 'O'){
- while(r < n){
- if(m[r][c] == 'O'){
- cnt_O++;
- }
- else{
- break;
- }
- if(cnt_O == 3){
- O_win++;
- }
- r++;
- }
- r--;
- }
- }
- cnt_O = 0;
- }
- //PRIMARY DIAG WIN CHECK X;
- cnt_x = 0;
- for(int i = 0; i < n; i++){
- if(m[i][i] == 'X'){
- while(i < n){
- if(m[i][i] == 'X'){
- cnt_x++;
- }
- else{
- break;
- }
- if(cnt_x == 3){
- x_win++;
- }
- i++;
- }
- i--;
- }
- cnt_x = 0;
- }
- //PRIMARY DIAG WIN CHECK O;
- cnt_O = 0;
- for(int i = 0; i < n; i++){
- if(m[i][i] == 'O'){
- while(i < n){
- if(m[i][i] == 'O'){
- cnt_O++;
- }
- else{
- break;
- }
- if(cnt_O == 3){
- O_win++;
- }
- i++;
- }
- i--;
- }
- cnt_O = 0;
- }
- //SECONDARY DIAG WIN CHECK X;
- cnt_x = 0;
- int j = n;
- for(int i = 0; i < n; i++){
- j--;
- if(m[i][j] == 'X'){
- while(i < n){
- if(m[i][j] == 'X'){
- cnt_x++;
- }
- else{
- break;
- }
- if(cnt_x == 3){
- x_win++;
- }
- i++;
- j--;
- }
- i--;
- j++;
- }
- cnt_x = 0;
- }
- //SECONDARY DIAG WIN CHECK O;
- cnt_O = 0;
- j = n;
- for(int i = 0; i < n; i++){
- j--;
- if(m[i][j] == 'O'){
- while(i < n){
- if(m[i][j] == 'O'){
- cnt_O++;
- }
- else{
- break;
- }
- if(cnt_O == 3){
- O_win++;
- }
- i++;
- j--;
- }
- i--;
- j++;
- }
- cnt_O = 0;
- }
- int flag = 1;
- if(countO == countX || (countO+1) == countX){
- flag = 1;
- if(x_win > 0 || O_win > 0){
- if((x_win == 1 && O_win == 0) || (x_win == 0 && O_win == 1)){
- if(x_win == 1){
- if(countX > countO){
- flag = 1;
- }
- else{
- flag = 0;
- }
- }
- if(O_win == 1){
- if(countO == countX){
- flag = 1;
- }
- else{
- flag = 0;
- }
- }
- }
- else{
- flag = 0;
- }
- }
- }
- else{
- flag = 0;
- }
- if(flag == 1) cout << "yes" << endl;
- else if(flag == 0) cout << "no" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement