Giter Site home page Giter Site logo

acmicpc's Introduction

๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ(medium)

Hits

acmicpc's People

Contributors

zzanzu avatar

Watchers

 avatar

acmicpc's Issues

3649๋ฒˆ / ๋กœ๋ด‡ ํ”„๋กœ์ ํŠธ / Greedy

3

  • ์ด ๋ฌธ์ œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ค„์ด๋Š๋ƒ๊ฐ€ ๊ด€๊ฑด์ด๋‹ค

  • ์ผ๋‹จ์€ ๋ง‰๋Œ€๊ธฐ ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ๊ณ ,

  • ๋งจ ์ฒ˜์Œ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€,

  1. ๋งจ ์™ผ์ชฝ์„ ๊ณ ์ •์‹œ์ผฐ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์˜ค๋ฅธ์ชฝ์—์„œ ์ ์  ์™ผ์ชฝ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉฐ ํ•ฉ์„ ๊ตฌํ•ด๋ณธ๋‹ค
  2. ๊ทธ๋ž˜๋„ ์—†๋‹ค๋ฉด ๋งจ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์„œ 1๋ฒˆ ๊ณผ์ •์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

๊ฐ•์‚ฌ๋‹˜์˜ ์‹ฌํ”Œํ•œ ๋Œ€๋‹ต(๊ณ ์ˆ˜๋Š” ๊ฐ„๊ฒฐํ•˜๋‹ค)

while(start < end) {
    int sum = length[start] + length[end];
    
    if(length > N) {
        end--:
    } else if(length < N) {
        start++;
    } else {
        find = true;
        break;
    }
}

if(find == true) {
    sysout(yes ~~);
else {
    sysout("danger");
}

2621๋ฒˆ / ์นด๋“œ ๊ฒŒ์ž„ / ์นด์šดํŒ… ๊ธฐ๋ฒ•

1

์นด๋“œ์˜ ์ƒ‰๊น”๊ณผ ์ˆซ์ž๋ฅผ ๋ณด๊ณ  5์žฅ์˜ ์นด๋“œ์˜ ์ƒ‰๊น”๊ณผ ์ˆซ์ž์˜ ํŠน์ง•๋“ค์„ ๊ฐ€์ง€๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ์นด๋“œ์˜ ์ƒ‰๊น”๊ณผ ์ˆซ์ž์˜ ํŠน์ง•๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ๋งŒ๋“ค๊ณ , ๊ฐ ์ธ๋ฑ์Šค๋Š” ์ถœํ˜„ ํšŸ์ˆ˜๋ฅผ ์„ธ๊ฒŒ ๋œ๋‹ค.

2018-08-20 10 52 13

์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์นด๋“œ์˜ ์ƒ‰๊น”(color)์™€ ์ˆซ์ž(num)์— ๋Œ€ํ•œ ๊ฐ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

List, ๋ฐฐ์—ด ๋‚ด๋ถ€์˜ ๊ฐ์ฒด๋ฅผ ์ •๋ ฌํ•˜๊ธฐ

๊ฐ์ฒด ๋ฐฐ์—ด ์ •๋ ฌํ•˜๊ธฐ

  • Comparable : ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋†ˆ, ํด๋ž˜์Šค ์ •์˜๋ฅผ ํ•  ๋•Œ implementํ•˜๊ณ  ๋งŒ๋“ค์–ด์ค€๋‹ค.
        Employee[] data = new Employee[5];
		
	data[0] = new Employee(1,2);
	data[1] = new Employee(5,4);
	data[2] = new Employee(4,5);
	data[3] = new Employee(3,3);
	data[4] = new Employee(2,1);
    
        ...
        Arrays.sort(data); 
}

class Employee implements Comparable<Employee> { // 'Employee๋Š” Employee์™€ ๋น„๊ต ๊ฐ€๋Šฅํ•˜๋‹ค'์˜ ์˜๋ฏธ
	int rank1;
	int rank2;
	
	public Employee(int rank1, int rank2) {
		this.rank1 = rank1;
		this.rank2 = rank2;
	}

	@Override
	public String toString() {
		return "Employee [rank1=" + rank1 + ", rank2=" + rank2 + "]";
	}

	@Override
	public int compareTo(Employee arg0) {
		// this - ๋‚˜ ์ž์‹  , arg0 - ๋น„๊ต ๋Œ€์ƒ
		return Integer.compare(this.rank1, arg0.rank1); // ๋‚˜ ์ž์‹ (this)์™€ ๋‹ค๋ฅธ ๊ฒƒ(arg0)์„ ๋น„๊ตํ•œ๋‹ค
	}
	
	
}
  • Comparator : ๋น„๊ตํ•ด์ฃผ๋Š” ๋†ˆ
Arrays.sort(data, new Comparator<Employee>() {
	@Override
	public int compare(Employee arg0, Employee arg1) {
		return Integer.compare(arg0.rank1, arg1.rank1);
	}
});

or
// comparator๋ฅผ ๋งค๋ฒˆ ์ƒ์„ฑํ•˜๊ธฐ ๋ฒˆ๊ฑฐ๋กœ์šฐ๋‹ˆ๊นŒ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋†“๊ณ  ์“ฐ๊ธฐ
Comparator<Employee> cc =  new Comparator<Employee>() {
	@Override
	public int compare(Employee arg0, Employee arg1) {
		return Integer.compare(arg0.rank1, arg1.rank1);
	}
};

Arrays.sort(data, cc);

class Employee {
	int rank1;
	int rank2;
	
	public Employee(int rank1, int rank2) {
		this.rank1 = rank1;
		this.rank2 = rank2;
	}

	@Override
	public String toString() {
		return "Employee [rank1=" + rank1 + ", rank2=" + rank2 + "]";
	}
	
	
}

๋ฆฌ์ŠคํŠธ ์ •๋ ฌํ•˜๊ธฐ

public class SortingTest {

	public static void main(String[] args) {
		Employee[] data = new Employee[5];
		
		data[0] = new Employee(1,2);
		data[1] = new Employee(5,4);
		data[2] = new Employee(4,5);
		data[3] = new Employee(3,3);
		data[4] = new Employee(2,1);
		
		System.out.println(Arrays.toString(data));
		
		Comparator<Employee> cc =  new Comparator<Employee>() {
			@Override
			public int compare(Employee arg0, Employee arg1) {
				return Integer.compare(arg0.rank1, arg1.rank1);
			}
		};
		
		ArrayList<Employee> list = new ArrayList<>();
		for(int i = 0; i < data.length; i++) {
			list.add(data[i]);
		}
		
		System.out.println(data);
		list.sort(cc);
	}

}

class Employee {
	int rank1;
	int rank2;
	
	public Employee(int rank1, int rank2) {
		this.rank1 = rank1;
		this.rank2 = rank2;
	}

	@Override
	public String toString() {
		return "Employee [rank1=" + rank1 + ", rank2=" + rank2 + "]";
	}
	
	
}
  • ๋ฆฌ์ŠคํŠธ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋“ค์€ Collections.~~~
  • ๋ฐฐ์—ด ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋“ค์€ Arrays.~~~

1987๋ฒˆ / ์•ŒํŒŒ๋ฒณ / ๋ฐฑํŠธ๋ž˜ํ‚น

2018-08-23 10 23 56

  • ์˜ค๋Š˜์€ ๋ฐฑํŠธ๋ž˜ํ‚น ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋‹ค. DFSํ•˜๋Š” ๊ฑฐ๋ž‘ ๋ฐฉ๋ฒ•์ด ๋น„์Šทํ•œ๋ฐ, ๊ธฐ์กด์˜ DFS๋Š”

DFS

  1. ๋ฐฉ๋ฌธ ์ฒดํฌ
  2. ์—ฐ๊ฒฐ๋œ ๊ธธ ์ฐพ๊ธฐ
  3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ ์ฐพ๊ธฐ
  4. ๊ฐ„๋‹ค

๋ผ๊ณ  ํ•œ๋‹ค๋ฉด,

๋ฐฑํŠธ๋ž˜ํ‚น

๋ฐฑํŠธ๋ž˜ํ‚น์€ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰์— ๋‹จ๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

  1. ๋ฐฉ๋ฌธ ์ข…๋ฃŒ ์กฐ๊ฑด ์ฒดํฌ
  2. ๋ฐฉ๋ฌธ ์ฒดํฌ
  3. ์—ฐ๊ฒฐ๋œ ๊ธธ ์ฐพ๊ธฐ
  4. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ ์ฐพ๊ธฐ
  5. ๊ฐ„๋‹ค
  6. ๋ฐฉ๋ฌธ ์ฒดํฌ ํ•ด์ œ
  • ๋ฐฉ๋ฌธ ์ข…๋ฃŒ ์กฐ๊ฑด์€ ๋ฌธ์ œ๋งˆ๋‹ค ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฑฐ๋ฅผ ์ž˜ ๊ณ ๋ คํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ์ƒ๊ฐํ•ด๋‚ด์•ผํ•œ๋‹ค
  • 5๋ฒˆ ๋ฐฉ๋ฌธ ์ฒดํฌ ํ•ด์ œ๋Š” for๋ฌธ์ด ๋๋‚˜๊ณ (์—ฐ๊ฒฐ๋œ ๊ธธ๋“ค ๋ชจ๋‘ ๊ฒ€์ƒ‰ ํ›„) ์‹คํ–‰๋˜์–ด์ ธ์•ผํ•œ๋‹ค!
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Problem1987 {
	static Set<Integer> set;
	static int R, C;
	static int[][] map;
	
	static int[] dx = {0, 0, 1, -1};
	static int[] dy = {1, -1, 0, 0};
	static int max = Integer.MIN_VALUE;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		set = new HashSet<>();
		
		R = sc.nextInt();
		C = sc.nextInt();
		
		map = new int[R][C];
		
		for(int i = 0; i < R; i++) {
			String temp = sc.next();
			
			for(int j = 0 ; j < C; j++) {
				map[i][j] = temp.charAt(j);
			}
		}
		
		set.add(map[0][0]);
		dfs(0, 0, 1);
		
		System.out.println(max);
		
	}
	
	static void dfs(int x, int y, int count) {
		// 0. ์ข…๋ฃŒ ์ฒดํฌ
		if(count > max) {
			max = count;
		}
		
		// 1. ๋ฐฉ๋ฌธ ์ฒดํฌ
		set.add(map[y][x]);
		// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ ์ฒดํฌ
		for(int i = 0; i < 4; i++) {
			int targetX = x + dx[i];
			int targetY = y + dy[i];
			
			// 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ ์ฒดํฌ
			if(targetX >= 0 && targetX < C && targetY >= 0 && targetY < R) {
				if(!set.contains(map[targetY][targetX])) {
					// 4. ๊ฐ„๋‹ค
					dfs(targetX, targetY, count+1);
				}
			}
		}
		// 5. ๋ฐฉ๋ฌธ ์ฒดํฌ ํ•ด์ œ
		set.remove(map[y][x]);
	}

}

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด visited ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์„œ ํƒ์ƒ‰ํ•œ ์•ŒํŒŒ๋ฒณ์„ ํ‘œ์‹œํ•ด์ฃผ๊ณ 
์ข…๋ฃŒ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๊ธฐ์œ„ํ•ด ์ด๊ฑธ ๋ณด๊ณ  ๋งค๋ฒˆ ์ฒดํฌํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

ํ•˜์ง€๋งŒ ๊ฐ•์‚ฌ๋‹˜๊ป˜์„œ Set๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์†Œ๊ฐœํ•ด์ฃผ์…จ๊ณ ,
์ด๊ฒƒ์˜ ํŠน์ง•์€ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•  ๋•Œ(add) ์ค‘๋ณต์„ ํ—ˆ๋ฝํ•˜์ง€ ์•Š๊ณ  ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—
์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•˜๋‹ค๊ณ  ํ•˜์…จ๋‹ค. ํŠนํžˆ contains()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด set์— ๋‚ด๊ฐ€ ํƒ์ƒ‰ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ด
๊ธฐ์กด์— ์ €์žฅ๋˜์—ˆ๋Š”์ง€(ํƒ์ƒ‰ํ•˜์˜€๋Š”์ง€) ํ™•์ธํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ๋” ๊ฐ„๋‹จํ•ด์กŒ๋‹ค.

Set<Integer> set = new HashSet<>();

set.add(์ถ”๊ฐ€ํ•  ๋ฐ์ดํ„ฐ);

set.contains(์ฐพ๊ณ ์žํ•˜๋Š” ๋ฐ์ดํ„ฐ); // ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์ฒดํฌ

set.remove(์ง€์šฐ๊ณ ์žํ•˜๋Š” ๋ฐ์ดํ„ฐ);

1759๋ฒˆ / ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ / ๋ฐฑํŠธ๋ž˜ํ‚น

  • ๋ฐฑํŠธ๋ž˜ํ‚น
  • ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•˜๊ณ  ๋ฐ”๋กœ ์ด์ „ ๋ถ„๊ธฐ๋กœ ๋Œ์•„๊ฐ€๋Š” ํƒ€์ด๋ฐ์— ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ํ•ด์ง€ํ•ด์•ผํ•œ๋‹ค! ( == for๋ฌธ ์ข…๋ฃŒ ์‹œ์ )
package example04;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Problem1759 {
	static int L, C;
	static Character[] data;
	static boolean[] visited;
	static LinkedList<String> result;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		L = sc.nextInt();
		C = sc.nextInt();

		data = new Character[C];
		visited = new boolean[C];

		for (int i = 0; i < C; i++) {
			String temp = sc.next();
			data[i] = temp.charAt(0);
		}
		
		Arrays.sort(data);
		result = new LinkedList<String>();

		for (int i = 0; i < data.length - 3; i++) {
			if (isVow(data[i])) {
				Password pwd = new Password(data[i], 1, 0, 1, "" + data[i]);
				dfs(pwd);
			} else {
				Password pwd = new Password(data[i], 1, 1, 0, "" + data[i]);
				dfs(pwd);
			}
		}
		
		for(String r : result) {
			System.out.println(r);
		}
		
	}

	public static void dfs(Password pwd) {
		Password currentPwd = pwd;

		// 0. ์ข…๋ฃŒ ์กฐ๊ฑด ์ฒดํฌ
		if (currentPwd.depth == L) { // ์ด๋ถ€๋ถ„์„ 4๋กœ ํ–ˆ์—ˆ๋„ค;;
			if (currentPwd.con >= 2 && currentPwd.vow >= 1) {
				// ๊ฒฐ๊ณผ ์ €์žฅ
				result.add(currentPwd.pwd);
			}
		}

		// 1. ๋ฐฉ๋ฌธ ์ฒดํฌ
		visited[check(currentPwd.current)] = true;

		// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ
		for (int i = check(currentPwd.current); i < data.length; i++) {
			// 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ
			if (visited[i] == false) {
				// 4. ๊ฐ„๋‹ค.
				if (isVow(data[i])) {
					Password nextPwd = new Password(data[i], currentPwd.depth + 1, 
							currentPwd.con, currentPwd.vow + 1, currentPwd.pwd + data[i]);

					dfs(nextPwd);
				} else {
					Password nextPwd = new Password(data[i], currentPwd.depth + 1,
							currentPwd.con + 1, currentPwd.vow, currentPwd.pwd + data[i]);

					dfs(nextPwd);

				}
			}
		}
                // ๋ฐฑํŠธ๋ž˜ํ‚น!
		visited[check(currentPwd.current)] = false;

	}

	// data ๋ฐฐ์—ด๋‚ด์—์„œ ํ•ด๋‹น ๋ฌธ์ž์˜ index๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
	public static int check(char c) {
		for (int i = 0; i < data.length; i++) {
			if (c == data[i]) {
				return i;
			}
		}
		return -1;
	}

	// ๋ชจ์Œ ์ฒดํฌ
	public static boolean isVow(char c) {
		if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
			return true;
		}

		return false;
	}

}

class Password {
	char current;
	int depth;
	int con; // ์ž์Œ
	int vow; // ๋ชจ์Œ
	String pwd;

	public Password(char current, int depth, int con, int vow, String pwd) {
		this.current = current;
		this.depth = depth;
		this.con = con;
		this.vow = vow;
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "Password [current=" + current + ", depth=" + depth + ", con=" + con + ", vow=" + vow + ", pwd=" + pwd
				+ "]";
	}

}

11403๋ฒˆ / ๊ฒฝ๋กœ์ฐพ๊ธฐ / BFS

public class Problem11403 {
	static int N;
	static int[][] map;
	static int[][] result;
	static boolean[] visited;
	static Queue<Integer> queue;
	static boolean[] isExist;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		map = new int[N+1][N+1];
		result = new int[N][N];
		visited = new boolean[N+1];
		queue = new LinkedList<>();
		isExist = new boolean[N+1];
		
		for(int y = 1; y <= N; y++) {
			
			for(int x = 1; x <= N; x++) {
				map[y][x] = sc.nextInt();
				
				if(map[y][x] == 1) isExist[y] = true;
			}
		}
		
		for(int y = 1; y <= N; y++) {
			visited = new boolean[N+1];
			
			if(isExist[y] == true) {
                                  // 0. ํ์—๋‹ค๊ฐ€ ์‹œ์ž‘ ์ง€์  ๋„ฃ์–ด์ฃผ๊ธฐ
				queue.add(y);
                                  // 0. ์‹œ์ž‘ ์ง€์  ๋ฐฉ๋ฌธ ์ฒดํฌํ•˜๊ธฐ
//				visited[y] = true; // ๋ฐฉ๋ฌธ์ฒดํฌํ•˜๋Š” ํƒ€์ด๋ฐ์ด ๊ด€๊ฑด์ด๊ตฐ
				
				while(queue.isEmpty() == false) {
                                          // 1. ํ์—์„œ ๊บผ๋‚ด์˜ค๊ธฐ
					int current = queue.poll();
				        // 2. ์—ฐ๊ฒฐ๋œ ๊ธธ ์ฐพ๊ธฐ
					for(int i = 1; i <= N; i++) {
                                                   // 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ ์ฐพ๊ธฐ
						if(map[current][i] == 1 && visited[i] == false) {
                                                            // 4. ํ์— ๋„ฃ๊ธฐ
							queue.add(i);
							visited[i] = true; // ์—ฌ๊ธฐ์„œ ๋ฐฉ๋ฌธ์ฒดํฌ!
							result[y-1][i-1] = 1;
						}
					}
				}
			}
		}
		
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				System.out.print(result[i][j] + " ");
			}
			System.out.println();
		}
		
		
	}

}

2178๋ฒˆ / ๋ฏธ๋กœ ํƒ์ƒ‰ / DFS

https://www.acmicpc.net/problem/2178

2

์šฐ์„ ์€ DFS๋กœ ํ’€์–ด๋ด„.

	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
...
		// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ์„ ์ฐพ๋Š”๋‹ค.(์ƒํ•˜์ขŒ์šฐ, 4๊ฐ€์ง€) dx, dy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ง‘์ค‘!
		for(int i = 0; i < 4; i++) {
			int targetX = x + dx[i];
			int targetY = y + dy[i];
			
  • ํ•œ ์œ„์น˜์—์„œ ์ƒ,ํ•˜,์ขŒ,์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์œ„์น˜ (x, y)์— ๋ฏธ๋ฆฌ ์ •์˜ํ•œ ํƒ์ƒ‰ ์ขŒํ‘œ ์ด๋™๊ฑฐ๋ฆฌ dx, dy๋ฅผ ์ •์˜ํ•ด์„œ for๋ฌธ์•ˆ์—์„œ ํƒ์ƒ‰

  • ์ฑ„์  ์ƒ์œผ๋กœ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ

๊ธฐ์กด ์ฝ”๋“œ

package example02;

import java.util.Arrays;
import java.util.Scanner;

public class Problem2178 {
	static int N, M;
	static int[][] map;
	static boolean[][] visited;
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
	static int min = Integer.MAX_VALUE;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		sc.nextLine();
		
		// ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ 1,2,3 ์ด๋Ÿฐ๊ฒŒ ์•„๋‹ˆ๋ผ์„œ ๊ทธ๋ƒฅ N,M ์จ์คŒ
		map = new int[N][M];
		visited = new boolean[N][M];
		
		// ์—ฌ๊ธฐ์„œ int๋กœ String์˜ ํ•œ ๊ธ€์ž๋ฅผ ๋ฐ›์•„๋ณด๋Š” ๋ฐฉ์‹ ใ…‡ใ…‡
		for(int y = 0; y < N; y++) {
			String temp = sc.nextLine();
			
			for(int x = 0; x < M; x++) {
				if(temp.charAt(x) == '1') {
					map[y][x] = 1;
				} else {
					map[y][x] = 0;
				}
			}
		}
		
		dfs(0, 0, 1);
		System.out.println(min);
	}
	
	public static void dfs(int x, int y, int current) {
		// 0. ๋ชฉ์ ์ง€ ๋„๋‹ฌ ์—ฌ๋ถ€ ์ฒดํฌ , ์ด ๋ถ€๋ถ„์„ ์ƒ๊ฐ ๋ชปํ•จ!
		if(y == N-1 && x == M-1) {
			if(current < min) {
				min = current;
			}
			return; // ์“ธ๋ฐ์—†๋Š” ๊ธธ ํƒ์ƒ‰ ์•ˆํ•˜๊ฒŒ ํ•ด์คŒ
		}
		// 1. ๋ฐฉ๋ฌธ ๊ธฐ๋ก์„ ๋‚จ๊ธด๋‹ค.
		visited[y][x] = true;
		
		// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ์„ ์ฐพ๋Š”๋‹ค.(์ƒํ•˜์ขŒ์šฐ, 4๊ฐ€์ง€) dx, dy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ง‘์ค‘!
		for(int i = 0; i < 4; i++) {
			int targetX = x + dx[i];
			int targetY = y + dy[i];
			
			if(targetX >= 0 && targetX < M && targetY >= 0 && targetY < N) {
				// 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์„ ์ฐพ๋Š”๋‹ค.
				if(map[targetY][targetX] == 1 && visited[targetY][targetX] == false){
					// 4. ๊ฐ„๋‹ค
					dfs(targetX, targetY, current+1);
				}				
			}
		}
		// 5. ๋ฐฉ๋ฌธ ํ•ด์ง€
		visited[y][x] = false;
	}

}

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ visited๋ฅผ intํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๊ฐ ์นธ์ด ๋ฐฉ๋ฌธ์ด ๋˜์—ˆ์„ ๋•Œ ๊ฑฐ๊ธฐ๋ฅผ ์ง€๋‚˜๊ฐ„ ๋‹น์‹œ์˜ ๊ฑฐ๋ฆฌ(current)๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๋ฒˆ์— ๊ทธ ์นธ์„ ์ง€๋‚˜๊ฐ€๊ฒŒ ๋˜์—ˆ์„ ๋•Œ์˜ current์™€ visited(์ด์ „์˜ ๋ฐฉ๋ฌธ์‹œ ๊ฑฐ๋ฆฌ)๋ฅผ ๋น„๊ตํ•œ๋‹ค.

public static void dfs(int x, int y, int current) {
		// 0. ๋ชฉ์ ์ง€ ๋„๋‹ฌ ์—ฌ๋ถ€ ์ฒดํฌ , ์ด ๋ถ€๋ถ„์„ ์ƒ๊ฐ ๋ชปํ•จ!
		if(y == N-1 && x == M-1) {
			if(current < min) {
				min = current;
			}
			return; // ์“ธ๋ฐ์—†๋Š” ๊ธธ ํƒ์ƒ‰ ์•ˆํ•˜๊ฒŒ ํ•ด์คŒ
		}
		// 1. ๋ฐฉ๋ฌธ ๊ธฐ๋ก์„ ๋‚จ๊ธด๋‹ค.
		visited[y][x] = current;
		
		// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ์„ ์ฐพ๋Š”๋‹ค.(์ƒํ•˜์ขŒ์šฐ, 4๊ฐ€์ง€) dx, dy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ง‘์ค‘!
		for(int i = 0; i < 4; i++) {
			int targetX = x + dx[i];
			int targetY = y + dy[i];
			
			if(targetX >= 0 && targetX < M && targetY >= 0 && targetY < N) {
				// 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์„ ์ฐพ๋Š”๋‹ค.
				if(map[targetY][targetX] == 1){
					if(visited[targetY][targetX] == 0 || visited[targetY][targetX] > current+1)
					// 4. ๊ฐ„๋‹ค
					dfs(targetX, targetY, current+1);
				}				
			}
		}
		// 5. ๋ฐฉ๋ฌธ ํ•ด์ง€
//		visited[y][x] = 0;
	}
  • '1. ๋ฐฉ๋ฌธ ๊ธฐ๋ก์„ ๋‚จ๊ธด๋‹ค' ๋‹จ๊ณ„์—์„œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•ด๋‹น ์œ„์น˜๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•œ๋‹ค
    ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๋ฒˆ์— ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜์—ˆ์„ ๋•Œ, ์ด ๊ฐ’(visited)์„ current์™€ ๋น„๊ตํ•˜์—ฌ ํƒ์ƒ‰์„ ๋” ํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋œ๋‹ค.

  • ๋ฐฉ๋ฌธ ํ•ด์ง€๋Š” ํ•˜์ง€ ์•Š๋Š”๋‹ค!

7576๋ฒˆ / ํ† ๋งˆํ†  / BFS

1

  • ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ
package example03;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Problem7576 {
	static int N, M;
	static int[][] visited; // ์ต์€ ์‹œ๊ธฐ์˜ ์‹œ๊ฐ„
	static int[][] map;
	static Queue<Position3> queue;
	
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
	static int max = Integer.MIN_VALUE;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		M = sc.nextInt();
		N = sc.nextInt();
		map = new int[N][M];
		visited = new int[N][M];
		queue = new LinkedList<>();
		
		for(int y = 0 ; y < N; y++) {
			for(int x = 0; x < M; x++) {
				map[y][x] = sc.nextInt();
			}
		}
		
		// ์ฉ์€ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋Š” ์œ„์น˜ ํ์— ์ €์žฅ
		for(int y = 0 ; y < N; y++) {
			for(int x = 0; x < M; x++) {
				if(map[y][x] == 1) {
					Position3 pos = new Position3(x, y, 1);
					visited[y][x] = 1;
					queue.add(pos);
				}
			}
		}
		
		while(queue.isEmpty() == false) {
			Position3 currentPos = queue.poll();
			visited[currentPos.y][currentPos.x] = currentPos.time;
			
			if(max < currentPos.time) {
				max = currentPos.time;
			}
			
			for(int i = 0; i < 4; i++) {
				int targetX = currentPos.x + dx[i];
				int targetY = currentPos.y + dy[i];
				
				if(targetX >= 0 && targetX < M && targetY >= 0 && targetY < N) {
					if(map[targetY][targetX] != -1 && map[targetY][targetX] == 0) {
						if(visited[targetY][targetX] == 0) {
							Position3 nextPos = new Position3(targetX, targetY, currentPos.time + 1);
							queue.add(nextPos);
						}
					}
				}
			}
			
		}
		
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < M; j++) {
				if(visited[i][j] == 0 && map[i][j] == 0) {
					System.out.println(-1);
					return;
				}
			}
		}
		
		System.out.println(max-1);
		
		
	}

}

class Position3 {
	int y;
	int x;
	int time;
	
	public Position3(int x, int y, int time) {
		this.y = y;
		this.x = x;
		this.time = time;
	}
}
  • ํ•ด๊ฒฐํ•œ ์ฝ”๋“œ์˜ ์ˆ˜์ • ๋ถ€๋ถ„
while(queue.isEmpty() == false) {
	Position3 currentPos = queue.poll();
			
	if(max < currentPos.time) {
		max = currentPos.time;
	}
			
	for(int i = 0; i < 4; i++) {
		int targetX = currentPos.x + dx[i];
		int targetY = currentPos.y + dy[i];
		
		if(targetX >= 0 && targetX < M && targetY >= 0 && targetY < N) {
			if(map[targetY][targetX] != -1 && map[targetY][targetX] == 0) {
				if(visited[targetY][targetX] == 0) {
					Position3 nextPos = new Position3(targetX, targetY, currentPos.time + 1);
					queue.add(nextPos);
					
					visited[targetY][targetX] = currentPos.time+1;
				}
			}
		}
	}
			
}
  • ์›์ธ์€ BFS๋ฅผ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” while๋ฌธ์— ์žˆ์—ˆ๋‹ค.
  • ์—ฌ๊ธฐ์„œ visited๋Š” ํ•ด๋‹น ์œ„์น˜์˜ ํ† ๋งˆํ† ๊ฐ€ ์ฉ์€ ์ˆœ๊ฐ„์˜ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๊ฒƒ์„ ํ์— ์ €์žฅ(queue.add)ํ•˜๋Š” ์ˆœ๊ฐ„์— ์ €์žฅํ•  ๊ฒƒ์ธ์ง€, ์•„๋‹ˆ๋ฉด while๋ฌธ์ด ์‹œ์ž‘๋˜๋Š” ์ˆœ๊ฐ„์— currentPos์˜ time๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜์—ฌ ๋ณ€ํ™”์‹œ์ผœ์ค„ ๊ฒƒ์ธ์ง€๊ฐ€ ๊ด€๊ฑด์ด์—ˆ๋‹ค.

11724๋ฒˆ / ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ / BFS

screen shot 2018-09-26 at 19 49 21

package bfs;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Problem11724 {
	static int N, M;
	static boolean[][] map;
	static boolean[] visited;
	static boolean[] check;
	static Queue<Integer> queue;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		
		map = new boolean[N+1][N+1];
		visited = new boolean[N+1];
		check = new boolean[N+1];
		queue = new LinkedList<>();
		
		for(int i = 0; i < M; i++) {
			int u = sc.nextInt();
			int v = sc.nextInt();
			
			map[u][v] = true;
			map[v][u] = true;
			check[u] = true;
			check[v] = true;
		}
		
		int count = 0;
		for(int i = 1; i <= N; i++) {
			if(check[i] == true && visited[i] == false) {
				// 0. ํ์—๋‹ค๊ฐ€ ์‹œ์ž‘ ์ง€์  ๋„ฃ์–ด์ฃผ
				queue.add(i);
				// 0. ์‹œ์ž‘์ง€์  ๋ฐฉ๋ฌธ ๊ธฐ๋ก ๋‚จ๊ธฐ
				visited[i] = true;
				
				while(queue.isEmpty() == false) {
					// 1. ํ์—์„œ ๊บผ๋‚ด์˜ค๊ธฐ
					int current = queue.poll();
					
					// 2. ์—ฐ๊ฒฐ๋œ ๊ธธ๋“ค ์ฐพ๊ธฐ
					for(int j = 1; j <= N; j++) {
						// 3. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ ์ฐพ๊ธฐ
						if(map[current][j] == true && visited[j] == false) {
							// 4. ํ์— ๋„ฃ๊ธฐ(๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ)
							queue.add(j);
							// 5. ๋ฐฉ๋ฌธ ์ฒดํฌ(๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ)
							visited[j] = true;
						}
					}
				}
				count++;
			} else if(check[i] == false) {
				count++;
			}
		}
		
		System.out.println(count);
	}

}
  • ๊ธฐ๋ณธ์ ์ธ BFS ๋ฌธ์ œ
  • DFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์„๋“ฏ
  • else if ๋ถ€๋ถ„์„ ์ƒ๊ฐ ๋ชปํ•ด์„œ 2๋ฒˆ ํ‹€๋ฆผ : ์•„๋ฌด ๊ฐ„์„ ๋„ ์—ฐ๊ฒฐ ์•ˆ๋œ ๊ฒฝ์šฐ์—๋„ count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค˜์•ผ๋œ๋‹ค.

untitled

  • else if ๋ถ€๋ถ„์ด ์—†๋‹ค๋ฉด 2๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ๋œ๋‹ค. ๋”ฐ๋ผ์„œ check[]๊ฐ€ false์ธ ๋ถ€๋ถ„์€ ์•„๋ฌด ๊ฐ„์„ ๋„ ์—ฐ๊ฒฐ์ด ์•ˆ๋œ ๋…ธ๋“œ์ด๋ฏ€๋กœ ์ด๊ฒƒ์„ ์ฒดํฌํ•ด์„œ count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

๊ทผ๋ฐ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ์งฐ๋‹ค...

1205๋ฒˆ

๋ฐฐ์—ด.indexOf( )
๋ฐฐ์—ด.lastIndexOf( ) ์‚ฌ์šฉํ•˜๊ธฐ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.