abbc0209   4달 전

이거 무슨 최단 경로 비용 계산해서 짜야 하는 것 같긴한데... 잘 모르겠네요

 제가 초보라서 그런데 이 소스에서 좀만 바꾸면 해결이 되는 건가요 아니면 완전히 바꿔야 하면 어떻게 해야할 지 조언 부탁드립니다


--------------> 밑 부분이 제소스입니다...

import java.util.ArrayList;
import java.util.Scanner;

class TempMemory{
int beforeIndex;
int sum;
ArrayList<Integer> sumList = new ArrayList<>();
public int getBeforeIndex() {
return beforeIndex;
}
public void setBeforeIndex(int beforeIndex) {
this.beforeIndex = beforeIndex;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public ArrayList<Integer> getSumList() {
return sumList;
}
public void setSumList(ArrayList<Integer> sumList) {
this.sumList = sumList;
}

}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<ArrayList<Integer>> houseList = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> arrayedHouseList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> eachHouseList = null;

int houseNumber = scanner.nextInt(); //insert the number of family

//////////////////////////////////////////insert values function
for(int i=0; i< houseNumber; i++){
eachHouseList = new ArrayList<Integer>();
for(int j=0; j<3; j++){
eachHouseList.add(scanner.nextInt()); //each contents of r g b
}
houseList.add(eachHouseList);
eachHouseList.sort(null);
arrayedHouseList.add(eachHouseList); // for comparing values
}
/////////////////////////////////////// calculating values of minimum
TempMemory memory = new TempMemory();

for(int i=0; i<3; i++){
memory.setSum(houseList.get(0).get(i));
memory.setBeforeIndex(i);
for(int j=1; j<houseNumber; j++){
eachHouseList = houseList.get(j);

if(eachHouseList.get(memory.getBeforeIndex()).equals(arrayedHouseList.get(j).get(0))){
//System.out.println("come");
for(int k=0; k<3; k++){
if(eachHouseList.get(k).equals(arrayedHouseList.get(j).get(1))){
memory.setBeforeIndex(k);
memory.setSum(eachHouseList.get(k) + memory.getSum());
}//if
}//for

}else{//if
for(int k=0; k<3; k++){
if(eachHouseList.get(k).equals(arrayedHouseList.get(j).get(0))){
memory.setBeforeIndex(k);
memory.setSum(eachHouseList.get(k) + memory.getSum());
}//if
}//for
}

}//for
memory.sumList.add(memory.sum);
}//for
//System.out.println(memory.sumList);
memory.sumList.sort(null);
System.out.println(memory.sumList.get(0));
}
}


댓글을 작성하려면 로그인해야 합니다.