Netease game 2016 intern recruitment test questions-jing Ziqi, 2016 Jing Ziqi

Source: Internet
Author: User

Netease game 2016 intern recruitment test questions-jing Ziqi, 2016 Jing Ziqi
Description

Solutions

Use a three-dimensional array input [] [3] [3] To save the input board status. Use a one-dimensional array result [] to save the outcome of each game and use numbers to represent the outcome. 3 indicates o wins, 12 indicates x wins, 0 indicates a full draw under the board, and-1 indicates a non-existent Board status.
For each game board, enter the status 3*3 to determine the outcome. Determine the status of each row, column, diagonal line, and inverse diagonal line respectively, and save the final judgment result value to the result array.

Implementation Code
# Include & lt; iostream & gt; # define SBOUNDARY 2000 # define BOARDSIZE 3 using namespace std; int status [8]; // records the sum of three rows and three columns and two diagonal lines. int input [SBOUNDARY] [3] [3]; // each input is processed separately, as the chessboard is not traversed in the future, you only need to use a [3] [3] matrix to get int result [SBOUNDARY]; int main () {char c; int S = 0; int x_count = 0; int o_count = 0; cin> S; for (int s = 0; s <S; s ++) {x_count = 0; o_count = 0; for (int I = 0; I <8; I ++) {status [I] = 0;} // enter the board for (int I = 0; I <BOARDSIZE; I ++) {for (int j = 0; j <BOARDSIZE; j ++) {cin> c; // counts input, to determine the checker status if (c = 'X') {input [s] [I] [j] = 1; x_count ++ ;} else if (c = 'O') {input [s] [I] [j] = 4; o_count ++;} else if (c = '_') {input [s] [I] [j] = 0;} if (I = 0) {// status [0] + = input [s] [I] [j];} else if (I = 1) {// status [1] + = input [s] [I] [j];} else if (I = 2) {// status [2] + = input [s] [I] [j];} if (j = 0) {// first column status [3] + = input [s] [I] [j];} else if (j = 1) {status [4] + = input [s] [I] [j];} else if (j = 2) {status [5] + = input [s] [I] [j];} if (I = j) {// diagonal line status [6] + = input [s] [I] [j];} if (I + j = 2) {// reversed diagonal line status [7] + = input [s] [I] [j] ;}}// judge the current game board if (x_count <o_count) {// invalid result [s] =-1;} else {int I = 0; for (; I <8; I ++) {if (status [I] = 3) {result [s] = 3; // x wins; use 3 to mark as x to win -- three consecutive 1 break;} else if (status [I] = 12) {result [s] = 12; // o wins; Use 12 to mark o wins -- three consecutive 4 breaks exist;} else if (status [I] = 2 & x_count = o_count) {// two consecutive records of x are displayed, and the next step is the result [s] = 4; // next x wins the break ;} else if (status [I] = 8 & x_count = (o_count + 1 )) {// two consecutive o s appear and the next o follows result [s] = 13; // next o wins break ;}} if (I = 8 & (x_count + o_count) = 9) {// if the above outcome is not met, determine whether to draw the result [s] = 1; // full under the board, draw; the initial default value of result [] cannot be set to 0 }}// outputs the winning or losing situation of each game based on the numerical value in the result array for (int I = 0; I <S; I ++) {if (result [I] =-1) {cout <"Invalid" <endl ;} else if (result [I] = 3) {cout <"X win" <endl;} else if (result [I] = 12) {cout <"O win" <endl;} else if (result [I] = 4 | result [I] = 13) {cout <"Next win" <endl;} else if (result [I] = 1) {cout <"Draw" <endl ;} else {cout <"Next cannot win" <endl ;}} return 0 ;}
Test Cases and Output

Output result:

In the first example, because X goes down first, this situation cannot occur.

In the second example, X wins in the end.

In the third example, after all the grids are completed, both sides cannot win and draw

In the fourth example, the game won't win, but the next one is X, which can win.

In the fifth example, there is no victory or defeat, and the next one is O. No matter which grid is down, it cannot win.

The question is only a 3*3 simple board, so it is not extended, but the idea is the same.
In addition, there is no need to store the input state of the board. You only need a 3*3 array without a 3-dimensional array.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.