Compare commits
2 Commits
91d33b30f0
...
fe4f540812
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe4f540812 | ||
|
|
9ea135f2b2 |
56
code_study/Baekjoon/java/_10026.java
Normal file
56
code_study/Baekjoon/java/_10026.java
Normal file
@ -0,0 +1,56 @@
|
||||
import java.util.*;
|
||||
|
||||
public class _10026 {
|
||||
|
||||
public static int countAreaBFS(String[] grid, int N){
|
||||
int[] dx = {1,-1,0,0};
|
||||
int[] dy = {0,0,1,-1};
|
||||
Deque<Integer> qx = new ArrayDeque<>();
|
||||
Deque<Integer> qy = new ArrayDeque<>();
|
||||
boolean[][] visited = new boolean[N][N];
|
||||
int countArea = 0;
|
||||
|
||||
for(int i=0; i<N; i++){
|
||||
for(int j=0; j<N; j++){
|
||||
if(!visited[i][j]){
|
||||
visited[i][j] = true;
|
||||
char color = grid[i].charAt(j);
|
||||
qx.add(j);
|
||||
qy.add(i);
|
||||
while(qx.size() != 0 && qy.size() != 0){
|
||||
int cx = qx.poll();
|
||||
int cy = qy.poll();
|
||||
for(int d=0; d<4; d++){
|
||||
int nx = cx + dx[d];
|
||||
int ny = cy + dy[d];
|
||||
if(0<=nx && nx<N && 0<=ny && ny<N && grid[ny].charAt(nx) == color && !visited[ny][nx]){
|
||||
visited[ny][nx] = true;
|
||||
qx.add(nx);
|
||||
qy.add(ny);
|
||||
}
|
||||
}
|
||||
}
|
||||
countArea += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return countArea;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int N = Integer.parseInt(sc.nextLine());
|
||||
String[] grid_normal = new String[N];
|
||||
String[] grid_blindness = new String[N];
|
||||
for(int i=0; i<N; i++){
|
||||
String line = sc.nextLine();
|
||||
grid_normal[i] = line;
|
||||
grid_blindness[i] = line.replace('R', 'G');
|
||||
}
|
||||
sc.close();
|
||||
|
||||
int areaCount_normal = countAreaBFS(grid_normal, N);
|
||||
int areaCount_blindness = countAreaBFS(grid_blindness, N);
|
||||
|
||||
System.out.println(areaCount_normal + " " + areaCount_blindness);
|
||||
}
|
||||
}
|
||||
156
code_study/data_structure/DS_queue.c
Normal file
156
code_study/data_structure/DS_queue.c
Normal file
@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct C_Queue C_Queue;
|
||||
|
||||
void _enqueue(C_Queue *q, int value);
|
||||
int _dequeue(C_Queue *q);
|
||||
bool _isEmpty(C_Queue *q);
|
||||
bool _isFull(C_Queue *q);
|
||||
void _info(C_Queue *q);
|
||||
void _destroyQ(C_Queue *q);
|
||||
|
||||
struct C_Queue{
|
||||
int *data;
|
||||
int front, rear, size;
|
||||
|
||||
bool (*isEmpty)(C_Queue *q);
|
||||
bool (*isFull)(C_Queue *q);
|
||||
void (*enqueue)(C_Queue *q, int value);
|
||||
int (*dequeue)(C_Queue *q);
|
||||
void (*info)(C_Queue *q);
|
||||
void (*destroyQ)(C_Queue *q);
|
||||
};
|
||||
|
||||
C_Queue creatQueue(int capacity){
|
||||
C_Queue q;
|
||||
q.front = -1;
|
||||
q.rear = -1;
|
||||
q.size = capacity;
|
||||
q.data = (int*)malloc(sizeof(int)*capacity);
|
||||
|
||||
q.enqueue = _enqueue;
|
||||
q.dequeue = _dequeue;
|
||||
q.info = _info;
|
||||
q.isEmpty = _isEmpty;
|
||||
q.isFull = _isFull;
|
||||
q.destroyQ = _destroyQ;
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
int opcode(char *input);
|
||||
|
||||
int main(){
|
||||
printf("생성하려는 원형큐의 용량을 입력해주세요. : ");
|
||||
int capacity;
|
||||
scanf("%d",&capacity);
|
||||
|
||||
C_Queue q = creatQueue(capacity);
|
||||
bool exit_flag = true;
|
||||
while(exit_flag){
|
||||
printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
|
||||
printf("명령어 목록 : enqueue | dequeue | info | exit\n");
|
||||
printf("명령어를 입력해주세요. : ");
|
||||
char input[10];
|
||||
scanf("%s",input);
|
||||
switch (opcode(input))
|
||||
{
|
||||
case 1:
|
||||
if(q.isFull(&q)) {
|
||||
printf("큐가 꽉차있어서 입력할 수 없습니다.\n");
|
||||
}
|
||||
else{
|
||||
printf("넣을 값을 입력해주세요. : ");
|
||||
int num;
|
||||
scanf("%d",&num);
|
||||
q.enqueue(&q, num);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if(q.isEmpty(&q)){
|
||||
printf("큐가 비어있어서 값을 꺼낼 수가 없습니다.\n");
|
||||
}
|
||||
else{
|
||||
printf("추출값 : %d\n",q.dequeue(&q));
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
q.info(&q);
|
||||
break;
|
||||
case 4:
|
||||
exit_flag = false;
|
||||
printf("종료합니다\n");
|
||||
break;
|
||||
default:
|
||||
printf("명령어를 잘못 입력했습니다. 다시 입력해주세요.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
q.destroyQ(&q);
|
||||
}
|
||||
|
||||
int opcode(char *input){
|
||||
if(!strcmp(input, "enqueue")) return 1;
|
||||
if(!strcmp(input, "dequeue")) return 2;
|
||||
if(!strcmp(input, "info")) return 3;
|
||||
if(!strcmp(input, "exit")) return 4;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool _isEmpty(C_Queue *q){
|
||||
return q->front == -1;
|
||||
}
|
||||
|
||||
bool _isFull(C_Queue *q){
|
||||
return ((q->rear)+1)%(q->size) == q->front;
|
||||
}
|
||||
|
||||
void _enqueue(C_Queue *q, int value){
|
||||
if(q->isFull(q)){
|
||||
return;
|
||||
}
|
||||
if(q->isEmpty(q)){
|
||||
q->front = 0;
|
||||
}
|
||||
q->rear = ((q->rear)+1)%(q->size);
|
||||
q->data[q->rear] = value;
|
||||
}
|
||||
|
||||
int _dequeue(C_Queue *q){
|
||||
if(q->isEmpty(q)){
|
||||
return -1;
|
||||
}
|
||||
int value = q->data[q->front];
|
||||
if(q->front == q->rear){
|
||||
q->front = -1;
|
||||
q->rear = -1;
|
||||
}
|
||||
else{
|
||||
q->front = ((q->front)+1)%q->size;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void _info(C_Queue *q){
|
||||
if(q->isEmpty(q)){
|
||||
printf("큐가 비어있습니다.\n");
|
||||
}
|
||||
else{
|
||||
printf("큐 목록: ");
|
||||
int i = q->front;
|
||||
while(true){
|
||||
printf("%d ",q->data[i]);
|
||||
if(i==q->rear) break;
|
||||
i = (i+1)%q->size;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void _destroyQ(C_Queue *q){
|
||||
free(q->data);
|
||||
}
|
||||
120
code_study/data_structure/DS_queue.java
Normal file
120
code_study/data_structure/DS_queue.java
Normal file
@ -0,0 +1,120 @@
|
||||
import java.security.cert.TrustAnchor;
|
||||
import java.util.*;
|
||||
|
||||
class Cir_Queue {
|
||||
private int[] data;
|
||||
private int front, rear, size;
|
||||
|
||||
Cir_Queue(int size){
|
||||
this.data = new int[size];
|
||||
this.front = -1;
|
||||
this.rear = -1;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return this.front == -1;
|
||||
}
|
||||
|
||||
public boolean isFull(){
|
||||
return (this.rear + 1)%(this.size) == front;
|
||||
}
|
||||
|
||||
public void enqueue(int value){
|
||||
if(this.isFull()){
|
||||
System.out.println("Queue is Full!");
|
||||
return;
|
||||
}
|
||||
if(this.isEmpty()){
|
||||
this.front = 0;
|
||||
}
|
||||
this.rear = (this.rear + 1)%(this.size);
|
||||
this.data[rear] = value;
|
||||
}
|
||||
|
||||
public int dequeue(){
|
||||
if(this.isEmpty()){
|
||||
System.out.println("Queue is Empty!");
|
||||
return -1;
|
||||
}
|
||||
int value = this.data[this.front];
|
||||
if(this.rear == this.front){
|
||||
this.rear = -1;
|
||||
this.front = -1;
|
||||
}
|
||||
else {
|
||||
this.front = (this.front + 1)%this.size;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public int peek(){
|
||||
if(this.isEmpty()){
|
||||
System.out.println("Queue is Empty!");
|
||||
return -1;
|
||||
}
|
||||
return this.data[this.front];
|
||||
}
|
||||
|
||||
public void info(){
|
||||
if(this.isEmpty()){
|
||||
System.out.println("큐가 비어있습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder("Queue List : ");
|
||||
int i = this.front;
|
||||
while(true){
|
||||
result.append(this.data[i] + " ");
|
||||
if(i==this.rear) break;
|
||||
i = (i + 1)%this.size;
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
public class DS_queue {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc= new Scanner(System.in);
|
||||
System.out.println("큐의 사이즈를 입력해주세요.");
|
||||
int size = Integer.parseInt(sc.nextLine());
|
||||
Cir_Queue q = new Cir_Queue(size);
|
||||
|
||||
boolean exitFlag = true;
|
||||
while(exitFlag){
|
||||
System.out.println("명령어 목록 : enque [value] | deque | peek | info | exit");
|
||||
String[] opcode = sc.nextLine().split(" ");
|
||||
switch (opcode[0]) {
|
||||
case "enque":
|
||||
q.enqueue(Integer.parseInt(opcode[1]));
|
||||
break;
|
||||
case "deque":
|
||||
if(q.isEmpty()){
|
||||
q.dequeue();
|
||||
break;
|
||||
}
|
||||
System.out.println(q.dequeue());
|
||||
break;
|
||||
case "peek":
|
||||
if(q.isEmpty()){
|
||||
q.peek();
|
||||
break;
|
||||
}
|
||||
System.out.println(q.peek());
|
||||
break;
|
||||
case "info":
|
||||
q.info();
|
||||
break;
|
||||
case "exit":
|
||||
exitFlag = false;
|
||||
System.out.println("Exit!!");
|
||||
break;
|
||||
default:
|
||||
System.out.println("제대로 입력해주세요.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
96
code_study/data_structure/DS_queue.py
Normal file
96
code_study/data_structure/DS_queue.py
Normal file
@ -0,0 +1,96 @@
|
||||
class queue :
|
||||
|
||||
def __init__(self,size) :
|
||||
self.__size = size
|
||||
self.__data = [0]*size
|
||||
self.__front = -1
|
||||
self.__rear = -1
|
||||
|
||||
def isEmpty(self) :
|
||||
return self.__front == -1
|
||||
|
||||
def isFull(self) :
|
||||
return (self.__rear + 1)%(self.__size) == self.__front
|
||||
|
||||
def enqueue(self, value) :
|
||||
if self.isFull() :
|
||||
print("Queue is Full!!")
|
||||
return
|
||||
|
||||
if self.isEmpty() :
|
||||
self.__front = 0
|
||||
|
||||
self.__rear = (self.__rear + 1) % self.__size
|
||||
self.__data[self.__rear] = value
|
||||
print("삽입 완료")
|
||||
|
||||
def dequeue(self) :
|
||||
if self.isEmpty() :
|
||||
print("Queue is Empty!")
|
||||
return -1
|
||||
|
||||
value = self.__data[self.__front]
|
||||
|
||||
if self.__front == self.__rear :
|
||||
self.__front = -1
|
||||
self.__rear = -1
|
||||
else :
|
||||
self.__front = (self.__front + 1) % self.__size
|
||||
|
||||
print(value)
|
||||
|
||||
return value
|
||||
|
||||
def peek(self) :
|
||||
if self.isEmpty() :
|
||||
print("Queue is Empty!")
|
||||
return -1
|
||||
|
||||
value = self.__data[self.__front]
|
||||
|
||||
print(value)
|
||||
|
||||
return value
|
||||
|
||||
def info(self) :
|
||||
if self.isEmpty() :
|
||||
print("Queue is Empty!, Max Size is : {0}".format(self.__size))
|
||||
return
|
||||
|
||||
i = self.__front
|
||||
result = "Queue List : [ "
|
||||
while True:
|
||||
result += str(self.__data[i])
|
||||
if i == self.__rear :
|
||||
result += " ]"
|
||||
break
|
||||
|
||||
result += ", "
|
||||
i = (i + 1) % self.__size
|
||||
|
||||
print("Max Size is : {0}".format(self.__size))
|
||||
print(result)
|
||||
|
||||
q = queue(int(input("원형큐의 최대사이즈를 입력해주세요 : ")))
|
||||
|
||||
endFlag = True
|
||||
|
||||
while endFlag :
|
||||
opcode = input("명령어 목록 : enqueue [num] | dequeue | peek | info | exit\n")
|
||||
|
||||
if "enqueue" in opcode :
|
||||
if opcode.split()[0] != "enqueue" :
|
||||
print("제대로 입력해주세요 enqueue와 정수를 입력받습니다.")
|
||||
continue
|
||||
q.enqueue(int(opcode.split()[1]))
|
||||
elif opcode == "dequeue" :
|
||||
q.dequeue()
|
||||
elif opcode == "peek" :
|
||||
q.peek()
|
||||
elif opcode == "info" :
|
||||
q.info()
|
||||
elif opcode == "exit" :
|
||||
endFlag = False
|
||||
print("Exit")
|
||||
else :
|
||||
print("제대로 입력해주세요")
|
||||
117
code_study/data_structure/DS_queue.swift
Normal file
117
code_study/data_structure/DS_queue.swift
Normal file
@ -0,0 +1,117 @@
|
||||
class CircularQueue {
|
||||
private var data: [Int]
|
||||
private var front, rear, size : Int
|
||||
|
||||
init(size: Int){
|
||||
self.front = -1
|
||||
self.rear = -1
|
||||
self.size = size
|
||||
self.data = Array(repeating: 0, count: size)
|
||||
}
|
||||
|
||||
func isEmpty() -> Bool {
|
||||
return self.front == -1
|
||||
}
|
||||
|
||||
func isFull() -> Bool {
|
||||
return (self.rear + 1)%self.size == self.front
|
||||
}
|
||||
|
||||
func info(){
|
||||
if self.isEmpty(){
|
||||
print("Empty!")
|
||||
return
|
||||
}
|
||||
var i: Int = self.front
|
||||
while true {
|
||||
print("\(self.data[i]) ",terminator: " ")
|
||||
if i == self.rear {
|
||||
print()
|
||||
break
|
||||
}
|
||||
i = (i+1)%self.size
|
||||
}
|
||||
}
|
||||
|
||||
func enqueue(to value: Int){
|
||||
if self.isFull() {
|
||||
print("Full!!!")
|
||||
return
|
||||
}
|
||||
|
||||
if self.isEmpty(){
|
||||
self.front = 0
|
||||
}
|
||||
self.rear = (self.rear + 1)%self.size
|
||||
self.data[self.rear] = value
|
||||
}
|
||||
|
||||
func dequeue() -> Int? {
|
||||
if self.isEmpty(){
|
||||
return nil
|
||||
}
|
||||
let value: Int = self.data[self.front]
|
||||
if(self.front == self.rear){
|
||||
self.front = -1
|
||||
self.rear = -1
|
||||
}
|
||||
else {
|
||||
self.front = (self.front+1) % self.size
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func peek() -> Int? {
|
||||
if self.isEmpty(){
|
||||
return nil
|
||||
}
|
||||
return self.data[self.front]
|
||||
}
|
||||
}
|
||||
|
||||
let cirQueue: CircularQueue = CircularQueue(size: 10)
|
||||
var exitFlag: Bool = true
|
||||
|
||||
while exitFlag {
|
||||
print("명령어를 입력하세요 (enqueue [값], dequeue, peek, info, exit):")
|
||||
if let input = readLine() {
|
||||
let components = input.split(separator: " ")
|
||||
let order = String(components.first ?? "")
|
||||
|
||||
switch order {
|
||||
case "enqueue" :
|
||||
if components.count == 2, let value = Int(components[1]) {
|
||||
cirQueue.enqueue(to: value)
|
||||
print("\(value) enqueue!")
|
||||
}
|
||||
else {
|
||||
print("enqueue 명령어 뒤에 공백에 이어서 숫자를 입력해야합니다.")
|
||||
}
|
||||
case "dequeue" :
|
||||
if let value = cirQueue.dequeue(){
|
||||
print("\(value) dequeue!!")
|
||||
}
|
||||
else {
|
||||
print("Empty!!")
|
||||
}
|
||||
case "peek" :
|
||||
if let value = cirQueue.peek(){
|
||||
print("\(value) peek!!")
|
||||
}
|
||||
else {
|
||||
print("Empty!!")
|
||||
}
|
||||
case "info" :
|
||||
cirQueue.info()
|
||||
case "exit" :
|
||||
exitFlag = false
|
||||
print("Exit!!!")
|
||||
default :
|
||||
print("제대로 된 명령어를 입력하세요")
|
||||
}
|
||||
}
|
||||
else {
|
||||
print("제대로 입력해주세요")
|
||||
}
|
||||
}
|
||||
109
code_study/data_structure/DS_queue.ts
Normal file
109
code_study/data_structure/DS_queue.ts
Normal file
@ -0,0 +1,109 @@
|
||||
export {};
|
||||
|
||||
class CircularQueue {
|
||||
private data: number[];
|
||||
private front: number;
|
||||
private rear: number
|
||||
private size: number;
|
||||
|
||||
constructor(size: number){
|
||||
this.size = size;
|
||||
this.rear = -1;
|
||||
this.front = -1;
|
||||
this.data = Array.from({length: size}, () => 0);
|
||||
}
|
||||
|
||||
public isEmpty(): boolean{
|
||||
return this.front === -1;
|
||||
}
|
||||
|
||||
public isFull(): boolean{
|
||||
return (this.rear + 1)%(this.size) === this.front;
|
||||
}
|
||||
|
||||
public enqueue(value: number){
|
||||
if(this.isFull()){
|
||||
console.log("Queue is full!");
|
||||
return;
|
||||
}
|
||||
if(this.isEmpty()){
|
||||
this.front = 0;
|
||||
}
|
||||
|
||||
this.rear = (this.rear + 1)%(this.size);
|
||||
this.data[this.rear] = value;
|
||||
}
|
||||
|
||||
public dequeue(): number | undefined {
|
||||
if(this.isEmpty()) return undefined;
|
||||
const value: number = this.data[this.front];
|
||||
if(this.rear === this.front) {
|
||||
this.rear = -1;
|
||||
this.front = -1;
|
||||
}
|
||||
else {
|
||||
this.front = (this.front + 1)%(this.size);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public peek(): number | undefined {
|
||||
return (this.isEmpty()) ? undefined : this.data[this.front];
|
||||
}
|
||||
|
||||
public info() {
|
||||
if(this.isEmpty()){
|
||||
console.log("Empty!");
|
||||
return;
|
||||
}
|
||||
let result = "list : [";
|
||||
let i = this.front;
|
||||
while(true){
|
||||
result += this.data[i].toString();
|
||||
if(i === this.rear){
|
||||
result += "]";
|
||||
break;
|
||||
}
|
||||
i = (i+1)%(this.size);
|
||||
result += ",";
|
||||
}
|
||||
console.log(result);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("원형큐를 생성합니다. 사이즈를 입력해주세요.")
|
||||
const size: number = Number(require("fs").readFileSync(0,"utf8").toString().trim());
|
||||
let cq = new CircularQueue(size);
|
||||
let exitFlag: boolean = true;
|
||||
|
||||
while(exitFlag){
|
||||
console.log("++++++++++++++++명령어모음++++++++++++++++");
|
||||
console.log("enqueue <value> | dequeue | peek | info | exit");
|
||||
console.log("========================================");
|
||||
console.log("명령어를 입력해주세요!");
|
||||
const input:string[] = require("fs").readFileSync(0,"utf8").toString().trim().split(" ");
|
||||
switch(input[0])
|
||||
{
|
||||
case "enqueue":
|
||||
cq.enqueue(Number(input[1]));
|
||||
break;
|
||||
case "dequeue":
|
||||
const deqvalue = cq.dequeue();
|
||||
console.log(deqvalue===undefined ? "큐가 비었습니다." : deqvalue);
|
||||
break;
|
||||
case "peek":
|
||||
const peekValue = cq.peek();
|
||||
console.log(peekValue===undefined ? "큐가 비었습니다." : peekValue);
|
||||
break;
|
||||
case "info":
|
||||
cq.info();
|
||||
break;
|
||||
case "exit":
|
||||
exitFlag = false;
|
||||
console.log("종료합니다.")
|
||||
break;
|
||||
default:
|
||||
console.log("제대로 입력해주세요.")
|
||||
break;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user