Huawei machine trial-go sub-Contest (lower go) Decision (160 points for advanced questions: In-depth priority traversal)

Source: Internet
Author: User

Question:

In go, a piece is on the board, and the blank point directly adjacent to it is the "gas" of the piece. The piece is directly adjacent to the point. If a wife of the same color exists, then they form an inseparable whole, and their "gas" should also be calculated. If one or more pieces of chess are 0, they will be eaten.

1. In the case of a piece, there is an empty point on the right of the white game in the left figure below. At this time, the white game is 1 and will not be eaten. When the player plays chess at this empty point, the player is 0 and will be eaten.


2. A piece of chess piece. For example, there is an empty point under the white game in the lower left corner. Because other white games are directly adjacent to it, the Qi of the entire white game is 1 and won't be eaten. When playing chess at this empty point, the White air is 0 and the game will be eaten.


3. When playing chess makes both sides useless, only the opponent's pawns are eaten. For example, at an empty point, after playing the game, the middle of the game and the middle of the white game are not angry, but only the white game is eaten. In addition to this situation. You are not allowed to play chess, causing your chess piece to become unangry (suicide ).



In this article, a two-dimensional array of 10x10 is used to represent the chessboard. 0 in the array represents the empty point, 1 represents the white game, and 2 represents the black game. The lower-left corner of the board is (0, 0), the upper-right corner is (9, 9), the horizontal side is X, and the vertical side is Y.

To avoid playing too many games, you can set the checkerboard interface to initialize the checkerboard as a mess. Afterwards, the game continues to play chess. The order of playing chess is not fixed. It can be black or white consecutively (go allows one party to give up the next child). When the chess piece is eaten, the corresponding sub-cases are returned, the system should also remove the pawns from the Board to ensure that the Board status is correct.


Running time limit: Unlimited


Memory limit: Unlimited


Input: Enter a 10*10 integer to initialize the checker. Input to ensure that there is no child whose Qi is 0 in the initialization mess (that is, the abnormal state that is taken but not removed ). Enter one or more integer lines. Each line has three integers, indicating the X position, y position, and chess piece type ).


Output: An integer. Meaning:

0: This game has not been eaten yet

2147483647 playing chess error (this position already has a chess piece, or playing chess causes the chess piece to become useless)

Other positive values: some white games are eaten, and the number of eaten items is equal to the returned value.

Negative value: when a player is eaten, the number of shards equals the absolute value of the returned value.


Resolution:

When a piece of input is given, we first determine whether this piece of chess makes the other piece unangry. Then, we determine whether this piece of chess makes the other piece unangry, count the number of useless pawns. If the other party does not have any pawns, but the other party does not, the system will violate the rules. Otherwise, the system will return the number of useless pawns of the other party.

The key question here is how to determine whether a piece is useless. This problem can be solved using a typical DFS (depth-first) method. Given a position and the type of the chess piece at the position, we first determine whether the top of the chess piece is empty. If it is empty, it indicates that the same chess piece connected to the position of the chess piece has gas; if the top part is a piece of the same color, the system will recursively determine the position. If the top part is the other part, it indicates there is no gas above the part. After traversing the position that can be traversed at the top of the chess piece, if no gas is found, you can choose another direction to continue the traversal. Until the gas is found in one direction, or no gas is found in four directions.


# Include <iostream> using namespace STD; int A [10] [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool B [10] [10]; int eatcount = 0; // initialization moment All array B values are false void initflagmatrix () {for (INT I = 0; I <10; I ++) {for (Int J = 0; j <10; j ++) B [I] [J] = false ;}} bool hasair (int I, Int J, int type) {if (a [I] [J] = 0) return true; if (a [I] [J]! = Type) return false; eatcount ++; B [I] [J] = true; if (I> 0 &&! B [I-1] [J] & hasair (I-1, J, type) return true; else if (I <9 &&! B [I + 1] [J] & hasair (I + 1, J, type) return true; else if (j> 0 &&! B [I] [J-1] & hasair (I, J-1, type) return true; else if (j <9 &&! B [I] [J + 1] & hasair (I, j + 1, type) return true; else return false ;} // eat all the pawns of the same type connected to a [I] [J] void eatchess (int I, Int J, int type) {if (a [I] [J]! = Type) return; A [I] [J] = 0; // eat sub if (I> 0) eatchess (I-1, J, type); if (I <9) eatchess (I + 1, J, type); If (j> 0) eatchess (I, J-1, type); If (j <9) eatchess (I, j + 1, type) ;}// search for the entire board to see if the chess piece is angry bool hasairoftype (INT type, Int & P, Int & Q) {for (INT I = 0; I <10; I ++) {for (Int J = 0; j <10; j ++) {if (a [I] [J]! = Type | B [I] [J]) continue; eatcount = 0; If (! Hasair (I, j, type) {P = I, q = J; return false ;}} return true ;}// when the position is [I, j] int eatenchesscount (int I, Int J, int type) {initflagmatrix (); bool self_hasair = hasair (I, j, type); eatcount = 0; int p = 0, q = 0; int other_type = (type = 1? 2: 1); initflagmatrix (); bool other_hasair = hasairoftype (other_type, p, q); If (! Self_hasair & other_hasair) // suicide {A [I] [J] = 0; return 2147483647;} If (! Other_hasair) {eatchess (p, q, other_type); If (other_type = 1) // return eatcount; else // negative return 0-eatcount;} return 0 ;} // print the final state void printchessstate () {for (INT I = 0; I <10; I ++) {for (Int J = 0; j <10; j ++) {cout <A [I] [J] <"" ;}cout <Endl ;}} int main () {int I, j, type; while (CIN> I> j> type) {if (a [I] [J]! = 0) {cout <"2147483647" <Endl; continue;} A [I] [J] = type; cout <"Current residual State: "<Endl; printchessstate (); cout <" Number of subusers to be eaten this time: "<eatenchesscount (I, j, type) <Endl; cout <"residual State after eating" <Endl; printchessstate ();} return 0 ;}






Huawei machine trial-go sub-Contest (lower go) Decision (160 points for advanced questions: In-depth priority traversal)

Related Article

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.