**Sam's Puzzle (Approximate)**

### Problem Statement :

Sam invented a new puzzle game played on an n x n matrix named puzzle, where each cell contains a unique integer in the inclusive range between 1 and n^2. The coordinate of the top-left cell is (1, 1). The Moves A move consists of two steps: Choose a sub-square of puzzle. Rotate the sub-square in the clockwise direction. Good Pairs of Cells A pair of cell is good if one of the following is true: They're located in the same row and the number in the left cell is less than the number in the right cell. They're located in the same column and the number in the upper cell is less than the number in the lower cell. The diagram below depicts all the good pairs of cells located in the same row: Goodness of a Square We define the goodness of a sub-square to be the total number of good pairs of cells in the sub-square. The Goal Given the initial value of puzzle, maximize its goodness as much as is possible by performing a sequence of at most 500 moves. Then print the necessary moves according to the Output Format specified below. Input Format The first line contains an integer denoting n. Each of the n subsequent lines contains n space-separated integers. The jth integer in the ith line denotes the cell located in coordinate (i, j ) Constraints 1 <= n <= 30 Each cell contains a unique number in the inclusive range between 1 and n^2. Output Format Print the following lines of output: On the first line, print an integer, m, denoting the number of moves necessary to maximize the goodness of puzzle. Recall that this number must be <= 500 . For each move, print three space-separated integers describing its respective ,i , j and k values on a new line. Recall that a move is described as the clockwise rotation of a k x k sub-square whose top-left corner is located at coordinate ( i, j ).

### Solution :

` ````
Solution in C :
In Python3 :
#!/bin/python3
import math
import os
import random
import re
import sys
import itertools
import time
def score(a):
count = 0
for r in range(len(a)):
for ind in itertools.combinations(range(len(a)), 2):
if a[r][ind[0]] < a[r][ind[1]]:
count += 1
for c in range(len(a[0])):
for ind in itertools.combinations(range(len(a[0])), 2):
if a[ind[0]][c] < a[ind[1]][c]:
count += 1
return count
def rotate(a, r, c, d):
seg = [[a[i][c+j] for i in range(r+d, r-1, -1)] for j in range(d+1)]
for i in range(d+1):
for j in range(d+1):
a[r+i][c+j] = seg[i][j]
def undo(a, r, c, d):
seg = [[a[r+i][j] for i in range(d+1)] for j in range(c+d, c-1, -1)]
for i in range(d+1):
for j in range(d+1):
a[r+i][c+j] = seg[i][j]
def getRandom():
r = random.randint(0, len(a)-2)
c = random.randint(0, len(a)-2)
d = random.randint(1, len(a)-1-max(r,c))
return r, c, d
def countDes(a):
arrayMax = []
for i in range(len(a[0])-1):
count = 1
curI = 0
curMax = 1
posMax = 0
for j in range(1, len(a)):
if a[j][i] < a[j-1][i]:
count+= 1
if j == len(a) - 1:
if count > curMax:
curMax = count
posMax = curI
else:
if count > curMax:
curMax = count
posMax = curI
count = 1
curI = j
# length #r #c
arrayMax.append([curMax, posMax, i])
return arrayMax
def processNotRandom(a, getArray, history):
maxPos = -1
maxScore = score(a)
for i in range(len(getArray)):
r = getArray[i][1]
c = getArray[i][2]
d = min(len(a)-1-max(r,c), getArray[i][0])
rotate(a, r, c, d)
sc = score(a)
if sc > maxScore:
maxPos = i
maxScore = sc
undo(a, r, c, d)
if maxPos != -1:
r = getArray[maxPos][1]
c = getArray[maxPos][2]
d = min(len(a)-1-max(r,c), getArray[maxPos][0])
rotate(a, r, c, d)
history.append([r+1, c+1, d+1])
n = int(input())
a = []
for _ in range(n):
a.append(list(map(int, input().rstrip().split())))
# Write Your Code Here
curMax = score(a)
curSc = curMax
start = time.time()
Beta = 0.8
Beta_increase = 0.8
history = []
maxScore = score(a)
maxPos = -1
i = 0
while time.time() - start < 8.0 and len(history) < 500:
r, c, d = getRandom()
numRotate = 0
for j in range(3):
rotate(a, r, c, d)
sc = score(a)
if sc > maxScore:
maxScore = sc
numRotate = j + 1
rotate(a, r, c, d)
for j in range(numRotate):
history.append([r+1, c+1, d+1])
rotate(a, r, c, d)
print(len(history))
for i in range(0, len(history)):
print('{} {} {}'.format(history[i][0], history[i][1], history[i][2]))
```

## View More Similar Problems

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →## Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →## Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →