/* * given an 2D array and a point p(x,y), find all neighbors of p from left, right, * up, and down. */ import java.util.*; public class Neighbors { public static void main(String[] args){ String input = "5 5\n0 1 2 3 4\n5 6 7 8 0\n" + "1 2 3 4 5\n"+ "6 7 8 0 1\n"+ "2 3 4 5 6\n"+ "1 1\n"+ "3 4\n\n" + "5 6\n"+ "0 1 2 3 4 7\n"+ "5 6 7 8 0 7\n" + "1 2 3 4 5 7\n" + "6 7 8 0 1 7\n"+ "2 3 4 5 6 7\n"+ "1 4\n4 0"; Scanner s = new Scanner ( input); while(s.hasNextLine()){ String line = s.nextLine(); int index = line.indexOf(" "); int rows = Integer.parseInt(line.substring(0,index)); int cols = Integer.parseInt(line.substring(index+1)); //System.out.println("rows:"+ rows); //System.out.println("cols:"+ cols); int[][] arr = new int[rows][cols]; for(int i=0;i=0;i--) sb2.append(arr[i][y] + " "); sb.append( sb2).append(" end of list"); } sb2.delete(0,sb2.length()); System.out.println(sb); sb.delete(0, sb.length()); sb.append("the south neighbors of element ").append (arr[x][y]); sb.append( " at position [").append(x).append("][").append(y).append("] are:"); if( x == arr.length-1) sb.append(" null end of list"); else{ for(int i=x+1;i