Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. class Trak {
  5.  
  6. String[] predmeti;
  7.  
  8. public Trak(int d) {
  9. predmeti = new String[d];
  10. }
  11. }
  12.  
  13.  
  14.  
  15.  
  16. public class Naloga2 {
  17. public static void main(String args[]) {
  18. Scanner sc = new Scanner(System.in);
  19.  
  20. int n = sc.nextInt();
  21. int d = sc.nextInt();
  22. n++;
  23. Trak traki[] = new Trak[n];
  24. Trak iskaniTraki[] = new Trak[n];
  25.  
  26.  
  27. for(int i = 1; i < n; i++) {
  28. traki[i] = new Trak(d);
  29. iskaniTraki[i] = new Trak(d);
  30. }
  31.  
  32. for(int i = 1; i < n; i++) {
  33. for(int j = 0; j < d; j++) {
  34. traki[i].predmeti[j] = "";
  35. iskaniTraki[i].predmeti[j] = "";
  36. }
  37. }
  38.  
  39.  
  40. for(int i = 1; i < n; i++) {
  41. String vrstica = sc.next();
  42. String[] split = vrstica.split(":");
  43. if(split.length == 1) {
  44. //trak je prazan;
  45. }else {
  46. String[] split2 = split[1].split(",");
  47. for(int j = 0; j < split2.length; j++) {
  48. traki[i].predmeti[j] = split2[j];
  49. }
  50. }
  51.  
  52.  
  53. }
  54. for(int i = 1; i < n; i++) {
  55. String vrstica = sc.next();
  56. //System.out.println(vrstica);
  57. String[] split = vrstica.split(":");
  58. if(split.length == 1) {
  59. //trak je prazan;
  60. }else {
  61. String[] split2 = split[1].split(",");
  62. for(int j = 0; j < split2.length; j++) {
  63. iskaniTraki[i].predmeti[j] = split2[j];
  64. }
  65. }
  66. }
  67. /*for(int i = 1; i < n; i++) {
  68. System.out.println("PREDMET " + i);
  69. for(int j = 0; j < d; j++) {
  70. if(traki[i].predmeti[j].equals("")) {
  71. System.out.println("PRAZNO");
  72. }else {
  73. System.out.println(traki[i].predmeti[j]);
  74. }
  75. }
  76. }*/
  77.  
  78.  
  79.  
  80. int pozicija = 0;
  81. String vozicek = "";
  82. Queue queue = new Queue();
  83. Stanje stanje;
  84. for(int i = 1; i < n; i++) {
  85. stanje = new Stanje("PREMIK " + i, vozicek, pozicija, traki);
  86. queue.enqueue(stanje);
  87. }
  88.  
  89.  
  90.  
  91.  
  92. while(!queue.empty()) {
  93. stanje = (Stanje)queue.front();
  94. queue.dequeue();
  95.  
  96. if(preveri(stanje.traki, iskaniTraki, n, d)) {
  97.  
  98. for(int i = 1; i < n; i++) {
  99. System.out.println(i);
  100. for(int j = 0; j < d; j++) {
  101. System.out.println(traki[i].predmeti[j]);
  102. }
  103. }
  104. break;
  105. }
  106.  
  107.  
  108. if(stanje.ukaz.charAt(0) == ('P')) {
  109.  
  110. for(int i = 1; i<n; i++) {
  111. if(stanje.ukaz.equals("PREMIK " + i)) {
  112. stanje.pozicija = i;
  113. }
  114. }
  115. }
  116.  
  117. if(stanje.ukaz.equals("GOR")) {
  118.  
  119. gor(stanje.traki[stanje.pozicija]);
  120.  
  121. }
  122.  
  123. if(stanje.ukaz.equals("DOL")) {
  124.  
  125. dol(stanje.traki[stanje.pozicija]);
  126.  
  127.  
  128. }
  129. if(stanje.ukaz.equals("ODLOZI")) {
  130.  
  131. if (stanje.pozicija != 0 && stanje.traki[stanje.pozicija].predmeti[0] == "") {
  132. odlaganje(stanje.vozicek, stanje.traki[stanje.pozicija]);
  133. stanje.vozicek = "";
  134. }else {
  135. continue;
  136. }
  137.  
  138.  
  139.  
  140. }
  141. if(stanje.ukaz.equals("NALOZI")) {
  142. if(stanje.pozicija != 0) {
  143. if(stanje.vozicek == "") {
  144. stanje.vozicek = stanje.traki[stanje.pozicija].predmeti[0];
  145. stanje.traki[stanje.pozicija].predmeti[0] = "";
  146. }
  147. }else {
  148. continue;
  149. }
  150.  
  151. }
  152.  
  153.  
  154.  
  155. for(int i = 1; i < n; i++) {
  156. queue.enqueue(new Stanje("PREMIK " + i,stanje.vozicek, stanje.pozicija, stanje.traki));
  157. }
  158. queue.enqueue(new Stanje("GOR", stanje.vozicek, stanje.pozicija, stanje.traki));
  159. queue.enqueue(new Stanje("DOL", stanje.vozicek, stanje.pozicija, stanje.traki));
  160. queue.enqueue(new Stanje("ODLOZI", stanje.vozicek, stanje.pozicija, stanje.traki));
  161. queue.enqueue(new Stanje("NALOZI", stanje.vozicek, stanje.pozicija, stanje.traki));
  162.  
  163. }
  164.  
  165.  
  166. sc.close();
  167. }
  168.  
  169.  
  170. static void odlaganje(String vozicek, Trak trak) {
  171.  
  172. trak.predmeti[0] = vozicek;
  173.  
  174.  
  175. }
  176.  
  177. static boolean preveri(Trak[] traki, Trak[] iskaniTraki, int n, int d) {
  178. for(int i = 1; i < n; i++) {
  179. for(int j = 0; j < d; j++) {
  180. if(!(traki[i].predmeti[j].equals(iskaniTraki[i].predmeti[j]))) {
  181. return false;
  182. }
  183. }
  184. }
  185. return true;
  186. }
  187.  
  188. static void gor(Trak trak) {
  189. for (int i = trak.predmeti.length - 2; i > -1; i--) {
  190. trak.predmeti[i + 1] = trak.predmeti[i];
  191.  
  192. }
  193. trak.predmeti[0] = "";
  194. }
  195.  
  196. static void dol(Trak trak) {
  197. for(int i = 1; i <= trak.predmeti.length-1; i++) {
  198. trak.predmeti[i-1] = trak.predmeti[i];
  199. }
  200. trak.predmeti[trak.predmeti.length-1] = "";
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. }
  209.  
  210. class Stanje{
  211. String vozicek;
  212. int pozicija;
  213. Trak[] traki;
  214. String ukaz;
  215. String dosadasnjiUkazi;
  216.  
  217. public Stanje(String ukaz, String vozicek, int pozicija, Trak[] traki) {
  218. this.vozicek = vozicek;
  219. this.ukaz = ukaz;
  220. this.pozicija = pozicija;
  221. this.traki = traki;
  222. }
  223. }
  224.  
  225. class QueueElement
  226. {
  227. Stanje stanje;
  228. QueueElement next;
  229.  
  230. QueueElement(Stanje stanje)
  231. {
  232. this.stanje = stanje;
  233. next = null;
  234. }
  235. }
  236.  
  237. class Queue
  238. {
  239. //QueueElement -> QueueElement -> QueueElement -> ... -> QueueElement
  240. // front rear
  241. //
  242. // nove elemente dodajamo za element 'rear'
  243. // elemente jemljemo s front strani
  244.  
  245. QueueElement front;
  246. QueueElement rear;
  247.  
  248. public Queue()
  249. {
  250. makenull();
  251. }
  252.  
  253. public void makenull()
  254. {
  255. front = null;
  256. rear = null;
  257. }
  258.  
  259. public boolean empty()
  260. {
  261. return (front == null);
  262. }
  263.  
  264. public Stanje front()
  265. {
  266. if (!empty())
  267. return front.stanje;
  268. else
  269. return null;
  270. }
  271.  
  272. public void enqueue(Stanje stanje)
  273. {
  274. QueueElement el = new QueueElement(stanje);
  275. el.next = null;
  276.  
  277. if (empty())
  278. {
  279. front = el;
  280. }
  281. else
  282. {
  283. rear.next = el;
  284. }
  285.  
  286. rear = el;
  287. }
  288.  
  289. public void dequeue()
  290. {
  291. if (!empty())
  292. {
  293. front = front.next;
  294.  
  295. if (front == null)
  296. rear = null;
  297. }
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement