다니고 있는 회사에서 Socket을 자주 사용한다. 매일 다른 블로거님들의 자료를 긁어오다가 나도 공부차원에서 계속해서 반복했던 패턴들을 정리하고자 한다.

 

나는 WPF에 UDP 통신이 가능한 프로그램을 만들고자 한다.

 

이름은 MySocketProjcet 로 지어준다.

 

틀만 먼저 만들어준다.

UDP Socket UI Frame

아직은 배우는 단계라 Margin 을 막써서 최대한 간결하게 UI를 구상했다.

데이터를 받는 부분은 색감을 줘서 구분을 지어줬다.

 

IP와 PORT를 기입한 후 Socket Setting 버튼을 눌러 UDP 소켓을 설정해줄 예정이다.

 

<Window x:Class="MySocketProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MySocketProject"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <TextBlock  Text="IP" Height="20" TextAlignment="Left" Margin="13,10,338,10"/>
            <TextBox Text="127.0.0.1" Width="100" Height="20" Margin="77,10,223,10"/>
        </Grid>

        <Grid Grid.Row="1">
            <TextBlock  Text="PORT" x:Name="IP_Tb" Height="20" TextAlignment="Left" Margin="13,10,338,10"/>
            <TextBox Text="5000" x:Name="PORT_Tb" Width="100" Height="20" Margin="77,10,223,10" />
            <Button Content="Socket Setting" Width="100" Height="20" Margin="296,10,4,10"/>
        </Grid>

        <Grid Grid.Row="2">
            <TextBox x:Name="ReceiveData" Background="#FFF9F2E7"/>
        </Grid>

        <Grid Grid.Row="3">
            <TextBox Text="Send Message Data" Height="20" Margin="10,10,114,10"/>
            <Button Content="Send" x:Name="SendBtn" Width="100" Height="20" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="293,0,0,0"/>
        </Grid>

    </Grid>
</Window>

 

해당 디자인의 xaml 코드이다.

 

다음장에는 Socket 클래스를 만들 예정이다.

 

UGameInstance에서 상속을 받아서 사용을 할 예정이다.

 

C++를 오랜만에 써서 그런가 Super는 자바에서 봤는데, 헷갈리기 시작한다..

 

Unreal에서는 기존의 C++에서 제공하는 string을 쓰지 않고 TEXT라는 문자열을 사용한다.

 

"MyGameInstacne.h"

#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"

/**
 * 
 */
UCLASS()
class STUDY_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()
public:
	virtual void Init() override;

	
};

 

 

"MyGameInstacne.cpp"

#include "MyGameInstance.h"

void UMyGameInstance::Init()
{
	Super::Init();

	UE_LOG(LogTemp, Log, TEXT("%s"), TEXT("Hello Unreal"));

} 

 

출력화면

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

 

10384번: 팬그램

팬그램은 모든 알파벳을 적어도 한 번씩을 사용한 영어 문장을 말한다. 다음은 유명한 팬그램 중 하나이다. The quick brown fox jumps over a lazy dog 더블 팬그램은 모든 알파벳을 적어도 두 번씩은 사용

www.acmicpc.net

A~Z까지 key로 만들어주고 key 값을 계속 돌면서 세주었다.

Pangram의 수를 어떻게 셀지 고민했는데 제일 작은 수를 출력하면 되는 거였다..

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int cnt = Integer.parseInt(bf.readLine());

        HashMap<Character, Integer> az = new HashMap<>();
        for (int i = 'a'; i <= 'z'; i++) {
            az.put((char) i, 0);
        }
        for (int i = 0; i < cnt; i++) {
            String str = bf.readLine();
            str = str.toLowerCase();
            HashMap<Character, Integer> map = new HashMap<>();
            for (int j = 0; j < str.length(); j++) {
                for (Character c : az.keySet()) {
                    if (str.charAt(j) == c) {
                        map.put(c, map.getOrDefault(c, 0) + 1);
                        break;
                    }
                }
            }
            int min = Integer.MAX_VALUE;
            for (Character c : az.keySet()) {
                if (map.getOrDefault(c, 0) == 0) {
                    min = 0;
                    break;
                }else{
                    if(min > map.get(c)){
                        min = map.get(c);
                    }
                }
            }
            if(min >=3){
                System.out.println("Case " + (i+1) + ": Triple pangram!!!");
            }else if(min == 2){
                System.out.println("Case " + (i+1) + ": Double pangram!!");
            }else if(min == 1){
                System.out.println("Case " + (i+1) + ": Pangram!");
            }else{
                System.out.println("Case " + (i+1) + ": Not a pangram");
            }
        }
    }
}

'알고리즘' 카테고리의 다른 글

[백준] 7569번 : 토마토  (0) 2023.03.06
[백준] 16499번 : 동일한 단어 그룹화하기  (0) 2023.03.05
[백준] 1439번 : 뒤집기  (0) 2023.03.04
[백준] 17413번 : 단어 뒤집기 2  (0) 2023.03.03
[백준] 14490번 : 백대열  (1) 2023.02.27

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

 

2993번: 세 부분

첫째 줄에 원섭이가 고른 단어가 주어진다. 고른 단어는 알파벳 소문자로 이루어져 있고, 길이는 3보다 크거나 같고, 50보다 작거나 같다.

www.acmicpc.net

부르트 포스 알고리즘 비슷한 유형을 풀어봐서 쉽게 푼 거 같다. 아니면 아이디어가 안 떠올랐을 거 같다.

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String str = bf.readLine();
		ArrayList<String> arrStr = new ArrayList<>();

		StringBuffer sb;

		for (int i = 0; i < str.length(); i++) {
			for (int j = i + 1; j < str.length(); j++) {
				for (int k = j + 1; k < str.length(); k++) {
					String strTemp = "";

					sb = new StringBuffer(str.substring(0, j));
					strTemp += sb.reverse().toString();
					sb = new StringBuffer(str.substring(j, k));
					strTemp += sb.reverse().toString();
					sb = new StringBuffer(str.substring(k, str.length()));
					strTemp += sb.reverse().toString();
					arrStr.add(strTemp);
				}
			}
		}
		Collections.sort(arrStr);
		System.out.println(arrStr.get(0));
	}
}

 

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,

www.acmicpc.net

머리의 한계로 3차 배열을 상상하는 거조차 어려웠던 거 같다..

pair 함수를 만들어서 사용하는데 출력 순서가 달라서 헷갈렸던 거 같다.

처음에 만든 방법은 그때그때마다 검사를 해줘서 시간 초과가 떴다.

import java.io.*;
import java.util.*;

public class Main {

	static int[] dx = { -1, 0, 1, 0 };
	static int[] dy = { 0, 1, 0, -1 };
	static int[] dh = { -1, 1 };
	static boolean[][][] visited = new boolean[101][101][101];
	static int[][][] map = new int[101][101][101];
	static int N, M, H;

	static Queue<pair> q = new LinkedList<>();

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		M = sc.nextInt();
		H = sc.nextInt();

		for (int k = 0; k < H; k++) {
			for (int i = 0; i < M; i++) {
				for (int j = 0; j < N; j++) {
					map[k][j][i] = sc.nextInt();
				}
			}
		}

		for (int k = 0; k < H; k++) {
			for (int i = 0; i < M; i++) {
				for (int j = 0; j < N; j++) {
					if (map[k][j][i] == 1) {
						q.add(new pair(i, j, k));
						visited[k][j][i] = true;
					}
				}
			}
		}

		System.out.println(bfs());
	}

	static boolean zeroCountFunc() {
		for (int k = 0; k < H; k++) {
			for (int i = 0; i < M; i++) {
				for (int j = 0; j < N; j++) {
					if (map[k][j][i] == 0) {
						return false;
					}
				}
			}
		}
		return true;
	}

	static class pair {
		int x;
		int y;
		int h;

		pair(int x, int y, int h) {
			this.x = x;
			this.y = y;
			this.h = h;
		}
	}

	static int bfs() {
		int cnt = 0;
		while (!q.isEmpty()) {
			cnt++;
			//print();
			int qSize = q.size();
			for (int o = 0; o < qSize; o++) {
				// 상하좌우
				for (int i = 0; i < 4; i++) {
					int nx = q.peek().x + dx[i];
					int ny = q.peek().y + dy[i];
					if (nx < 0 || nx > M - 1 || ny < 0 || ny > N - 1)
						continue;
					if (!visited[q.peek().h][ny][nx] && map[q.peek().h][ny][nx] == 0) {
						visited[q.peek().h][ny][nx] = true;
						map[q.peek().h][ny][nx] = 1;
						q.add(new pair(nx, ny, q.peek().h));
					}
				}
				// 위아래
				for (int i = 0; i < 2; i++) {
					int nh = q.peek().h + dh[i];
					if (nh < 0 || nh > H - 1)
						continue;
					if (!visited[nh][q.peek().y][q.peek().x] && map[nh][q.peek().y][q.peek().x] == 0) {
						visited[nh][q.peek().y][q.peek().x] = true;
						map[nh][q.peek().y][q.peek().x] = 1;
						q.add(new pair(q.peek().x, q.peek().y, nh));
					}
				}
				q.poll();
			}
		}
		if (!zeroCountFunc()) {
			return -1;
		} else {
			return cnt - 1;
		}
	}

	static void print() {
		for (int k = 0; k < H; k++) {
			for (int i = 0; i < M; i++) {
				for (int j = 0; j < N; j++) {
					System.out.print(map[k][j][i] + " ");
				}
				System.out.println();
			}
			System.out.println("==============================");
		}
	}
}

 

'알고리즘' 카테고리의 다른 글

[백준] 10384번 : 팬그램  (0) 2023.03.11
[백준] 16499번 : 동일한 단어 그룹화하기  (0) 2023.03.05
[백준] 1439번 : 뒤집기  (0) 2023.03.04
[백준] 17413번 : 단어 뒤집기 2  (0) 2023.03.03
[백준] 14490번 : 백대열  (1) 2023.02.27

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

 

16499번: 동일한 단어 그룹화하기

첫째 줄에 단어의 개수 N이 주어진다. (2 ≤ N ≤ 100) 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 소문자로만 이루어져 있고, 길이는 10을 넘지 않는다.

www.acmicpc.net

toCharArray 함수는 처음 써보는 거 같다.

Arrays로는 정렬을 안 돌려서

보통은 ArrayList를 선언해서 Collection으로만 정렬시키는데

여러 함수를 자주 써보려고 노력해야겠다.

import java.io.*;
import java.util.*;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int cnt = Integer.parseInt(bf.readLine());
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < cnt; i++) {
            String str = bf.readLine();
            char[] chars = str.toCharArray();
            Arrays.sort((chars));
            str = new String(chars);
            map.put(str, 0);
        }
        System.out.println(map.size());
    }
}

 

'알고리즘' 카테고리의 다른 글

[백준] 10384번 : 팬그램  (0) 2023.03.11
[백준] 7569번 : 토마토  (0) 2023.03.06
[백준] 1439번 : 뒤집기  (0) 2023.03.04
[백준] 17413번 : 단어 뒤집기 2  (0) 2023.03.03
[백준] 14490번 : 백대열  (1) 2023.02.27

+ Recent posts