Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Trak {
- String[] predmeti;
- public Trak(int d) {
- predmeti = new String[d];
- }
- public Trak copy() {
- Trak novi = new Trak(predmeti.length);
- for(int i = 0; i < predmeti.length; i++) {
- novi.predmeti[i] = new String(predmeti[i]);
- }
- return novi;
- }
- }
- public class Naloga2 {
- public static void main(String args[]) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int d = sc.nextInt();
- n++;
- Trak traki[] = new Trak[n];
- Trak iskaniTraki[] = new Trak[n];
- for(int i = 1; i < n; i++) {
- traki[i] = new Trak(d);
- iskaniTraki[i] = new Trak(d);
- }
- for(int i = 1; i < n; i++) {
- for(int j = 0; j < d; j++) {
- traki[i].predmeti[j] = "";
- iskaniTraki[i].predmeti[j] = "";
- }
- }
- for(int i = 1; i < n; i++) {
- String vrstica = sc.next();
- String[] split = vrstica.split(":");
- if(split.length == 1) {
- //trak je prazan;
- }else {
- String[] split2 = split[1].split(",");
- for(int j = 0; j < split2.length; j++) {
- traki[i].predmeti[j] = split2[j];
- }
- }
- }
- for(int i = 1; i < n; i++) {
- String vrstica = sc.next();
- //System.out.println(vrstica);
- String[] split = vrstica.split(":");
- if(split.length == 1) {
- //trak je prazan;
- }else {
- String[] split2 = split[1].split(",");
- for(int j = 0; j < split2.length; j++) {
- iskaniTraki[i].predmeti[j] = split2[j];
- }
- }
- }
- int pozicija = 0;
- String vozicek = "";
- Queue queue = new Queue();
- Stanje pocetno = new Stanje(vozicek, pozicija, traki);
- for(int i = 1; i < n; i++) {
- Stanje premik = pocetno.copy();
- premik.pozicija = i;
- queue.enqueue(premik);
- }
- while(!queue.empty()) {
- Stanje stanje = queue.front();
- queue.dequeue();
- if(preveri(stanje.traki, iskaniTraki, n, d)) {
- for(int i = 1; i < n; i++) {
- System.out.println(i);
- for(int j = 0; j < d; j++) {
- System.out.println(stanje.traki[i].predmeti[j]);
- }
- }
- break;
- }
- for(int i = 1; i < n; i++) {
- Stanje premik = stanje.copy();
- if(premik.pozicija != i) {
- premik.pozicija = i;
- queue.enqueue(premik);
- }
- }
- Stanje gor = stanje.copy();
- gor(gor.traki[gor.pozicija]);
- queue.enqueue(gor);
- Stanje dol = stanje.copy();
- dol(dol.traki[dol.pozicija]);
- queue.enqueue(dol);
- Stanje odlozi = stanje.copy();
- if(odlozi.traki[odlozi.pozicija].predmeti[0].equals("") && !(odlozi.vozicek.equals(""))) {
- odlaganje(odlozi.vozicek, odlozi.traki[odlozi.pozicija]);
- odlozi.vozicek = "";
- queue.enqueue(odlozi);
- }
- Stanje nalozi = stanje.copy();
- if(nalozi.vozicek.equals("") && !(nalozi.traki[nalozi.pozicija].predmeti[0].equals(""))) {
- nalozi.vozicek = nalozi.traki[nalozi.pozicija].predmeti[0];
- nalozi.traki[nalozi.pozicija].predmeti[0] = "";
- queue.enqueue(nalozi);
- }
- }
- //sc.close();
- }
- static void odlaganje(String vozicek, Trak trak) {
- trak.predmeti[0] = vozicek;
- }
- static boolean preveri(Trak[] traki, Trak[] iskaniTraki, int n, int d) {
- for(int i = 1; i < n; i++) {
- for(int j = 0; j < d; j++) {
- if(!(traki[i].predmeti[j].equals(iskaniTraki[i].predmeti[j]))) {
- return false;
- }
- }
- }
- return true;
- }
- static boolean nijePrazno(Trak trak, int d) {
- for(int i = 0; i < d; i++) {
- if(trak.predmeti[i] != "") {
- return true;
- }
- }
- return false;
- }
- static void gor(Trak trak) {
- for (int i = trak.predmeti.length - 2; i > -1; i--) {
- trak.predmeti[i + 1] = trak.predmeti[i];
- }
- trak.predmeti[0] = "";
- }
- static void dol(Trak trak) {
- for(int i = 1; i <= trak.predmeti.length-1; i++) {
- trak.predmeti[i-1] = trak.predmeti[i];
- }
- trak.predmeti[trak.predmeti.length-1] = "";
- }
- }
- class Stanje{
- String vozicek;
- int pozicija;
- Trak[] traki;
- String dosadasnjiUkazi;
- public Stanje(String vozicek, int pozicija, Trak[] traki) {
- this.vozicek = vozicek;
- this.pozicija = pozicija;
- this.traki = traki;
- }
- public Stanje copy() {
- Stanje novo = new Stanje(null, 0, null);
- novo.vozicek = new String(vozicek);
- novo.pozicija = pozicija;
- novo.traki = new Trak[traki.length];
- for(int i = 1; i < traki.length; i++) {
- novo.traki[i] = traki[i].copy();
- }
- return novo;
- }
- }
- class QueueElement
- {
- Stanje stanje;
- QueueElement next;
- QueueElement(Stanje stanje)
- {
- this.stanje = stanje;
- next = null;
- }
- }
- class Queue
- {
- //QueueElement -> QueueElement -> QueueElement -> ... -> QueueElement
- // front rear
- //
- // nove elemente dodajamo za element 'rear'
- // elemente jemljemo s front strani
- QueueElement front;
- QueueElement rear;
- public Queue()
- {
- makenull();
- }
- public void makenull()
- {
- front = null;
- rear = null;
- }
- public boolean empty()
- {
- return (front == null);
- }
- public Stanje front()
- {
- if (!empty())
- return front.stanje;
- else
- return null;
- }
- public void print() {
- int cnt = 1;
- QueueElement trenutni = front;
- while(trenutni != null) {
- System.out.println("ELEMENT " + cnt);
- System.out.println(" POZICIJA " + trenutni.stanje.pozicija);
- System.out.println("VOZICEK " + trenutni.stanje.vozicek);
- for(int i = 1; i < trenutni.stanje.traki.length; i++) {
- System.out.println("TRAK " + i);
- for(int j = 0; j < trenutni.stanje.traki[i].predmeti.length; j++) {
- if(trenutni.stanje.traki[i].predmeti[j].equals("")) {
- System.out.println("PRAZNO");
- }else {
- System.out.println(trenutni.stanje.traki[i].predmeti[j]);
- }
- }
- }
- cnt++;
- trenutni = trenutni.next;
- }
- }
- public void enqueue(Stanje stanje)
- {
- QueueElement el = new QueueElement(stanje);
- el.next = null;
- if (empty())
- {
- front = el;
- }
- else
- {
- rear.next = el;
- }
- rear = el;
- }
- public void dequeue()
- {
- if (!empty())
- {
- front = front.next;
- if (front == null)
- rear = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement