Advertisement
alisadafi

Matrix-Search-D&C

Nov 9th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         // Example usage
  6.         int[][] X = {
  7.             {1, 2, 3},
  8.             {4, 5, 6},
  9.             {7, 8, 9}
  10.         };
  11.  
  12.         int k = 5;
  13.         int top_left_i = 0;
  14.         int top_left_j = 0;
  15.         int bottom_right_i = X.length - 1;
  16.         int bottom_right_j = X[0].length - 1;
  17.  
  18.         Pair<Integer, Integer> result = find(X, k, top_left_i, top_left_j, bottom_right_i, bottom_right_j);
  19.  
  20.         if (result.getKey() == -1 && result.getValue() == -1) {
  21.             System.out.println("Element not found.");
  22.         } else {
  23.             System.out.println("Element found at position (" + result.getKey() + ", " + result.getValue() + ").");
  24.         }
  25.     }
  26.  
  27.     public static Pair<Integer, Integer> find(int[][] X, int k, int top_left_i, int top_left_j, int bottom_right_i, int bottom_right_j) {
  28.         int m_i = (top_left_i + bottom_right_i) / 2;
  29.         int m_j = (top_left_j + bottom_right_j) / 2;
  30.  
  31.         if (X[m_i][m_j] == k) {
  32.             return new Pair<>(m_i, m_j);
  33.         }
  34.  
  35.         if (top_left_i == bottom_right_i && top_left_j == bottom_right_j) {
  36.             return new Pair<>(-1, -1);  // not found
  37.         }
  38.  
  39.         if (top_left_i > bottom_right_i || top_left_j > bottom_right_j) {
  40.             return new Pair<>(-1, -1);  // not found
  41.         }
  42.  
  43.         if (X[m_i][m_j] > k) {
  44.             Pair<Integer, Integer> pos = find(X, k, top_left_i, top_left_j, m_i - 1, m_j - 1);
  45.             if (!pos.equals(new Pair<>(-1, -1))) {
  46.                 return pos;
  47.             }
  48.  
  49.             pos = find(X, k, m_i, top_left_j, bottom_right_i, m_j - 1);
  50.             if (!pos.equals(new Pair<>(-1, -1))) {
  51.                 return pos;
  52.             }
  53.  
  54.             pos = find(X, k, top_left_i, m_j, m_i - 1, bottom_right_j);
  55.             if (!pos.equals(new Pair<>(-1, -1))) {
  56.                 return pos;
  57.             }
  58.         } else {
  59.             Pair<Integer, Integer> pos = find(X, k, m_i + 1, m_j + 1, bottom_right_i, bottom_right_j);
  60.             if (!pos.equals(new Pair<>(-1, -1))) {
  61.                 return pos;
  62.             }
  63.  
  64.             pos = find(X, k, m_i + 1, top_left_j, bottom_right_i, m_j);
  65.             if (!pos.equals(new Pair<>(-1, -1))) {
  66.                 return pos;
  67.             }
  68.  
  69.             pos = find(X, k, top_left_i, m_j + 1, m_i, bottom_right_j);
  70.             if (!pos.equals(new Pair<>(-1, -1))) {
  71.                 return pos;
  72.             }
  73.         }
  74.  
  75.         return new Pair<>(-1, -1);  // not found
  76.     }
  77. }
  78.  
Tags: D&C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement