The Dangerous Seven
Difficulty: Hard

Super Mario is studying how to use a mysterious Power Square. The Power Square is n×n with integer values between 0 and n^2 − 1.  
A number y is a neighbor of another number x in the Power Square if y is directly above or below
x, or directly to the left or right of x. Rosalina asks Super Mario to find all the numbers in the Power Square that are neighbors of the number 7, since she can tell that those numbers are quite nervous.    
"Why are the numbers scared of seven?" Mario asks Rosalina.  
"Because seven ate nine!" Rosalina exclaims.  

**Input/Output Format:**
Input is a description of of the Power Square, followed by a number of commands. The first line is the size of the Power Square n. You may assume n ≤ 100. The second line contains the n^2 values in the Power Square, separated by spaces. Values start from the top left corner and move from left to right, moving down one row to the leftmost position when a row is filled.

Following the Power Square description are a number of commands, with each command on a separate line. Each command begins with the name of the command, followed by any additional command parameters.

* The command ”SHOW” causes the current state of the Power Square to be displayed in n × n form (each row of n values on a single line, separated by spaces), followed by a blank line.
* The command ”NEIGHBORS” is followed by a value x in the Power Square. The values neighbouring x are output and displayed on a single line (in the order: above, left, right and below x), separated by spaces. You may assume that x is always in the power square.

**Example:**

	Input: 						
    
	3 											
	8 7 6 5 4 3 2 1 0 							
	SHOW 										
	NEIGHBORS 7
	NEIGHBORS 1 								
	NEIGHBORS 4 								
					                                            
    Output:
    
	8 7 6
	5 4 3
	2 1 0
	8 6 4
	4 2 0
	7 5 3 1                                            


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
	import java.io.File;
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.PrintStream;
	import java.util.Random;
	import java.util.Scanner;

	
	public class Puzzle {
	static final int maxPuzzle = 1000;	// size of puzzle
	static int puzzleSize = 1;	// size of puzzle
	static int puzzleTiles;	// number of tiles & 1 space in puzzle

	static int puzzle[];

	public static void main(String[] args) throws Exception {
		long time;
		int rowNum = 0;
		int row[] = new int [maxPuzzle];
		int idx;

		time = System.currentTimeMillis();
		time = System.currentTimeMillis() - time;
		// System.out.println("Solve Time = "+(time/1000));

		// read size of puzzle
		Scanner sc = new Scanner( System.in );
		String line = sc.nextLine();
		Scanner lsc = new Scanner( line );

		puzzleSize = lsc.nextInt();
		puzzleTiles = puzzleSize*puzzleSize;
		puzzle = new int[puzzleTiles];

		// read contents of puzzle
		line = sc.nextLine();
		lsc = new Scanner( line );
		for (int i = 0; i< puzzleTiles; i++) {
			puzzle[i] = lsc.nextInt();
		}

		// perform commands
		while (sc.hasNextLine()) {
			line = sc.nextLine();
			lsc = new Scanner( line );

			// skip past empty lines
			if (!lsc.hasNext()) 
				continue;

			String s = lsc.next();
			if (s.equals("SHOW")) {
				printPuzzle(puzzle);
			} 
			else if (s.equals("NEIGHBORS")) {
				int target = lsc.nextInt();
				int neighbors[] = findNeighbors(target);
				for (int i = 0; i< neighbors.length; i++) {
					System.out.print(neighbors[i] + " ");
				}
				System.out.println();
			} 
			else if (s.equals("SOLUTION")) {
				boolean solved = isSolution(puzzle);
				System.out.println(solved);
			} 
			else if (s.equals("DIRECTIONS")) {
				int target = lsc.nextInt();
				takeSteps(target);
			}
			else if (s.equals("SOLVE")) {
				int maxSteps = lsc.nextInt();
				solvePuzzle(maxSteps);
			}
			else {
				System.out.println("Illegal input " + s);
				return;
			}
		}
	}

	public static boolean isSolution(int p[]) {
		// values must be in sorted order, except for last position
		for (int i = 0; i < (puzzleTiles) - 2; i++) {
			if (p[i] > p[i+1])
				return false;
		}
		// last position in puzzle must be 0
		return (p[puzzleTiles-1] == 0);
	}

	public static int row_col_to_pos(int size, int r, int c) {
		return (r*size)+c;
	}

	public static int pos_to_row(int size, int pos) {
		return pos/size;
	}

	public static int pos_to_col(int size, int pos) {
		return pos % size;
	}

	public static int[] findNeighbors(int target) {
		int i;

		for (i = 0; i < (puzzleTiles); i++) {
			if (puzzle[i] == target) 
				break;
		}
		if (i == puzzleTiles) {
			System.out.println("Not found");
			return null;
		}

		int row = i/puzzleSize;
		int col = i%puzzleSize;
		int numNeighbors = 0;
		int neighbors[] = new int[4];

		// top neighbor
		if ((row - 1) >= 0) {
			neighbors[numNeighbors] = puzzle[i-puzzleSize];
			numNeighbors++;
		}
		// left neighbor
		if ((col - 1) >= 0) {
			neighbors[numNeighbors] = puzzle[i-1];
			numNeighbors++;
		}
		// right neighbor
		if ((col + 1) < puzzleSize) {
			neighbors[numNeighbors] = puzzle[i+1];
			numNeighbors++;
		}
		// bottom neighbor
		if ((row + 1) < puzzleSize) {
			neighbors[numNeighbors] = puzzle[i+puzzleSize];
			numNeighbors++;
		}

		int retVal[] = new int[numNeighbors];

		for (int k = 0; k < numNeighbors; k++) {
			retVal[k] = neighbors[k];
		}

		return retVal;
	}

	public static void takeSteps(int target) {

	}

	public static void solvePuzzle(int maxSteps) {

	}

	public static void printPuzzle(int p[]) {
		for (int i = 0; i<puzzleTiles ; i++) {
			String leadingSpace = "";
			if (p[i] < 10)
				leadingSpace = "  ";
			else if (p[i] < 100) 
				leadingSpace = " ";

			System.out.print(leadingSpace + p[i] + " ");
			if (((i+1) % puzzleSize) == 0) 
				System.out.println();
		}
	}
	}