Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Random;
- public class GetNeighbors {
- public static void main(String args[])throws IOException
- {
- Scanner sc=new Scanner(new File("GetNeighbors.txt"));
- int[][]nums=new int[sc.nextInt()][sc.nextInt()];
- Random rand=new Random();
- rand.setSeed(sc.nextInt());
- for(int r=0;r<nums.length;r++)
- {
- for(int c=0;c<nums[r].length;c++)
- {
- nums[r][c]=rand.nextInt(1000)+1;
- System.out.print(nums[r][c]+" ");
- }
- System.out.println();
- }
- while(sc.hasNext())
- {
- int north=0; int south=0; int east=0; int west=0;
- boolean inBounds = true;
- int row=sc.nextInt();
- int col=sc.nextInt();
- if(row>=0 && row<nums.length)
- {
- if(col>=0&&col<nums[row].length)
- System.out.println("Requested neighbors for element at row "+row+", col "+col+" ("+nums[row][col]+")");
- else
- {
- System.out.println(("Requested neighbors for element at row "+row+", col "+col+" "+"(-1)"));
- inBounds=false;
- }
- }
- else
- {
- System.out.println(("Requested neighbors for element at row "+row+", col "+col+" "+"(-1)"));
- inBounds=false;
- }
- if(inBounds==false)
- {
- System.out.println("North -1, South -1, East -1, West -1");
- }
- else
- {
- north = getNorth(row, col, nums);
- south = getSouth(row, col, nums);
- east = getEast(row, col, nums);
- west = getWest(row, col, nums);
- System.out.println("North "+north+", South "+south+", East "+east+", West "+west);
- }
- }
- }
- public static int getNorth(int row, int col, int[][]nums)
- {
- int n=row-1;
- if(n<0)
- return -1;
- else
- return nums[n][col];
- }
- public static int getSouth(int row, int col, int[][]nums)
- {
- int s=row+1;
- if(s>nums.length-1)
- return -1;
- else
- return nums[s][col];
- }
- public static int getEast(int row, int col, int[][]nums)
- {
- int e = col+1;
- if(e>nums[row].length-1)
- return -1;
- else
- return nums[row][e];
- }
- public static int getWest(int row, int col, int[][]nums)
- {
- int w= col-1;
- if(w<0)
- return -1;
- else
- return nums[row][w];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement