반응형

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

 

2638번: 치즈

첫째 줄에는 모눈종이의 크기를 나타내는 두 개의 정수 N, M (5 ≤ N, M ≤ 100)이 주어진다. 그 다음 N개의 줄에는 모눈종이 위의 격자에 치즈가 있는 부분은 1로 표시되고, 치즈가 없는 부분은 0으로

www.acmicpc.net

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

class Point{
    int x, y;
    Point(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}

public class Main {

    static int dx[] = {1,0,-1,0};
    static int dy[] = {0,1,0,-1};
    static int map[][] = new int[101][101];

    static Queue<Point> Q = new LinkedList<>();
    static Queue<Point> willDisappear = new LinkedList<>();

    static boolean isContact(int x, int y){
        int cnt = 0;
        for(int i=0; i<4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx<0 || nx>n || ny<0 || ny>m) continue;
            if(map[nx][ny]==-1){
                cnt++;
            }
        }
        if(cnt>=2) return true;
        else return false;
    }

    static int n,m;

    static void spreadAir(){
        while(!Q.isEmpty()){
            Point cur = Q.poll();

            for(int i=0; i<4; i++){
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];
                if(nx<0 || nx>n || ny<0 || ny>m) continue;
                if(map[nx][ny]==0){
                    map[nx][ny] = -1;
                    Q.add(new Point(nx, ny));
                }
            }
        }
    }

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

        for(int i=0; i<n; i++){
            for(int j=0;j<m; j++){
                map[i][j] = sc.nextInt();
            }
        }

        Q.add(new Point(0,0));
        Q.add(new Point(0,m-1));
        Q.add(new Point(n-1,0));
        Q.add(new Point(n-1,m-1));
        spreadAir();

        int ans = 0;
        while(true){
            boolean flag = false;
            for(int i=0; i<n; i++){
                for(int j=0;j<m; j++){
                    if(map[i][j] == 1){
                        flag = true;
                        if(isContact(i,j)){
                            willDisappear.add(new Point(i,j));
                        }
                    }
                }
            }


            if(flag==false) break;

            while(!willDisappear.isEmpty()){
                Point curr = willDisappear.poll();
                map[curr.x][curr.y] = -1;
                Q.add(curr);
            }
            spreadAir();
            ans++;
        }

        System.out.println(ans);

    }
}
반응형
반응형

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

 

4803번: 트리

입력으로 주어진 그래프에 트리가 없다면 "No trees."를, 한 개라면 "There is one tree."를, T개(T > 1)라면 "A forest of T trees."를 테스트 케이스 번호와 함께 출력한다.

www.acmicpc.net

 

1. DFS 풀이

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

public class Main {
    static boolean ch[] = new boolean[501];
    static LinkedList[] adj = new LinkedList[501];
    static boolean flag = false;

    static void DFS(int cur, int pre){
        for(Object a: adj[cur]){
            if(ch[(int)a]==true){
                if((int)a!=pre)flag = true;
            }
            else{
                ch[(int)a] = true;
                DFS((int)a, cur);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int trial = 1;
        while(true){
            int n = sc.nextInt();
            int k = sc.nextInt();
            if(n==0) break;
            int trees = 0;
            Arrays.fill(ch,false);

            for(int i=0; i<n+1; i++){
                adj[i] = new LinkedList<Integer>();
            }

            for(int i=0; i<k; i++){
                int n1 = sc.nextInt();
                int n2 = sc.nextInt();
                adj[n1].add(n2);
                adj[n2].add(n1);
            }


            for(int i=1; i<=n; i++){
                flag = false;
                if(ch[i]==false){
                    ch[i] = true;
                    DFS(i, 0);
                    if(!flag) {
                        trees+=1;
                    }
                }
            }

            if(trees>1){
                System.out.printf("Case %d: A forest of %d trees.\n",trial, trees);
            }
            else if(trees==1) {
                System.out.printf("Case %d: There is one tree.\n",trial);
            }
            else{
                System.out.printf("Case %d: No trees.\n", trial);
            }

            trial+=1;
        }
    }
}

 

 

2.Union&Find

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

public class Main {

    static int[] unf = new int[501];

    static int Find(int a){
        if(unf[a] < 0) return a;
        else return unf[a] = Find(unf[a]);
    }

    static void Union(int a, int b){
        a= Find(a);
        b = Find(b);
        if(a!=b){
            if(unf[a]==-2){
                unf[b] = a;
            }
            else if(unf[b]==-2){
                unf[a] = b;
            }
            else if(unf[a] < 0){
                unf[b] = a;
            }
            else {
                unf[a] = b;
            }
        }
    }


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


        while(true){
            Arrays.fill(unf,-1);
            int n = sc.nextInt();
            int k = sc.nextInt();
            if(n==0) break;
            int trees = 0;


            for(int i=0; i<k; i++){
                int n1 = sc.nextInt();
                int n2 = sc.nextInt();

                n1 = Find(n1);
                n2 = Find(n2);

                if(n1==n2){
                    unf[n1] = -2;
                }
                else{
                    Union(n1,n2);
                }
            }

            for(int i=1; i<=n; i++){
                if(unf[i]==-1) trees+=1;
            }

            if(trees>1){
                System.out.printf("Case %d: A forest of %d trees.\n",trial, trees);
            }
            else if(trees==1) {
                System.out.printf("Case %d: There is one tree.\n",trial);
            }
            else{
                System.out.printf("Case %d: No trees.\n", trial);
            }

            trial+=1;
        }
    }
}
반응형
반응형

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

 

2696번: 중앙값 구하기

첫째 줄에 테스트 케이스의 개수 T(1 ≤ T ≤ 1,000)가 주어진다. 각 테스트 케이스의 첫째 줄에는 수열의 크기 M(1 ≤ M ≤ 9999, M은 홀수)이 주어지고, 그 다음 줄부터 이 수열의 원소가 차례대로 주

www.acmicpc.net

중앙값을 다이렉트하게 뽑아낼 수 있는 자료구조 라이브러리는 존재하지 않는다.

 

하지만 우선순위 큐 두개를 이용해서 중앙값을 적은 cost로 뽑아 낼 수 있다.

 

import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) throws IOException {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int t = Integer.parseInt(br.readLine());

        for(int T=0; T<t; T++){
            maxHeap.clear();
            minHeap.clear();
            int n = Integer.parseInt(br.readLine());
            bw.write((n/2 + 1) + "\n");
            int lines = n%10==0 ? n/10 : n/10 + 1;
            int cnt = 0;
            int printed = 0;
            for(int i=0; i<lines; i++){
                String[] split = br.readLine().split(" ");

                for(int j=0; j<split.length;j++){
                    int cur = Integer.parseInt(split[j]);
                    if(cnt==0){
                        minHeap.add(cur);
                    }
                    else{
                        if(cur<=minHeap.peek()){
                            maxHeap.add(cur);
                        }
                        else {
                            minHeap.add(cur);
                        }
                    }
                    cnt++;


                    if(cnt%2==1){
                        while(maxHeap.size() >= minHeap.size()){
                            minHeap.add(maxHeap.peek());
                            maxHeap.poll();
                        }

                        while(maxHeap.size() + 1 < minHeap.size()){
                            maxHeap.add(minHeap.peek());
                            minHeap.poll();
                        }
                        bw.write(minHeap.peek().toString() + " ");

                        printed++;

                        if(printed%10==0) bw.write("\n");
                    }

                }
            }
            bw.write("\n");
        }
        bw.flush();
    }
}
반응형
반응형

키보드 입력기

평상시에 잘 되던 이클립스 자동정렬 Ctrl + Shift + F 나

주석 Ctrl + Shift  + '/' 같은 이클립스 단축키가 안되서 찾아보니까

 

키보드가 한컴입력기로 되어있으면 이클립스가 인식을 못하는 거였습니다..

 

제어판 -> 언어 -> 키보드에서 Microsoft 입력기로 바꾸면

 

이클립스의 모든 단축키를 정상적으로 이용할 수 있습니다.

 

반응형
반응형

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

 

2096번: 내려가기

첫째 줄에 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 숫자가 세 개씩 주어진다. 숫자는 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 중의 하나가 된다.

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;

int n, a,b,c, a2,b2,c2, maxx[3], minn[3];
int main() {

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> a >> b >> c;
		a2 = a;
		b2 = b;
		c2 = c;

		a = a + max(maxx[0], maxx[1]);
		b = b + max({ maxx[0], maxx[1], maxx[2] });
		c = c + max(maxx[2], maxx[1]);

		maxx[0] = a;
		maxx[1] = b;
		maxx[2] = c;


		a2 = a2 + min(minn[0], minn[1]);
		b2 = b2 + min({ minn[0], minn[1], minn[2] });
		c2 = c2 + min(minn[2], minn[1]);

		minn[0] = a2;
		minn[1] = b2;
		minn[2] = c2;
	}

	cout << max({ maxx[0], maxx[1], maxx[2]}) << ' ' << min({minn[0],minn[1],minn[2]});


	return 0;
}
반응형
반응형

Makefile:4: *** missing separator.  Stop. VScode

 

Vscode로 Makefile을 만들고 make 명령어를 실행하면

 

Makefile:4: *** missing separator.  Stop.

이런 오류가 발생할 수 있다.

 

이 오류가 발생하는 이유는 Makefile의 형식에 맞지 않기 때문이다.

 

이유는 모르겠으나 Makefile은 tab을 통한 공백과 그냥 space를 통한 공백을 구분한다

 

 

파란색으로 드래그가 된 부분을

 

tab을 쳐서 띄워주고 작성해야 오류를 피할 수 있다.

 

그래도 해결되지 않는다면

 

perl -pi -e 's/^  */\t/' (Makefile파일이름)

이 명령을 실행해보자

 

 

참고자료

https://stackoverflow.com/questions/16931770/makefile4-missing-separator-stop

 

makefile:4: *** missing separator. Stop

This is my makefile: all:ll ll:ll.c gcc -c -Wall -Werror -02 c.c ll.c -o ll $@ $< clean : \rm -fr ll When I try to make clean or make make, I get this error: :makefile:4: *** m...

stackoverflow.com

 

반응형
반응형
include <cmath>

(int)(log10(구하고 싶은 수)+1)

 

반응형
반응형

WSL 글꼴 및 폰트 설정

구글에 검색하면 레지스트리 편집기를 이용해서 바꾸는 글이 많이 나오는데 

 

굳이 그러지 않아도 됩니다.

 

WSL 터미널에서

 

상단의 리눅스 펭귄을 우클릭하면 속성을 찾을 수 있습니다.

WSL 속성

속성에서 글씨체와 글씨크기등 편한대로 조작이 가능합니다.

 

 

반응형

+ Recent posts