Sgu139:help Needed (Jigsaw)

Source: Internet
Author: User

Topic:

Description

Little Johnny likes puzzles a lot! Just a few days ago, he found out about the ' traditional ' 4x4 puzzle. For this puzzle, you have all the numbers from 0 to arranged in 4 rows and 4 column S. You is allowed to switch adjacent elements (horizontally or vertically), only if one of the them have the value 0 . The purpose of the puzzle is to reach the following final state:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0

Given the initial state of the the puzzle and you had to decide whether there exists a sequence of moves which brings the puzzle into the final state.

Input

The input would consist of 4 lines, each of them containing 4 integers, describing the initial state of The puzzle.

Output

For every initial state, you should print "YES" If the final state can be reached after several moves or "no< /c2> ", if such a thing is impossible.

Sample Input #1

1 2 3 4
5 6 7 8
9 Ten 0 13 14 15 12

Sample Output #1

YES

Sample Input #2

2 1 3 4
5 6 7 8
9 Ten-
0 13 14 15

Sample Output #2

NO

The topic is to say, enter 16 numbers, 0-15, and ask if you can recover from such a state.

See my other blog for a detailed conclusion. Puzzle Games-Strategy and assessment

Code:

#include <iostream>
using namespace std;

int list[16];

int main ()
{	
	int sum = 0;
	for (int i = 0; i < i++)
	{
		cin >> list[i];
		for (int j = 0; J < i; j + +) if (list[j]>list[i]) sum++;
		if (list[i] = = 0) sum + = I/4 + i% 4;
	}
	if (sum% 2) cout << "YES";
	else cout << "NO";
	return 0;
}


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.