Advertisement
arfin97

[UVa 10363] Tic Tac Toe

Feb 9th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. //https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1304
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main(){
  7. int tc;
  8. cin >> tc;
  9. for(int tr = 0; tr < tc; tr++){
  10. int n = 3;
  11. char m[n][n];
  12.  
  13. string input[n];
  14. for(int i = 0; i < n; i++) cin >> input[i];
  15.  
  16. for(int r = 0; r < n; r++){
  17. for(int c = 0; c < n; c++){
  18. m[r][c] = input[r][c];
  19. }
  20. }
  21.  
  22. //COUNTING X, Y;
  23. int countX = 0;
  24. int countO = 0;
  25. for(int i = 0; i < n; i++){
  26. for(int j = 0; j < n; j++){
  27. if(m[i][j] == 'X') countX++;
  28. else if(m[i][j] == 'O') countO++;
  29. }
  30. }
  31.  
  32. //ROW WIN CHECK X;
  33. int x_win = 0;
  34. int cnt_x = 0;
  35. for(int r = 0; r < n; r++){
  36. cnt_x = 0;
  37. for(int c = 0; c < n; c++){
  38. if(m[r][c] == 'X'){
  39. while(c < n){
  40. if(m[r][c] == 'X'){
  41. cnt_x++;
  42. }
  43. else{
  44. break;
  45. }
  46. if(cnt_x == 3){
  47. x_win++;
  48. }
  49. c++;
  50. }
  51. c--;
  52. }
  53. }
  54. cnt_x = 0;
  55. }
  56.  
  57. //ROW WIN CHECK O;
  58. int O_win = 0;
  59. int cnt_O = 0;
  60. for(int r = 0; r < n; r++){
  61. for(int c = 0; c < n; c++){
  62. if(m[r][c] == 'O'){
  63. while(c < n){
  64. if(m[r][c] == 'O'){
  65. cnt_O++;
  66. }
  67. else{
  68. break;
  69. }
  70. if(cnt_O == 3){
  71. O_win++;
  72. }
  73. c++;
  74. }
  75. c--;
  76. }
  77. }
  78. cnt_O = 0;
  79. }
  80.  
  81. //COLUMN WIN CHECK X;
  82. cnt_x = 0;
  83. for(int c = 0; c < n; c++){
  84. for(int r = 0; r < n; r++){
  85. if(m[r][c] == 'X'){
  86. while(c < n){
  87. if(m[r][c] == 'X'){
  88. cnt_x++;
  89. }
  90. else{
  91. break;
  92. }
  93. if(cnt_x == 3){
  94. x_win++;
  95. }
  96. r++;
  97. }
  98. r--;
  99. }
  100. }
  101. cnt_x = 0;
  102. }
  103.  
  104. //COLUMN WIN CHECK O;
  105. cnt_O = 0;
  106. for(int c = 0; c < n; c++){
  107. for(int r = 0; r < n; r++){
  108. if(m[r][c] == 'O'){
  109. while(r < n){
  110. if(m[r][c] == 'O'){
  111. cnt_O++;
  112. }
  113. else{
  114. break;
  115. }
  116. if(cnt_O == 3){
  117. O_win++;
  118. }
  119. r++;
  120. }
  121. r--;
  122. }
  123. }
  124. cnt_O = 0;
  125. }
  126.  
  127. //PRIMARY DIAG WIN CHECK X;
  128. cnt_x = 0;
  129. for(int i = 0; i < n; i++){
  130. if(m[i][i] == 'X'){
  131. while(i < n){
  132. if(m[i][i] == 'X'){
  133. cnt_x++;
  134. }
  135. else{
  136. break;
  137. }
  138. if(cnt_x == 3){
  139. x_win++;
  140. }
  141. i++;
  142. }
  143. i--;
  144. }
  145. cnt_x = 0;
  146. }
  147.  
  148. //PRIMARY DIAG WIN CHECK O;
  149. cnt_O = 0;
  150. for(int i = 0; i < n; i++){
  151. if(m[i][i] == 'O'){
  152. while(i < n){
  153. if(m[i][i] == 'O'){
  154. cnt_O++;
  155. }
  156. else{
  157. break;
  158. }
  159. if(cnt_O == 3){
  160. O_win++;
  161. }
  162. i++;
  163. }
  164. i--;
  165. }
  166. cnt_O = 0;
  167. }
  168.  
  169. //SECONDARY DIAG WIN CHECK X;
  170. cnt_x = 0;
  171. int j = n;
  172. for(int i = 0; i < n; i++){
  173. j--;
  174. if(m[i][j] == 'X'){
  175. while(i < n){
  176. if(m[i][j] == 'X'){
  177. cnt_x++;
  178. }
  179. else{
  180. break;
  181. }
  182. if(cnt_x == 3){
  183. x_win++;
  184. }
  185. i++;
  186. j--;
  187. }
  188. i--;
  189. j++;
  190. }
  191. cnt_x = 0;
  192. }
  193.  
  194. //SECONDARY DIAG WIN CHECK O;
  195. cnt_O = 0;
  196. j = n;
  197. for(int i = 0; i < n; i++){
  198. j--;
  199. if(m[i][j] == 'O'){
  200. while(i < n){
  201. if(m[i][j] == 'O'){
  202. cnt_O++;
  203. }
  204. else{
  205. break;
  206. }
  207. if(cnt_O == 3){
  208. O_win++;
  209. }
  210. i++;
  211. j--;
  212. }
  213. i--;
  214. j++;
  215. }
  216. cnt_O = 0;
  217. }
  218.  
  219.  
  220. int flag = 1;
  221. if(countO == countX || (countO+1) == countX){
  222. flag = 1;
  223. if(x_win > 0 || O_win > 0){
  224. if((x_win == 1 && O_win == 0) || (x_win == 0 && O_win == 1)){
  225. if(x_win == 1){
  226. if(countX > countO){
  227. flag = 1;
  228. }
  229. else{
  230. flag = 0;
  231. }
  232. }
  233. if(O_win == 1){
  234. if(countO == countX){
  235. flag = 1;
  236. }
  237. else{
  238. flag = 0;
  239. }
  240. }
  241. }
  242. else{
  243. flag = 0;
  244. }
  245. }
  246. }
  247. else{
  248. flag = 0;
  249. }
  250.  
  251.  
  252. if(flag == 1) cout << "yes" << endl;
  253. else if(flag == 0) cout << "no" << endl;
  254. }
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement