Advertisement
Guest User

Untitled

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