Coin array Problem

Source: Internet
Author: User

Problem Description:

There are m x N (m<=100, n<=100) gold coins lined up on the desktop into an M-row N-column coin array. Each coin is either facing up or facing up on the back. The gold status is indicated by a number, 0 means the gold coin is facing up, and 1 means the back is facing up. The rules of the coin array game are:
(1) Each line of gold coins can be turned over and placed in the original position;
(2) You can choose 2 columns at a time to exchange the positions of these 2 gold coins.

Algorithm design:

Given the initial and target states of a coin array, the minimum number of transformations required to transform the gold array from the initial state to the target State is calculated by the game of gold rule.


Data input:
The input data is given by the file input.txt. There are multiple sets of data in the file. The 1th line of the file has 1 positive integer k, which indicates that there is a K-group of data. The 1th row of each group of data has 2 positive integers m and N. The following m line is the initial state of the gold array, each row has n digits indicating the status of the gold coin, 0 means the gold coin is facing up, and 1 is the back face up. The next M-line is the target state of the gold array.

Result output:

The minimum number of transformations calculated is output to the file Output.txt in the order of the input data. Output-1 when the corresponding data is not solved.
Example output file for input file sample
Input.txt
2
4 3
1 0 1
0 0 0
1 1 0
1 0 1

1 0 1
1 1 1
0 1 1

1 0 1


4 3
1 0 1
0 0 0
1 0 0
1 1 1

1 1 0
1 1 1
0 1 1
1 0 1


Output.txt
2

-1

Leave the pit ...

Coin array Problem

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.