Advertisement
Guest User

Untitled

a guest
Oct 13th, 2013
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import java.io._;
  2. import scala.io._;
  3.  
  4. object Skyscraper_BruteForce extends App {
  5. def isCorrect(
  6. hintNumber:Int,
  7. rowOrColumn:Array[Array[Int]],
  8. puzzleSize:Int,
  9. rowOrColumnNumber:Int,
  10. whatSide:String
  11. ):Boolean =
  12. {
  13. var observedSkyscrapers:Int = 0;
  14. var prev:Int = -1;
  15.  
  16. for (i:Int<-0 to puzzleSize-1)
  17. {
  18. if(whatSide=="West")
  19. {
  20. if(rowOrColumn(i)(rowOrColumnNumber-1)>prev)
  21. {
  22. observedSkyscrapers=observedSkyscrapers+1;
  23. }
  24. }
  25. else if(whatSide=="North")
  26. {
  27. if(rowOrColumn(rowOrColumnNumber-1)(i)>prev)
  28. {
  29. observedSkyscrapers=observedSkyscrapers+1;
  30. }
  31. }
  32. else if(whatSide=="South")
  33. {
  34. if(rowOrColumn(rowOrColumnNumber-1)(puzzleSize-1-i)>prev)
  35. {
  36. observedSkyscrapers=observedSkyscrapers+1;
  37. }
  38. }
  39. else if(whatSide=="East")
  40. {
  41. if(rowOrColumn(puzzleSize-1-i)(rowOrColumnNumber-1)>prev)
  42. {
  43. observedSkyscrapers=observedSkyscrapers+1;
  44. }
  45. }
  46. prev=i;
  47. }
  48.  
  49. if(observedSkyscrapers==hintNumber)
  50. return true;
  51. else
  52. return false;
  53. }
  54.  
  55. val inputdir = """C:\skyscraper\Input""";
  56. val outputdir = """C:\skyscraper\Output""";
  57.  
  58. val dir = new File(inputdir);
  59. for(f<-dir.listFiles()){
  60. solveSkySkraper(f)
  61. }
  62.  
  63. def solveSkySkraper(f:File):Unit = {
  64. f.getName();
  65. val lines = Source.fromFile(f).mkString;
  66. val numPuzzles = lines(0);
  67.  
  68. val out = new PrintWriter( new File(outputdir+"\\"+f.getName()) , "UTF-8");
  69. out.println(numPuzzles);
  70. println(numPuzzles);
  71. var lineOffset:Int = 0;
  72.  
  73. for(i<-0 until numPuzzles.asDigit)
  74. {
  75. var puzzleSize = 0;
  76. var tracker = 0;
  77. var northHints:Array[String] = Array();
  78. var westHints:Array[String] = Array();
  79. var eastHints:Array[String] = Array();
  80. var southHints:Array[String] = Array();
  81.  
  82. for (line <- Source.fromFile(f).getLines)
  83. {
  84. if(tracker==1+lineOffset)
  85. {
  86. puzzleSize = augmentString(line).toInt;
  87. }
  88. else if(tracker==2+lineOffset)
  89. {
  90. northHints = line split " ";
  91. }
  92. else if(tracker==3+lineOffset)
  93. {
  94. westHints = line split " ";
  95. }
  96. else if(tracker==4+lineOffset)
  97. {
  98. eastHints = line split " ";
  99. }
  100. else if(tracker==5+lineOffset)
  101. {
  102. southHints = line split " ";
  103. }
  104.  
  105. tracker=tracker+1;
  106. }
  107. var solved:Boolean=false;
  108. var puzzleBoardTest = Array.fill[Int](puzzleSize, puzzleSize){1};
  109. var currentBoard = 0;
  110.  
  111. while(solved==false)
  112. {
  113. //initializes all fields to 1
  114. for(k<-0 to puzzleSize-1)
  115. {
  116. for(l<-0 to puzzleSize-1)
  117. {
  118. puzzleBoardTest(k)(l)=1;
  119. }
  120. }
  121. puzzleBoardTest(0)(0)=puzzleBoardTest(0)(0)+currentBoard;
  122. currentBoard = currentBoard+1;
  123. //println(puzzleBoardTest(0)(0));
  124. //checks if a field has a higher value than the highest allowed value. If it does, it subtracts the highest allowed value from that field,
  125. //and adds 1 to the next field until the field is below the allowed value
  126. for(k<-0 to puzzleSize-1)
  127. {
  128. for(l<-0 to puzzleSize-1)
  129. {
  130. var fieldValue = puzzleBoardTest(k)(l);
  131.  
  132. while(fieldValue>puzzleSize)
  133. {
  134. puzzleBoardTest(k)(l)=fieldValue-puzzleSize;
  135. fieldValue = puzzleBoardTest(k)(l);
  136.  
  137. if(l<puzzleSize-1)
  138. {
  139. val nextFieldValue=puzzleBoardTest(k)(l+1);
  140. puzzleBoardTest(k)(l+1)=nextFieldValue+1;
  141. }
  142. else if(k<puzzleSize-1)
  143. {
  144. val nextFieldValue=puzzleBoardTest(k+1)(0);
  145. puzzleBoardTest(k+1)(0)=nextFieldValue+1;
  146. }
  147. }
  148. }
  149. }
  150. println(puzzleBoardTest(3)(3));
  151.  
  152. solved=true;
  153. for (n<-1 to puzzleSize)
  154. {
  155. if(isCorrect(northHints(n-1).toInt,puzzleBoardTest, puzzleSize, n, "North")==false)
  156. {
  157. solved=false;
  158. }
  159. if(isCorrect(eastHints(n-1).toInt,puzzleBoardTest, puzzleSize, n, "East")==false)
  160. {
  161. solved=false;
  162. }
  163. if(isCorrect(southHints(n-1).toInt,puzzleBoardTest, puzzleSize, n, "South")==false)
  164. {
  165. solved=false;
  166. }
  167. if(isCorrect(westHints(n-1).toInt,puzzleBoardTest, puzzleSize, n, "West")==false)
  168. {
  169. solved=false;
  170. }
  171. }
  172. }
  173.  
  174. var solutionString:String="";
  175. out.println(puzzleSize);
  176. println(puzzleSize);
  177. for(i<-0 to (puzzleSize-1))
  178. {
  179. for(n<-0 to (puzzleSize-1))
  180. {
  181. solutionString=solutionString+puzzleBoardTest(n)(i)+" ";
  182. }
  183. out.println(solutionString);
  184. println(solutionString);
  185. solutionString="";
  186. }
  187. /*println(northHints.mkString);
  188. println(westHints.mkString);
  189. println(eastHints.mkString);
  190. println(southHints.mkString);*/
  191.  
  192. lineOffset=lineOffset+5;
  193. }
  194. out.close();
  195. }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement