[swexpert] 5644. 무선 충전 (구현, java)

728x90
반응형

사용자를 2명으로 제한해줘서 그나마 다행인 문제

충전소까지 거리가 닿냐에 따라서 사용할 수 있거나 없거나 하는데 사용하는 충전소를 중복 조합으로 만들어준다.

 

중복조합이라고 해서 재귀를 돌릴 필요는 없고 충전소 개수만큼 for문을 돌려주면 된다

다른 충전소이면 총 충전량을 더해주고 같은 충전소이면 충전소가 제공하는 양만큼 (충전한 곳이 하나라도 있을 경우 둘 다 거리가 안닿으면 0) 더해준다.

 

 

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

class Pos{
	int y,x;

	public Pos(int y, int x) {
		super();
		this.y = y;
		this.x = x;
	}
	
}

class Charge{
	int y,x,c,p;

	public Charge(int x, int y, int c, int p) {
		super();
		this.x = x;
		this.y = y;
		this.c = c;
		this.p = p;
	}
	
}

public class Solution {
	static int t;
	static int m,a;
	static ArrayList<Charge> arr;		
	static int[] da;
	static int[] db;
	static int[] xpos= {0,0,1,0,-1};
	static int[] ypos= {0,-1,0,1,0};//0상우하좌
	static int answer;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		
		t=sc.nextInt();
		for (int tc = 1; tc <=t; tc++) {
			arr=new ArrayList<>();//충전소 리스트
			m=sc.nextInt();
			a=sc.nextInt();
			answer=0;
			da=new int[m+1];
			db=new int[m+1];
			for (int i = 1; i <= m; i++) {
				da[i]=sc.nextInt();// a방향 방향
			}
			for (int i = 1; i <= m; i++) {
				db[i]=sc.nextInt();//b 진행 방향 
			}
			for (int i = 0; i < a; i++) {//충전기 a개
				//충전기 x,y,c,p
				arr.add(new Charge(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt()));
			}
			//현재 a좌표, b좌표
			Pos pa=new Pos(1,1);
			Pos pb=new Pos(10,10);
			int[] charge;
			for (int i = 0; i <= m; i++) {
				//위치 갱신
				pa=new Pos(pa.y+ypos[da[i]],pa.x+xpos[da[i]]);
				pb=new Pos(pb.y+ypos[db[i]],pb.x+xpos[db[i]]);
				//현재 위치에서 최대 충전량
				int max=0;
				for (int j = 0; j < arr.size(); j++) {//i초에서 모든 충전기에서 충전가능성 본다
					for (int k = 0; k < arr.size(); k++) {
						//조합. 사용자 2명이 사용하는 충전소 중복 조합 
						int sum=0;
						charge=new int[2];
						Charge temp=arr.get(j);//a가 사용하는 충전소 
						Charge temp2=arr.get(k);//b가 사용하는 충전소 
						if(Math.abs(pa.x-temp.x)+Math.abs(pa.y-temp.y)<=temp.c) {
							charge[0]=temp.p;//사용할 수 있으면 충전량 갱신 
						}
						if(Math.abs(pb.x-temp2.x)+Math.abs(pb.y-temp2.y)<=temp2.c) {
							charge[1]=temp2.p;// 사용할 수 있으면 충전량 갱신 
						}
						
						if(j!=k) {// 다른 충전소 사용하면 그냥 더한다
							sum=charge[0]+charge[1];
						}else {// 같은 충전소 사용하면 2로 나눌 필요 없음 합하면 그 양이다.
							//max를 해주는 이유는 사용할 수 없으면 0이기 때문에
							sum=Math.max(charge[0], charge[1]);
						}
						//최대 충전량 갱신 
						if(sum>max)max=sum;
						
					}	
				}
				//i초에서의 최대 충전량 갱신해준다
				answer+=max;
			}
			
			System.out.printf("#%d %d\n",tc,answer);
		}
	}

	
	
}
728x90
반응형
TAGS.

Comments