SWEA5644 무선 충전

·2022년 4월 27일
0

SWEA 알고리즘

목록 보기
20/29


각 이동마다 그 상태에서 최대 충전 성능을 구해 더해주면 구할 수 있다.
2차원 배열에 대한 이해와 구현을 할 수 있는 지 묻는 문제다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static int[][] map;
	static BC[] bcs;
	static int A_x, A_y, B_x, B_y, N, M;
	static int[] A, B;
	static int[][] dir = {
			{0,0},
			{0,-1},
			{1,0},
			{0,1},
			{-1,0}
	};
	public static int calc() {
		int sum = 0;
		boolean[][] check = new boolean[N][2]; // 한사람이 두개를 밟고있는 경우 제외.
		for (int j = 0; j < N; j++) {
			if (bcs[j].b(A_x, A_y) || bcs[j].b(B_x, B_y)) {
				if (bcs[j].b(A_x, A_y))
					check[j][0] = true;
				if (bcs[j].b(B_x, B_y))
					check[j][1] = true;
			}
		}
		int count_a = 1;
		int count_b = 1;
		int count = 0;
		int[] c = new int[2];
		int j = 0;
		while ((count_a > 0 || count_b > 0) && j < N) {
			if(count == 2) break;
			
			if(check[j][0] && check[j][1]) {
				c[count++] = j;
			}else if(check[j][0] && count_a > 0){
				c[count++] = j;
				count_a--;
			}else if(check[j][1] && count_b > 0) {
				c[count++] = j;
				count_b--;
			}
			j++;
		}
		
		for(int k = 0; k < count; k++) {
			sum += bcs[c[k]].P;
		}
		return sum;
	}
	public static class BC implements Comparable<BC> {
		public int x, y;
		int c;
		int P; // 성능;
		boolean[][] b_map = new boolean[10][10];
		boolean b = false;

		BC(int x, int y, int c, int P) {
			this.x = x - 1;
			this.y = y - 1;
			this.c = c;
			this.P = P;
		}

		@Override
		public int compareTo(BC o) {
			return o.P - this.P;
		}

		public void b_calc() {
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {

					if (Math.abs(i - x) + Math.abs(j - y) <= c) {
						b_map[j][i] = true;
					} else {
					}
				}
			}
		}

		public boolean b(int a, int b) { // 두 사용자의 위치 받음.
			if (b_map[a][b])
				return true;
			return false;
		}
	}

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine());

		for (int tc = 1; tc <= T; tc++) {
			A_x = 0; A_y = 0;
			B_x = 9; B_y = 9;
			st = new StringTokenizer(br.readLine(), " ");
			M = Integer.parseInt(st.nextToken()); // 이동시간.
			N = Integer.parseInt(st.nextToken()); // 충전기 개수.
			A = new int[M];
			B = new int[M];
			st = new StringTokenizer(br.readLine(), " ");
			for (int i = 0; i < M; i++) {
				A[i] = Integer.parseInt(st.nextToken());
			}
			st = new StringTokenizer(br.readLine(), " ");
			for (int i = 0; i < M; i++) {
				B[i] = Integer.parseInt(st.nextToken());
			}

			bcs = new BC[N];
			for (int i = 0; i < N; i++) {
				st = new StringTokenizer(br.readLine(), " ");
				bcs[i] = new BC(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
						Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
				bcs[i].b_calc();
			}

			Arrays.sort(bcs);
			int sumofP = 0;
			sumofP += calc();
			for (int i = 0; i < M; i++) {
				A_x = A_x + dir[A[i]][1];
				A_y = A_y + dir[A[i]][0];
				B_x = B_x + dir[B[i]][1];
				B_y = B_y + dir[B[i]][0];
				sumofP += calc();
			}
			System.out.println("#" + tc + " " + sumofP);
		}

	}
}
profile
SSAFY 7기

0개의 댓글