14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 

www.acmicpc.net

 

 

 


 

 

 

  • 생각

 

 

dfs ,백트래킹 방식으로 모든 방법을 확인하였다.

 

DFS

1. basecase는 index가 N인 경우로 입력한 숫자를 다 도는 경우를 말한다.

2. 연산자 4개를 하나씩 도는데 여기서 백트래킹 방식으로 작업을 한 뒤 작업이 끝나면 다시 되돌려주는 작업을 한다.

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class Main {
	static int N, max, min;
	static int[] array, mathSymbol;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(max + "\n" + min);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		N = in.nextInt();
		array = new int[N];
		mathSymbol = new int[4];
		max = Integer.MIN_VALUE;
		min = Integer.MAX_VALUE;
		
		for(int i = 0; i < N; i++)
			array[i] = in.nextInt();
		
		for(int i = 0 ; i < 4; i++) {
			mathSymbol[i] = in.nextInt();
		}
		
		dfs(1,  array[0]);
	}
	
	private static void dfs(int index, int value) {
		if(index == N) {
			max = Math.max(max, value);
			min = Math.min(min, value);
		}
		
		for(int j = 0; j < 4; j++) {
			if(mathSymbol[j] == 0)		continue;
				
			mathSymbol[j]--;
			dfs(index + 1, calculate(value, array[index], j));
			mathSymbol[j]++;
		}		
	}
	
	private static int calculate(int a, int b, int symbol) {
		if(symbol == 0)
			return a + b;
		else if(symbol == 1)
			return a - b;
		else if(symbol == 2)
			return a * b;
		else
			return a / b;
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts