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;
}