The grid of 4 numbers is represented by a 2×2 array:
int grid[2][2] = {{1,2},{3,4}};
// 1 2
// 3 4
|
The grid of 4 numbers is represented by a 2×2 array:
int grid[2][2] = {{1,2},{3,4}};
// 1 2
// 3 4
|
cin >> myString;
for ( int i = 0; i < myString.size(); i++ )
{
if ( myString[i] == 'V' )
{
Flip vertically
}
else
{
Flip horizontally
}
}
Print grid;
|
cin >> myString;
for ( int i = 0; i < myString.size(); i++ )
{
if ( myString[i] == 'V' )
{ // Flip vertically
int h;
h = grid[0][0]; grid[0][0] = grid[0][1]; grid[0][1] = h;
h = grid[1][0]; grid[1][0] = grid[1][1]; grid[1][1] = h;
}
else
{ // Flip horizontally
int h;
h = grid[0][0]; grid[0][0] = grid[1][0]; grid[1][0] = h;
h = grid[0][1]; grid[0][1] = grid[1][1]; grid[1][1] = h;
}
}
cout << grid[0][0] << " " << grid[0][1] << endl;
cout << grid[1][0] << " " << grid[1][1] << endl;
|
H and H commutes:
HH commutes:
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | H1 | c | d | H2 | a | b |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | a | b | | c | d |
+---+---+ +---+---+ +---+---+
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | H2 | c | d | H1 | a | b |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | a | b | | c | d |
+---+---+ +---+---+ +---+---+
|
In fact: HH will cancel each other !
V and V commutes:
VV commutes:
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | V1 | b | a | V2 | a | b |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | d | c | | c | d |
+---+---+ +---+---+ +---+---+
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | V2 | b | a | V1 | a | b |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | d | c | | c | d |
+---+---+ +---+---+ +---+---+
|
In fact: VV will cancel each other !
H and V commutes:
HH commutes:
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | H | c | d | V | d | c |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | a | b | | b | a |
+---+---+ +---+---+ +---+---+
arbitrary
initial state
+---+---+ +---+---+ +---+---+
| a | b | V | d | c | H | a | b |
+---+---+ -----> +---+---+ -----> +---+---+
| c | d | | d | c | | b | a |
+---+---+ +---+---+ +---+---+
|
Therefore: we can re-order the H, V operations
(1) Move all H operations to the front (2) Cancel every pair of H operations (3) Move all V operations to the back (4) Cancel every pair of V operations (5) Perform the remaining operations on the grid |
cin >> myString;
int nH = 0, nV = 0;
for ( int i = 0; i < myString.size(); i++ )
{
if ( myString[i] == 'V' )
nV++;
else
nH++;
}
if ( nV % 2 == 1 )
{ // Flip vertically
int h;
h = grid[0][0]; grid[0][0] = grid[0][1]; grid[0][1] = h;
h = grid[1][0]; grid[1][0] = grid[1][1]; grid[1][1] = h;
}
if ( nH % 2 == 1 )
{ // Flip horizontally
int h;
h = grid[0][0]; grid[0][0] = grid[1][0]; grid[1][0] = h;
h = grid[0][1]; grid[0][1] = grid[1][1]; grid[1][1] = h;
}
|