Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /**
  2. * This program finds a provided 'waldo' 2d array in a provided 'image' 2d array
  3. *
  4. * @author Zach VanDuyn
  5. *
  6. * CS1121, Fall 2010
  7. * Lab Section 4
  8. */
  9.  
  10. public class HW3 {
  11.  
  12. public static void main (String[] args)
  13. {
  14. String input = "dsleigheijasldkjfweifijl\nabcdefgWwZZZW1lkijejfifj\nhijklmnZAaZaZ2invnn,ie#v\nopqrstuZZLlZZ3ivhbiensli\nvwxyz12ZdZDdZ4qiur74nn9Z\n3456789oZZoOZ5bvifj(838,\nke8784jflkav88hj3jf838jv\n*%jie*^djfkei88738*)jeij\n";
  15. String wal = "Ww...W1\n.Aa.a.2\n..Ll..3\n.d.Dd.4\no..oO.5\n";
  16.  
  17. char[][] img = convert(input);
  18. char[][] waldo = convert(wal);
  19.  
  20. int[] res = findWaldo(img, waldo);
  21. for(int i=0; i<res.length;i++) System.out.print(res[i]+",");
  22. }
  23. /**
  24. * This method accepts a large image represented by a
  25. * two dimensional array of characters, and another
  26. * smaller image (Waldo). It returns pairs of row and
  27. * column indices of the upper left corner of Waldo in
  28. * the large image as successive two value pairs in the
  29. * 2-element array. If Waldo does not appear in the image,
  30. * it returns null.
  31. *
  32. * @param image A 2d array of characters (what we are searching through)
  33. * @param waldo A 2d array of characters (searching for this array in 'image')
  34. *
  35. * @return null - if 'waldo' is not found in 'image'
  36. * @return zachsArray - returns array of locations that waldo is found
  37. */
  38. public static int[] findWaldo(char [][] image, char [][] waldo)
  39. {
  40.  
  41. int[] zachsArray;
  42. zachsArray = new int[((image[0].length*image.length)*2)];
  43. int store = 0;
  44. //for each row in image
  45. for(int a = 0; a < image.length; a++)
  46. {
  47. int row = a;
  48.  
  49. //for each character in this row
  50. for(int b = 0; b < image[0].length; b++)
  51. {
  52. int column = b;
  53. boolean found = false;
  54. //for each row of waldo
  55. for(int c = 0; c < waldo.length; c++)
  56. {
  57. //for each character of this row
  58. for(int d = 0; d < waldo[0].length; d++)
  59. {
  60. if(column < image[row].length && (image[row][column] == waldo[c][d] || waldo[c][d] == '.'))
  61. {
  62. column++;
  63. if(d == waldo[0].length-1)
  64. found = true;
  65. }
  66. else
  67. {
  68. d = waldo[0].length;
  69. c = waldo.length;
  70. }
  71.  
  72. }//end of each character of this row
  73.  
  74. }//end of each row of waldo
  75. if(found)
  76. {
  77. zachsArray[store] = a;
  78. store++;
  79. zachsArray[store] = b;
  80. store++;
  81. }
  82.  
  83.  
  84.  
  85. }//end of each character in this row
  86.  
  87. }// end of each row in image
  88. if(store == 0)
  89. return null;
  90. int[] toReturn = new int[store];
  91. for(int i=0; i<store; i++){
  92. toReturn[i]=zachsArray[i];
  93. }
  94. return toReturn;
  95. }//end of findWaldo
  96.  
  97. public static char[][] convert(String array)
  98. {
  99. int x = array.indexOf('\n');
  100. int y = array.length()/x;
  101. char[][] result = new char[x][y];
  102.  
  103. for(int i = 0; i < y; i++)
  104. {
  105. result[i] = array.substring(0, array.indexOf('\n')).toCharArray();
  106. System.out.println(array.substring(0, array.indexOf('\n')));
  107. array = array.substring(array.indexOf('\n') + 1);
  108. }
  109.  
  110. return result;
  111. }
  112. }//end of HW3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement