Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

김기찬 / 10월 4주차 / 월 #290

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

TearofCoding
Copy link
Contributor


🎈boj 12919- A와B2


🗨 해결방법 :


📝메모 :


✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;

//12919번
public class A와B2 {
	static boolean able = false;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String s = br.readLine();
		String t = br.readLine();

		sol(s, t);
		
		if(able) System.out.println(1);
		else System.out.println(0);
		
	}

	private static void sol(String s, String t) {
		if(able) return;
		if(s.length() == t.length()) {
			if(Objects.equals(s, t)) {
				able = true;
			}
			return;
		}
		
		if(t.charAt(t.length()-1) == 'A') {
			sol(s, t.substring(0, t.length()-1));
		}

		if(t.charAt(0) == 'B') {
			String substr = t.substring(1);
			StringBuilder sb = new StringBuilder(substr);
			sb.reverse();
			sol(s, sb.toString());
		}

		
	}
	
}

🎈boj 10159 - 저울


🗨 해결방법 :


📝메모 :


✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//10159번
public class 저울 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int m = Integer.parseInt(br.readLine());
		
		int[][] dist = new int[n+1][n+1];

		for(int i = 0; i < m; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			int from = Integer.parseInt(st.nextToken());
			int to = Integer.parseInt(st.nextToken());
			dist[from][to] = 1;
		}
		
		for(int k = 1; k <= n; k++) {
			for(int i = 1; i <= n; i++) {
				for(int j = 1; j <= n; j++) {
					if(dist[i][k]==1 && dist[k][j]==1) {
						dist[i][j] = 1;
					}
				}
			}
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 1; i <= n; i++) {
			int count = 0;
			for(int j = 1; j <= n; j++) {
				if(i==j) continue;
				if(dist[i][j] == 0 && dist[j][i] == 0) count++;
			}
			sb.append(count).append("\n");
		}
		System.out.println(sb);
		
	}
}

🎈boj 18430 - 무기공학


🗨 해결방법 :


📝메모 :


✔코드 :

package boj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 무기공학 {
	private static int[][] dxdy = {{1,0},{0,-1},{-1,0},{0,1}};//아래 왼쪽 위 오른쪽
	private static int n;
	private static int m;
	private static boolean[][] used;
	private static int[][] tree;
	private static int maxScore;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		
		tree = new int[n][m];
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j = 0; j < m; j++) {
				tree[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		used = new boolean[n][m];
		maxScore = 0;
		
		sol(0, 0, 0);
		
		System.out.println(maxScore);
		
		
	}

	private static void sol(int x, int y, int score) {
		if(x==n) {
			//최대값 찾기
			if(maxScore < score) {
				maxScore = score;
			}
			return;
		}
		
		if(!used[x][y]) {
			//4가지 부메랑 -> 다음 좌표 -> 전부 시도하면 빈칸으로
			for(int dir = 0; dir < 4; dir++) {
				int dir2 = (dir+1)%4;
				int nx1 = x + dxdy[dir][0];
				int ny1 = y + dxdy[dir][1];
				int nx2 = x + dxdy[dir2][0];
				int ny2 = y + dxdy[dir2][1];
				if(inRange(nx1, ny1) && inRange(nx2, ny2)) {
					if(!used[nx1][ny1] && !used[nx2][ny2]) {
						used[x][y] = true;
						used[nx1][ny1] = true;
						used[nx2][ny2] = true;

						if(y+1<m) {
							sol(x, y+1, score + tree[x][y]*2 + tree[nx1][ny1] + tree[nx2][ny2]);
						}else {
							sol(x+1, 0, score + tree[x][y]*2 + tree[nx1][ny1] + tree[nx2][ny2]);
						}
						
						used[nx2][ny2] = false;
						used[nx1][ny1] = false;
						used[x][y] = false;
						
					}
				}
			}
		}
		//다음좌표로
		if(y+1<m) {
			sol(x, y+1, score);
		}else {
			sol(x+1, 0, score);
		}
		
	}
	
	private static boolean inRange(int x, int y) {
		return x>=0 && y>=0 && x<n && y<m;
	}
}

@TearofCoding TearofCoding merged commit c32ba19 into SSAFY-10th-Seoul17:main Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants