diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..cfac4ef Binary files /dev/null and b/.DS_Store differ diff --git a/week1/.DS_Store b/week1/.DS_Store new file mode 100644 index 0000000..6f32edc Binary files /dev/null and b/week1/.DS_Store differ diff --git a/week1/youngjun/1000.c b/week1/youngjun/1000.c new file mode 100644 index 0000000..6070bd4 --- /dev/null +++ b/week1/youngjun/1000.c @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + int A,B; + scanf("%d %d",&A,&B); + printf("%d",A+B); +} \ No newline at end of file diff --git a/week1/youngjun/1001.c b/week1/youngjun/1001.c new file mode 100644 index 0000000..4bb8aa1 --- /dev/null +++ b/week1/youngjun/1001.c @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + int A,B; + scanf("%d %d",&A,&B); + printf("%d",A-B); +} \ No newline at end of file diff --git a/week1/youngjun/1008.c b/week1/youngjun/1008.c new file mode 100644 index 0000000..7a0c4a2 --- /dev/null +++ b/week1/youngjun/1008.c @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + int A,B; + scanf("%d %d",&A,&B); + printf("%.9lf",(double)A/(double)B); +} \ No newline at end of file diff --git a/week1/youngjun/10172.c b/week1/youngjun/10172.c new file mode 100644 index 0000000..3f8f56f --- /dev/null +++ b/week1/youngjun/10172.c @@ -0,0 +1,10 @@ +#include +#include + +int main(){ + printf("|\\_/|\n"); + printf("|q p| /}\n"); + printf("( 0 )\"\"\"\\ \n"); + printf("|\"^\"` |\n"); + printf("||_/=\\\\__|\n"); +} \ No newline at end of file diff --git a/week1/youngjun/10430.c b/week1/youngjun/10430.c new file mode 100644 index 0000000..f45de80 --- /dev/null +++ b/week1/youngjun/10430.c @@ -0,0 +1,13 @@ +#include +#include + +int main(){ + int A,B,C; + scanf("%d %d %d",&A,&B,&C); + + printf("%d\n",(A+B)%C); + printf("%d\n",((A%C) + (B%C))%C); + printf("%d\n",(A*B)%C); + printf("%d\n",((A%C) * (B%C))%C); + +} \ No newline at end of file diff --git a/week1/youngjun/10869.c b/week1/youngjun/10869.c new file mode 100644 index 0000000..1b6e9a6 --- /dev/null +++ b/week1/youngjun/10869.c @@ -0,0 +1,12 @@ +#include +#include + +int main(){ + int A,B; + scanf("%d %d",&A,&B); + printf("%d\n",A+B); + printf("%d\n",A-B); + printf("%d\n",A*B); + printf("%d\n",A/B); + printf("%d\n",A%B); +} \ No newline at end of file diff --git a/week1/youngjun/10926.c b/week1/youngjun/10926.c new file mode 100644 index 0000000..ac6392a --- /dev/null +++ b/week1/youngjun/10926.c @@ -0,0 +1,9 @@ +#include +#include + +int main(){ + char A[50]; + scanf("%s",A); + printf("%s?",A); + printf("?!"); +} \ No newline at end of file diff --git a/week1/youngjun/10998.c b/week1/youngjun/10998.c new file mode 100644 index 0000000..1ee44ac --- /dev/null +++ b/week1/youngjun/10998.c @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + int A,B; + scanf("%d %d",&A,&B); + printf("%d",A*B); +} \ No newline at end of file diff --git a/week1/youngjun/18108.c b/week1/youngjun/18108.c new file mode 100644 index 0000000..1efca54 --- /dev/null +++ b/week1/youngjun/18108.c @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + int years; + scanf("%d",&years); + printf("%d",years - 543); +} \ No newline at end of file diff --git a/week1/youngjun/25083.c b/week1/youngjun/25083.c new file mode 100644 index 0000000..f33d213 --- /dev/null +++ b/week1/youngjun/25083.c @@ -0,0 +1,11 @@ +#include +#include + +int main(){ + printf(" ,r'\"7 \n"); + printf("r`-_ ,' ,/ \n"); + printf(" \\. \". L_r' \n"); + printf(" `~\\/ \n"); + printf(" | \n"); + printf(" | \n"); +} \ No newline at end of file diff --git a/week1/youngjun/2557.c b/week1/youngjun/2557.c new file mode 100644 index 0000000..1ad36b8 --- /dev/null +++ b/week1/youngjun/2557.c @@ -0,0 +1,5 @@ +#include + +int main(){ + printf("Hello World!"); +} \ No newline at end of file diff --git a/week1/youngjun/2558.c b/week1/youngjun/2558.c new file mode 100644 index 0000000..af2038f --- /dev/null +++ b/week1/youngjun/2558.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int main(){ + int A; + char B[4]; // 널문자 조심 + scanf("%d %s",&A,B); + + printf("%d\n",A * (B[2] - '0')); + printf("%d\n",A * (B[1] - '0')); + printf("%d\n",A * (B[0] - '0')); + printf("%d\n",A * atoi(B)); + +} \ No newline at end of file diff --git a/week1/youngjun/3003.c b/week1/youngjun/3003.c new file mode 100644 index 0000000..1b45bd3 --- /dev/null +++ b/week1/youngjun/3003.c @@ -0,0 +1,14 @@ +#include +#include + +int main(){ + int tot[6] = {1,1,2,2,2,8}; + int sev[6]; + for (int i = 0; i < 6; i++) + { + scanf("%d",&sev[i]); + printf("%d",tot[i] - sev[i]); + printf(" "); + } + +} \ No newline at end of file diff --git a/week2/.DS_Store b/week2/.DS_Store new file mode 100644 index 0000000..8fa7e25 Binary files /dev/null and b/week2/.DS_Store differ diff --git a/week3/.DS_Store b/week3/.DS_Store new file mode 100644 index 0000000..6f32edc Binary files /dev/null and b/week3/.DS_Store differ diff --git a/week3/youngjun/11729.c b/week3/youngjun/11729.c new file mode 100644 index 0000000..4f34e4a --- /dev/null +++ b/week3/youngjun/11729.c @@ -0,0 +1,33 @@ +//하노이 탑 + +#include +#include + +int Num_Hanoi(int n); +void Hanoi(int n,int start,int to,int finish); + +int main(){ + int n; + scanf("%d",&n); + printf("%d\n",Num_Hanoi(n)); + Hanoi(n,1,2,3); +} + +void Hanoi(int n,int start,int to,int finish){ + if (n==1) + { + printf("%d %d\n",start,finish); + return; + } + Hanoi(n-1,start,finish,to); + printf("%d %d\n",start,finish); + Hanoi(n-1,to,start,finish); + +} + +int Num_Hanoi(int n){ + if(n==1){ + return 1; + } + return 2 * Num_Hanoi(n-1) + 1; +} \ No newline at end of file diff --git a/week3/youngjun/1431.c b/week3/youngjun/1431.c new file mode 100644 index 0000000..faff566 --- /dev/null +++ b/week3/youngjun/1431.c @@ -0,0 +1,103 @@ +//시리얼 번호 + +#include +#include +#include +#define _CRT_SECURE_NO_WARNINGS + +int N; + +void sort(char a[],char b[]){ + char temp[50]; + int num_a,num_b = 0; + char t[50],p[50]; + + if (strlen(a) > strlen(b)) + { + strcpy(temp,a); + strcpy(a,b); + strcpy(b,temp); + } + + if (strlen(a) == strlen(b)) + { + for (int i = 0; i < strlen(a); i++) + { + if (a[i] > '0' && a[i] <= '9') + { + strcpy(&t[i],&a[i]); + num_a += atoi(&t[i]); + } + if (b[i] > '0' && b[i] <= '9') + { + strcpy(&p[i],&b[i]); + num_b += atoi(&p[i]); + } + } + if (num_a > num_b) + { + strcpy(temp,a); + strcpy(a,b); + strcpy(b,temp); + } + else if (num_a == num_b) + { + for (int j = 0; j < strlen(a); j++) + { + if (a[j] > b[j]) + { + strcpy(temp,a); + strcpy(a,b); + strcpy(b,temp); + + break; + } + if (a[j] < b[j]) + { + break; + } + + + } + + } + } + else return; + +} + +int main(){ + scanf("%d\n",&N); + //2차배열 arr 동적할당 + char** arr; + arr = (char**)malloc(sizeof(char*)*N); + for(int i = 0; i < N; i++) + { + arr[i] = (char*) malloc (sizeof(char)*50); + scanf(" %s",arr[i]); + } + // + + for (int n1 = 0; n1 < N-1; n1++) + { + for (int n2 = n1+1; n2 < N; n2++) + { + sort(arr[n1],arr[n2]); + } + + } + + for (int j = 0; j < N; j++) + { + printf("\n%s",arr[j]); + } + + //동적할당 해제 + for (int l = 0; l < N; l++) + { + free(arr[l]); + } + free(arr); + // + return 0; +} \ No newline at end of file diff --git a/week3/youngjun/17478.c b/week3/youngjun/17478.c new file mode 100644 index 0000000..0ea50ef --- /dev/null +++ b/week3/youngjun/17478.c @@ -0,0 +1,59 @@ +//재귀함수가 뭔가요? + +#include + +int N; //지역변수 선언 + +void P(n){ + for (int i = 0; i < n*4; i++) + { + printf("_"); + } +} + +void K(n){ + if (n == 0) + { + return; + } + + for (int i = 0; i < (n-1)*4; i++) + { + printf("_"); + } + printf("라고 답변하였지.\n"); + K(n-1); +} + +void F(n){ + if(n == N){ + P(n); + printf("\"재귀함수가 뭔가요?\"\n"); + P(n); + printf("\"재귀함수는 자기 자신을 호출하는 함수라네\"\n"); + P(n); + printf("라고 답변하였지.\n"); + K(n); + + return; + } + + P(n); + printf("\"재귀함수가 뭔가요?\"\n"); + P(n); + printf("\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.\n"); + P(n); + printf("마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.\n"); + P(n); + printf("그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어.\"\n"); + + F(n+1); +} + +int main(){ + scanf("%d",&N); + printf("어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다.\n"); + F(0); + + return 0; +} \ No newline at end of file diff --git a/week3/youngjun/18870.c b/week3/youngjun/18870.c new file mode 100644 index 0000000..c7dbd89 --- /dev/null +++ b/week3/youngjun/18870.c @@ -0,0 +1 @@ +//좌표 압축 \ No newline at end of file diff --git a/week3/youngjun/2447.c b/week3/youngjun/2447.c new file mode 100644 index 0000000..b044d47 --- /dev/null +++ b/week3/youngjun/2447.c @@ -0,0 +1,58 @@ +//별 찍기 + +#include +#include + +char table[6562][6562]; +void origin_star(int y,int x); +void star(int n,int y,int x); + + +int main(){ + int n; + scanf("%d",&n); + memset(table,' ',sizeof(table)); + star(n,0,0); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + printf("%c",table[i][j]); + } + printf("\n"); + + } + return 0; +} + +void origin_star(int y,int x){ + table[y][x] = '*'; + table[y][x+1] = '*'; + table[y][x+2] = '*'; + + table[y+1][x] = '*'; + table[y+1][x+2] = '*'; + + table[y+2][x] = '*'; + table[y+2][x+1] = '*'; + table[y+2][x+2] = '*'; +} + +void star(int n,int y,int x){ + + if (n==3) + { + origin_star(y,x); + return; + } + star(n/3,y,x); + star(n/3,y,x+n/3); + star(n/3,y,x+n/3*2); + + star(n/3,y+n/3,x); + star(n/3,y+n/3,x+n/3*2); + + star(n/3,y+n/3*2,x); + star(n/3,y+n/3*2,x+n/3); + star(n/3,y+n/3*2,x+n/3*2); +} \ No newline at end of file diff --git a/week4/.DS_Store b/week4/.DS_Store new file mode 100644 index 0000000..6f32edc Binary files /dev/null and b/week4/.DS_Store differ diff --git a/week4/youngjun/1003 b/week4/youngjun/1003 new file mode 100755 index 0000000..d75ae53 Binary files /dev/null and b/week4/youngjun/1003 differ diff --git a/week4/youngjun/1003.c b/week4/youngjun/1003.c new file mode 100644 index 0000000..a5d5aaf --- /dev/null +++ b/week4/youngjun/1003.c @@ -0,0 +1,80 @@ +//피보나치 수열 + +#include + +int array[41]; + +int fibonacci(int n){ + + if (n == 0) { + return 0; + } + else if (n == 1) { + return 1; + } + for (int i = 2; i <= n; i++) + { + array[i] = array[i-1] + array[i-2]; + } + return array[n]; +} + + +int main(){ + int T; + scanf("%d",&T); + int N_array[T]; + for (int i = 0; i < T; i++) + { + scanf("\n%d",&N_array[i]); + } + array[0] = 0; + array[1] = 1; + + //N_array = (int*)malloc(sizeof(int)*T); + + for (int j = 0; j < T; j++) + { + if (N_array[j] == 0) + { + printf("%d %d\n",1,0); + }else if (N_array[j] == 1){ + printf("%d %d\n",0,1); + }else printf("%d %d\n",fibonacci(N_array[j]-1),fibonacci(N_array[j])); + } + return 0; +} + /*#include + +int num0,num1 = 0; + +int fibonacci(int n) { + if (n == 0) { + num0++; + return 0; + } else if (n == 1) { + num1++; + return 1; + } else return fibonacci(n-1) + fibonacci(n-2); +} + +int main(){ + int T; + scanf("%d",&T); + int N_array[T]; + for (int i = 0; i < T; i++) + { + scanf("\n%d",&N_array[i]); + } + + //N_array = (int*)malloc(sizeof(int)*T); + + for (int j = 0; j < T; j++) + { + num0 = 0; + num1 = 0; + fibonacci(N_array[j]); + printf("%d %d\n",num0,num1); + } + return 0; +}*/ diff --git a/week4/youngjun/12865.c b/week4/youngjun/12865.c new file mode 100644 index 0000000..25ebfa8 --- /dev/null +++ b/week4/youngjun/12865.c @@ -0,0 +1,48 @@ +//평범한 배낭 + +#include +#include +#define Max(a,b) (((a) > (b)) ? (a) : (b)) + +int N,K; + +int take(int things[][]){ + int dp[N+1][K+1]; + for(int i = 0; i < N; i++){ + dp[i][0] = 0; + } + for(int j = 0; j < K; j++){ + dp[0][j] = 0; + } + + for (int m = 0; m <= N; m++) + { + for (int l = 0; l <= K; l++) + { + if (things[m][0] > m) + { + dp[m][l] = dp[m-1][l]; + } + else{ + dp[m][l] = Max(dp[m-1][l], dp[m][l-things[m-1][0]] + things[m-1][1]); + } + } + + } + + return dp[N][K]; + +} +int main(){ + scanf("%d %d",&N,&K); + + int things[N][2]; + + for (int i = 0; i < N; i++) + { + scanf("\n%d %d",&things[i][0],&things[i][1]); + } + + printf("%d",take); + +} \ No newline at end of file diff --git a/week4/youngjun/1932 b/week4/youngjun/1932 new file mode 100755 index 0000000..e18e3cf Binary files /dev/null and b/week4/youngjun/1932 differ diff --git a/week4/youngjun/1932.c b/week4/youngjun/1932.c new file mode 100644 index 0000000..14f31be --- /dev/null +++ b/week4/youngjun/1932.c @@ -0,0 +1,87 @@ +//정수 삼각형 + +#include +#include +#define Max(a,b) (((a) > (b)) ? (a) : (b)) + +int N; + +int dp(int** wh){ + int sum[N][N]; + sum[0][0] = wh[0][0]; + + for (int i = 1; i < N; i++) + { + for (int j = 0; j <= i; j++) + { + if (j>0) + { + sum[i][j] = sum[i-1][j-1] + wh[i][j]; + } + if (j==0) + { + sum[i][j] = sum[i-1][j] + wh[i][j]; + } + + sum[i][j] = Max(sum[i][j],sum[i-1][j] + wh[i][j]); + } + } + int r = sum[N-1][0]; + for (int k = 1; k < N; k++) + { + r = Max(r,sum[N-1][k]); + } + + return r; +} + +int main(){ + scanf("%d",&N); + + //동적할당 + int** wh; + wh = (int**)malloc(sizeof(int*)*N); + for (int i = 0; i < N; i++) + { + wh[i] = (int*)malloc(sizeof(int)*N); + } + // + + for (int i = 0; i < N; i++) + { + getchar(); + for (int j = 0; j <= i; j++) + { + scanf("%d ",&wh[i][j]); + } + } + + printf("%d",dp(wh)); + + //동적할당 해제 + for (int k = 0; k < N; k++) + { + free(wh[k]); + } + free(wh); + // + + return 0; +} + +/* +int** wh; +wh = (int**)malloc(sizeof(int*)*(N+1)); +for (int i = 0; i < N; i++) +{ + wh[i] = (int*)malloc(sizeof(int)*(N+1)*2); +} +*/ + +/* +for (int k = 0; k <= N; k++) + { + free(wh[k]); + } + free(wh); +*/ \ No newline at end of file diff --git a/week4/youngjun/2156 b/week4/youngjun/2156 new file mode 100755 index 0000000..4fd602a Binary files /dev/null and b/week4/youngjun/2156 differ diff --git a/week4/youngjun/2156.c b/week4/youngjun/2156.c new file mode 100644 index 0000000..21f1452 --- /dev/null +++ b/week4/youngjun/2156.c @@ -0,0 +1,41 @@ +//포도주 시식 + +#include +#define Max(a,b) (((a) > (b)) ? (a) : (b)) + +int gr(int N, int arr[]){ + int dp[N+1]; + + dp[1] = arr[0]; + dp[2] = dp[1] + arr[1]; + + if (N<=2) + { + return dp[N]; + } + else { + for (int i = 0; i <= N; i++) + { + dp[i] = dp[i-3] + arr[i-2] + arr[i-1]; + dp[i] = Max(dp[i],dp[i-2]+arr[i-1]); + dp[i] = Max(dp[i],dp[i-1]); + } + } + + return dp[N]; +} + +int main(){ + int N; + scanf("%d",&N); + + int dt[N]; + for (int i = 0; i < N; i++) + { + scanf("\n%d",&dt[i]); + } + + printf("%d",gr(N,dt)); + + return 0; +} \ No newline at end of file diff --git a/week4/youngjun/2193 b/week4/youngjun/2193 new file mode 100755 index 0000000..c42f950 Binary files /dev/null and b/week4/youngjun/2193 differ diff --git a/week4/youngjun/2193.c b/week4/youngjun/2193.c new file mode 100644 index 0000000..81400d1 --- /dev/null +++ b/week4/youngjun/2193.c @@ -0,0 +1,25 @@ +//이친수 + +#include + +int main(){ + int N; + + scanf("%d",&N); + + long int arr[N+1]; + arr[1] = 1; + arr[2] = 1; + + for (int i = 3; i <= N; i++) + { + arr[i] = arr[i-1] + arr[i-2]; + } + + printf("%ld",arr[N]); + + return 0; +} + +//처음 두자리는 10으로 고정 +//10을 제외한 f(n) =>> 3번째 자리가 0일 때, 경우의 수가 f(n-1)/ 3번째 자리가 1일 때, 경우의 수가 f(n-2)이다. 즉, f(n) = f(n-1) + f(n-2), 피보나치 수열과 똑같다. \ No newline at end of file diff --git a/week5/.DS_Store b/week5/.DS_Store new file mode 100644 index 0000000..6f32edc Binary files /dev/null and b/week5/.DS_Store differ diff --git "a/week5/youngjun/5\354\243\274\354\260\250 \353\260\234\355\221\234 \353\213\244\354\235\264\353\202\230\353\257\271 \355\224\204\353\241\234\352\267\270\353\236\230\353\260\215.md" "b/week5/youngjun/5\354\243\274\354\260\250 \353\260\234\355\221\234 \353\213\244\354\235\264\353\202\230\353\257\271 \355\224\204\353\241\234\352\267\270\353\236\230\353\260\215.md" new file mode 100644 index 0000000..5409eb7 --- /dev/null +++ "b/week5/youngjun/5\354\243\274\354\260\250 \353\260\234\355\221\234 \353\213\244\354\235\264\353\202\230\353\257\271 \355\224\204\353\241\234\352\267\270\353\236\230\353\260\215.md" @@ -0,0 +1,66 @@ +# 5주차 발표: 다이나믹 프로그래밍 + +- 하나의 큰 문제를 여러 개의 작은 문제로 나누어서 그 결과를 저장하여 다시 큰 문제를 해결할 때 사용 +- 특정한 알고리즘이 아닌 하나의 문제해결법 → 다양한 문제에서 활용가능! + +## 1. 사용하는 이유?? + +- 재귀와 방식 유사 +- 차이점은 재귀의 특징인 불필요한 반복호출을 DP의 저장기능으로 없엠 → 시간복잡도 대폭 줄일 수 있음 +- 따라서 실질적으로 거의 사용되지 않는 재귀에 비해 DP는 많은 프로그래머가 사용한다. + +![스크린샷 2023-02-03 오후 4.42.27.png](5%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20%E1%84%83%E1%85%A1%E1%84%8B%E1%85%B5%E1%84%82%E1%85%A1%E1%84%86%E1%85%B5%E1%86%A8%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC%20b8eb7c6f4afb46769b5f7364d28bfc9f/%25EC%258A%25A4%25ED%2581%25AC%25EB%25A6%25B0%25EC%2583%25B7_2023-02-03_%25EC%2598%25A4%25ED%259B%2584_4.42.27.png) + +## 2. DP 사용 조건 + +- 1. 동일한 부분 문제들이 반복하여 나타나는 경우 +- 2. 부분 문제들의 답이 전체 문제의 답이 되는 경우 + +ex.) 피보나치 수열 + +- f(0)이라는 문제가 3번, f(1)은 5번, f(2)는 3번, f(3)은 2번 반복해서 나타남. +- 전체 문제 f(5)의 답은 f(4)라는 부분문제의 답과 f(3)이라는 부분문제의 답의 합이다. + +→ 피보나치 수열은 DP 사용 가능! + +## 3. DP 사용 방법 + +- 하나의 배열 만들기 → 정답으로 가는 흔적을 보여주는 배열 +- Top-down(재귀함수), Bottom-up(DP) + +ex.) 피보나치 수열 + +![스크린샷 2023-02-03 오후 5.39.47.png](5%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20%E1%84%83%E1%85%A1%E1%84%8B%E1%85%B5%E1%84%82%E1%85%A1%E1%84%86%E1%85%B5%E1%86%A8%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC%20b8eb7c6f4afb46769b5f7364d28bfc9f/%25EC%258A%25A4%25ED%2581%25AC%25EB%25A6%25B0%25EC%2583%25B7_2023-02-03_%25EC%2598%25A4%25ED%259B%2584_5.39.47.png) + +- Top-down + +> fib(0) = 1, fib(1) = 1; +> + +> fib(4) = fib(3) + fib(2); +> + +> fib(3) = fib(2) + fib(1); +> + +> fib(2) = fib(1) + fib(0); +> + +- Bottom-up + +> int sub[]; +> + +> sub[0] = 1, sub[1] = 1; +> + +> sub[2] = sub[0] + sub[1]; +> + +> sub[3] = sub[1] + sub[2]; +> + +> sub[4] = sub[2] + sub[3]; == fib(4) +> + +**T-d는 직관적이지만 스택에 한계가 있으므로 B-u으로 코딩하는 연습 필요!** \ No newline at end of file diff --git a/week6/.DS_Store b/week6/.DS_Store new file mode 100644 index 0000000..6f32edc Binary files /dev/null and b/week6/.DS_Store differ diff --git a/week6/youngjun/.DS_Store b/week6/youngjun/.DS_Store new file mode 100644 index 0000000..80b2b3f Binary files /dev/null and b/week6/youngjun/.DS_Store differ diff --git a/week6/youngjun/10026 b/week6/youngjun/10026 new file mode 100755 index 0000000..3f7f0e3 Binary files /dev/null and b/week6/youngjun/10026 differ diff --git a/week6/youngjun/10026.c b/week6/youngjun/10026.c new file mode 100644 index 0000000..bffdf2b --- /dev/null +++ b/week6/youngjun/10026.c @@ -0,0 +1,92 @@ +//적록 색약 + +#include +#include +int N; + +void dfs(char area[][N],int x,int y,int visited[][N]){ + visited[x][y] = 1; + + if (x>0) + { + if(area[x-1][y] == area[x][y] && visited[x-1][y] == 0){ + dfs(area,x-1,y,visited); + } + } + if (y>0) + { + if(area[x][y-1] == area[x][y] && visited[x][y-1] == 0){ + dfs(area,x,y-1,visited); + } + } + if (x +#include +#include + +int T; +int M,N,K; +int X,Y; + +void dfs(int field[][N],int x,int y,int visited[][N]){ //DFS 탐색 + visited[x][y] = 1; + if(x>0) + { + if (field[x-1][y] == 1 && visited[x-1][y] == 0) + { + dfs(field,x-1,y,visited); + } + } + if (x0) + { + if (field[x][y-1] == 1 && visited[x][y-1] == 0) + { + dfs(field,x,y+1,visited); + } + } + if (y +#include +#include "QUEUE.h" //c언어는 따로 지원해주는 헤더파일이 없기 때문에 다른 사람의 코드를 헤더파일로 저장했음 +int node; +int line; + +void corr(int graph[][line],int a,int b,int i){ //graph배열을 직관적으로 보이게 정리해주는 재귀함수 + if (!graph[a][i]) + { + graph[a][i] = b; + } + else corr(graph,a,b,i+1); + if (i>0) + { + for (int k = 0; k < i; k++){ + for (int j = k+1; j <= i; j++) + { + if (graph[a][k] > graph[a][j]) + { + int temp = graph[a][k]; + graph[a][k] = graph[a][j]; + graph[a][j] = temp; + } + + } + + } + } + +} + +void dfs(int graph[][line],int v,int visited[]){ //DFS 탐색 + visited[v] = 1; + printf("%d ",v); + for (int i = 0; i < line; i++) + { + if (!visited[graph[v][i]] && !(graph[v][i] == 0)) + { + v = graph[v][i]; + dfs(graph,v,visited); + } + + } + return; + +} + +void bfs(int graph[][line],int v,int visited[]){ + + struct Queue* queue = createQueue(1); + enqueue(queue,v); + printf("%d ",v); + visited[v] = 1; + while (queue -> size) + { + v = dequeue(queue); //먼저 들어온 노드를 꺼낼 수 있음 + for (int i = 0; i < sizeof(graph[v]) / sizeof(int); i++) + { + if (!visited[graph[v][i]] && !(graph[v][i] == 0)) + { + enqueue(queue,graph[v][i]); + printf("%d ",graph[v][i]); + visited[graph[v][i]] = 1; + } + + } + + } + + +} + +int main(){ + int start; + int a,b; + + scanf("%d %d %d",&node,&line,&start); + + int graph[node+1][line]; + for (int i = 0; i < node+1; i++) + { + for (int j = 0; j < line; j++) + { + graph[i][j] = 0; + } + + } + + + for (int i = 0; i < line; i++) + { + getchar(); + scanf("%d %d",&a,&b); + corr(graph,a,b,0); + corr(graph,b,a,0); + } + + int visited[1001] = {0, }; + + printf("\n"); + dfs(graph,start,visited); + printf("\n"); + for (int i = 0; i < 1001; i++) //다시 초기화 => visited배열은 포인터 인자로 넘겨지기 때문에 메모리값이 바뀐다. + { + visited[i] = 0; + } + + bfs(graph,start,visited); + + return 0; +} \ No newline at end of file diff --git a/week6/youngjun/2178 b/week6/youngjun/2178 new file mode 100755 index 0000000..0ec6c89 Binary files /dev/null and b/week6/youngjun/2178 differ diff --git a/week6/youngjun/2178.c b/week6/youngjun/2178.c new file mode 100644 index 0000000..e2e3e4d --- /dev/null +++ b/week6/youngjun/2178.c @@ -0,0 +1,83 @@ +//미로 탐색 + +#include +#include +#include +#define Min(a,b) (((a) < (b)) ? (a) : (b)) + +int N,M; +/* +int bfs(int** maze,int x,int y){ + struct QUEUE* queue = createQueue(N*M); + enqueue(queue,) +}*/ + +int dfs(int maze[][M],int x,int y,int visited[][M],int* num_s){ + if (x == N-1 && y == M-1) + { + *num_s = Min(maze[x][y],*num_s); + } + + visited[x][y] = 1; + if(x>0) + { + if (maze[x-1][y] == 1 && visited[x-1][y] == 0) + { + maze[x-1][y] = maze[x][y] + 1; + dfs(maze,x-1,y,visited,num_s); + } + } + if (x0) + { + if (maze[x][y-1] == 1 && visited[x][y-1] == 0) + { + maze[x][y-1] = maze[x][y] + 1; + dfs(maze,x,y-1,visited,num_s); + } + } + if (y +#include + +struct Node{ + int x; + int y; + int cnt; + struct Node *next; +}; + +char **map; +int **visit; + +int search_map(struct Node* head, int n, int m); + +int main(void){ + int n, m; + int cnt = 0, max = 0; + struct Node * head; + struct Node * new_node; + + scanf("%d %d", &n, &m); + map = (char**)malloc(n*sizeof(char*)); + visit = (int**)calloc(n, sizeof(int*)); + for(int i=0; inext = new_node; + new_node -> x = i; + new_node -> y = j; + new_node -> cnt = 0; + new_node -> next = NULL; + + cnt = search_map(head, n, m); + + if(cnt > max){ + max = cnt; + } + } + } + printf("%d\n", max); +} + +int search_map(struct Node* head, int n, int m){ + int max = 0; + int x[4] = {1, -1, 0, 0}; + int y[4] = {0, 0, 1, -1}; + int X, Y; + struct Node *temp, *new, *use; + temp = head; + temp = temp->next; + use = temp; + visit[temp->x][temp->y] = 1; + + while(head -> next != NULL){ + temp = head->next; + if(temp == NULL) + break; + for(int i=0; i<4; i++){ + X = temp->x + x[i]; + Y = temp->y + y[i]; + if(X < 0 || X >= n || Y <0 || Y >= m){ + continue; + } + if(visit[X][Y] == 1 || map[X][Y] == 'W'){ + continue; + } + + visit[X][Y] = 1; + new = (struct Node*)malloc(1*sizeof(struct Node)); + new->x = X; + new->y = Y; + new->cnt = temp->cnt + 1; + new->next = NULL; + use->next = new; + use = use->next; + if(new->cnt > max) + max = new->cnt; + + } + + if(temp->next == NULL) + break; + head->next = temp->next; + free(temp); + } + + for(int i=0; i +#include +#include "QUEUE.h" + +int M,N; + +int bfs(char map[][N],char visited[][N],int x, int y){ + struct QUEUE* queue = createQueue(M*N); + enqueue(queue,) + visited[x][y] = 1; +} + +int main(){ + scanf("%d %d",&M,&N); + char map[M][N]; + for (int i = 0; i < M; i++) + { + getchar(); + for (int j = 0; j < N; j++) + { + scanf("%c",&map[i][j]); + } + } + int visited[M][N]; + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + visited[i][j] = 0; + } + } + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + if (map[i][j] == 'L' && visited[i][j] == 0) + { + bfs(); + } + } + } + +} +*/ \ No newline at end of file diff --git "a/week6/youngjun/6\354\243\274\354\260\250 \353\260\234\355\221\234 DFS, BFS.md" "b/week6/youngjun/6\354\243\274\354\260\250 \353\260\234\355\221\234 DFS, BFS.md" new file mode 100644 index 0000000..a26076e --- /dev/null +++ "b/week6/youngjun/6\354\243\274\354\260\250 \353\260\234\355\221\234 DFS, BFS.md" @@ -0,0 +1,71 @@ +# 6주차 발표 : DFS, BFS + +- 깊이 우선 탐색(DFS, Depth-First Search) +- 너비 우선 탐색(BFS, Breadth-First Search) + +## 1. 정의 + +### 1.1 DFS(Depth-First Search) + +- 깊이 우선 탐색이라고 부르며 그래프에서 깊은 부분을 우선적으로 탐색하는 알고리즘. +- 스택 자료구조 혹은 재귀함수를 이용 + +> DFS 장점 +> +- 현재 경로상의 노드들만 기억하면 되므로 저장 공간의 수요가 비교적 적음 +- 목표 노드가 깊은 단계에 있는 경우 해를 빨리 구할 수 있음 +- BFS 보다 구현이 간단함 + +> DFS 단점 +> +- 단순 검색 속도가 BFS 보다 느림 +- 해가 없는 경우에 빠질 수 있음. 따라서 사전에 탐색할 임의의 깊이를 지정할 필요가 있다. +- DFS는 해를 구하면 탐색이 종료된다. 따라서 구한 해가 최적의 해가 아닐 수 있다. + +> 형태(c언어) +> + +![스크린샷 2023-02-10 오후 3.00.32.png](6%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20DFS,%20BFS%2016d8900e3af547f595886ede779d549e/%25EC%258A%25A4%25ED%2581%25AC%25EB%25A6%25B0%25EC%2583%25B7_2023-02-10_%25EC%2598%25A4%25ED%259B%2584_3.00.32.png) + +### 1.2 BFS(Breadth-First Search) + +- 너비 우선 탐색이라고 부르며 그래프에서 가까운 노드부터 우선적으로 탐색하는 알고리즘. +- 큐 자료구조를 이용 + +> BFS 의 장점 +> +- 노드의 수가 적고 깊이가 얕은 경우 빠르게 탐색할 수 있다. +- 단순 검색 속도가 DFS 보다 빠르다. +- 너비를 우선 탐색하므로 답이 되는 경로가 여러개여도 최단경로를 보장한다. + +> BFS의 단점 +> +- 큐에 다음에 탐색할 정점들을 저장해야 하므로 이에 따른 저장공간이 많이 필요하다. +- 노드의 수가 많아지면 탐색할 노드 또한 많아져 시간이 오래 걸린다. + +> 형태(c언어, c언어는 큐함수를 따로 지원을 안해서 직접 작성해야함 유의) +> + +![스크린샷 2023-02-07 오후 6.48.11.png](6%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20DFS,%20BFS%2016d8900e3af547f595886ede779d549e/%25EC%258A%25A4%25ED%2581%25AC%25EB%25A6%25B0%25EC%2583%25B7_2023-02-07_%25EC%2598%25A4%25ED%259B%2584_6.48.11.png) + +## 2. 문제에서의 선택 + +1) 그래프의 모든 정점을 방문하는 것이 핵심인 문제 + +DFS,BFS 중 상관없음. 편한거 사용하면 됌. + +2) 경로의 특징을 저장해둬야 하는 문제 + +예를 들면 각 정점에 숫자가 적혀있고 a부터 b까지 가는 경로를 구하는데 경로에 같은 숫자가 있으면 안 된다는 문제 등, 각각의 경로마다 특징을 저장해둬야 할 때는 DFS를 사용한다. + +3) 최단경로를 구해야 하는 문제 + +미로 찾기 등 최단거리를 구해야 할 경우, DFS도 가능하긴 하지만 BFS가 더 유리하다. + +왜냐하면 DFS로 경로를 검색할 경우 처음으로 발견되는 해답이 최단거리가 아닐 수 있지만, **BFS로 경로를 탐색 시 먼저 찾아지는 해답이 곧 최단거리이기 때문이다. + +4) 그래프 규모 + +BFS는 저장공간이 많이 필요하므로 그래프의 규모가 크다면 DFS를 우선적으로 고려, + +그래프 규모가 작으면 BFS를 우선으로 고려한다. \ No newline at end of file diff --git a/week6/youngjun/QUEUE.h b/week6/youngjun/QUEUE.h new file mode 100644 index 0000000..9f52fb5 --- /dev/null +++ b/week6/youngjun/QUEUE.h @@ -0,0 +1,69 @@ + +#include +#include +#include + + +struct Queue { // 큐 형태 + int front, rear, size; + unsigned capacity; + int* array; +}; + +struct Queue* createQueue(unsigned capacity) //큐 생성 +{ + struct Queue* queue = (struct Queue*)malloc( + sizeof(struct Queue)); + queue->capacity = capacity; + queue->front = queue->size = 0; + queue->rear = capacity - 1; + queue->array = (int*)malloc( + queue->capacity * sizeof(int)); + return queue; +} + +int isFull(struct Queue* queue) //큐가 꽉 찼으면 참 반환! +{ + return (queue->size == queue->capacity); +} + +int isEmpty(struct Queue* queue) //큐가 비었으면 참 반환! +{ + return (queue->size == 0); +} + +void enqueue(struct Queue* queue, int item) //큐 맨뒤에 노드 추가하고 프린트! +{ + if (isFull(queue)) + return; + queue->rear = (queue->rear + 1) + % queue->capacity; + queue->array[queue->rear] = item; + queue->size = queue->size + 1; + //printf("%d ", item); +} + +int dequeue(struct Queue* queue) //큐 맨앞에있는 노드 삭제하고 리턴! +{ + if (isEmpty(queue)) + return INT_MIN; + int item = queue->array[queue->front]; + queue->front = (queue->front + 1) + % queue->capacity; + queue->size = queue->size - 1; + return item; +} + +int front(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->front]; +} + +int rear(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->rear]; +} diff --git a/week7/.DS_Store b/week7/.DS_Store new file mode 100644 index 0000000..833a89d Binary files /dev/null and b/week7/.DS_Store differ diff --git a/week7/youngjun/14889 b/week7/youngjun/14889 new file mode 100755 index 0000000..9537bb2 Binary files /dev/null and b/week7/youngjun/14889 differ diff --git a/week7/youngjun/14889.c b/week7/youngjun/14889.c new file mode 100644 index 0000000..9b41d5d --- /dev/null +++ b/week7/youngjun/14889.c @@ -0,0 +1,130 @@ +//스타트와 링크 + +#include +#define minus(x, y) (((x) > (y)) ? (x-y) : (y-x)) + +int n, cap[20][20]; +int team[20]; // 스타트 팀와 링크 팀을 구분 짓기 위한 배열 +//int index = 0; // team 배열의 index +int min = 100; + +void calculate() { + int a1 = 0, b1 = 0; // 1이 스타트 팀일 때 + int a2 = 0, b2 = 0; // 0이 스타트 팀일 때 + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + continue; + if (team[i] == 1 && team[j] == 1) { + a1 += cap[i][j]; + a2 += cap[j][i]; + } + if (team[i] == 0 && team[j] == 0) { + b1 += cap[i][j]; + b2 += cap[j][i]; + } + } + } + + int gap1 = minus(a1, b1); + int gap2 = minus(a2, b2); + + if (gap1 < min) + min = gap1; + if (gap2 < min) + min = gap2; +} +void comb(int depth, int isChoose, int cnt) { + + if (isChoose == 1) + team[depth] = 1; + else + team[depth] = 0; + + if (depth - cnt == n / 2) + return; + else { + if (depth == n - 1 || cnt == n / 2) { + calculate(); + return; + } + } + + // O를 고르면 + comb(depth + 1, 1, cnt + 1); + // X를 고르면 + comb(depth + 1, 0, cnt); + + return; +} + +int main() { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + getchar(); + for (int j = 0; j < n; j++) + { + scanf("%d", &cap[i][j]); + } + } + comb(0, 1, 1); + + printf("%d", min); +} + + + +/* +#include +int N; +int num = 0; + +int team(int sol[N/2],int arr[][N]){ + +} + +int BT(int k,int sol[N/2],int visited[N+1],int p,int arr[][N]){ + if (k == N/2 && team(sol,arr) < num) + { + num = team(sol,arr); + return; + } + for (int i = 1; i <= N; i++) + { + if (!visited[i] && (p < i)) + { + sol[k] = i; + visited[i] = 1; + BT(k+1,sol,visited,i,arr); + visited[i] = 0; + } + + } +} + +int main(){ + scanf("%d",&N); + int sol[N/2]; + int arr[N][N]; + for (int i = 0; i < N; i++) + { + getchar(); + for (int j = 0; j < N; j++) + { + scanf("%d ",&arr[i][j]); + } + } + int visited[N+1]; + for (int i = 0; i <= N; i++) + { + visited[i] = 0; + } + + + +} +*/ \ No newline at end of file diff --git a/week7/youngjun/15649 b/week7/youngjun/15649 new file mode 100755 index 0000000..5112669 Binary files /dev/null and b/week7/youngjun/15649 differ diff --git a/week7/youngjun/15649.c b/week7/youngjun/15649.c new file mode 100644 index 0000000..e83332a --- /dev/null +++ b/week7/youngjun/15649.c @@ -0,0 +1,42 @@ +//N과 M(1) + +#include + +//k==M일 때를 기점으로 BT함수가 계속 리턴되면서 백트래킹 하는 흐름을 생각해보기 +int N,M; + +void BT(int k,int arr[M],int visited[N+1]){ + if (k == M) + { + printf("\n"); + for (int i = 0; i < M; i++) + { + printf("%d ",arr[i]); + } + return; + } + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + { + arr[k] = i; + visited[i] = 1; + BT(k+1,arr,visited); + visited[i] = 0; //백트래킹하면서 i를 다시 사용할 수 있도록 설정 + } + + } +} + +int main(){ + scanf("%d %d",&N,&M); + int arr[M]; + int visited[N+1]; + for (int i = 0; i <= N; i++) + { + visited[i] = 0; + } + BT(0,arr,visited); + + return 0; +} \ No newline at end of file diff --git a/week7/youngjun/15650 b/week7/youngjun/15650 new file mode 100755 index 0000000..2f910bb Binary files /dev/null and b/week7/youngjun/15650 differ diff --git a/week7/youngjun/15650.c b/week7/youngjun/15650.c new file mode 100644 index 0000000..4901018 --- /dev/null +++ b/week7/youngjun/15650.c @@ -0,0 +1,42 @@ +//N과 M (2) + +#include + +int N,M; + +void BT(int k,int arr[M],int visited[N+1],int p){ + if (k == M) + { + printf("\n"); + for (int i = 0; i < M; i++) + { + printf("%d ",arr[i]); + } + return; + } + for (int i = 1; i <= N; i++) + { + if (!visited[i] && (p < i)) //잘 생각해보기 //visited배열 없어도 풀어진다?? + { + arr[k] = i; + visited[i] = 1; + BT(k+1,arr,visited,i); + visited[i] = 0; + } + + } +} + +int main(){ + scanf("%d %d",&N,&M); + int arr[M]; + + int visited[N+1]; + for (int i = 0; i <= N; i++) + { + visited[i] = 0; + } + BT(0,arr,visited,0); + + return 0; +} \ No newline at end of file diff --git a/week7/youngjun/15652 b/week7/youngjun/15652 new file mode 100755 index 0000000..8baa6a1 Binary files /dev/null and b/week7/youngjun/15652 differ diff --git a/week7/youngjun/15652.c b/week7/youngjun/15652.c new file mode 100644 index 0000000..1edeab3 --- /dev/null +++ b/week7/youngjun/15652.c @@ -0,0 +1,34 @@ +//N과 M(4) + +#include +int N,M; + +void BT(int k,int arr[M],int p){ + if (k == M) + { + printf("\n"); + for (int i = 0; i < M; i++) + { + printf("%d ",arr[i]); + } + return; + } + for (int i = 1; i <= N; i++) + { + if (p <= i) + { + arr[k] = i; + BT(k+1,arr,i); + } + } + +} + +int main(){ + scanf("%d %d",&N,&M); + int arr[M]; + + BT(0,arr,0); + + return 0; +} \ No newline at end of file diff --git a/week7/youngjun/2580.c b/week7/youngjun/2580.c new file mode 100644 index 0000000..e69de29 diff --git "a/week7/youngjun/7\354\243\274\354\260\250 \353\260\234\355\221\234 \353\260\261\355\212\270\353\236\230\355\202\271.md" "b/week7/youngjun/7\354\243\274\354\260\250 \353\260\234\355\221\234 \353\260\261\355\212\270\353\236\230\355\202\271.md" new file mode 100644 index 0000000..6087855 --- /dev/null +++ "b/week7/youngjun/7\354\243\274\354\260\250 \353\260\234\355\221\234 \353\260\261\355\212\270\353\236\230\355\202\271.md" @@ -0,0 +1,38 @@ +# 7주차 발표 : 백트래킹 + +## 1. 정의 + +백트래킹이란 해를 찾는 도중 해가 아니어서 막히면, 되돌아가서 다시 해를 찾아가는 기법 + +![Untitled](7%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20%E1%84%87%E1%85%A2%E1%86%A8%E1%84%90%E1%85%B3%E1%84%85%E1%85%A2%E1%84%8F%E1%85%B5%E1%86%BC%200d211495e984430b9c066c88793ad07a/Untitled.png) + +- 재귀함수를 사용한다는 것에서 DFS와 비슷함. +- DFS는 가능한 모든 경로를 탐색하지만 백트래킹은 특정한 조건을 만족하는 경우만 살펴봄. ⇒ 효율성 좋음 + +**즉, 답이 될 만한지 판단하고 그렇지 않으면 그 부분까지 탐색하는 것을 하지 않고 가지치기 하는 것이 백트래킹!** + +## 2. 용어 + +### 2.1 유망성 + +- 해(정답)가 될 수 있는 노드를 유망하다고 함. +- 유망하지 않는 노드의 이전 노드으로 돌아가 ****다음 자식 노드로 이동한다. + +--- + +### 2.2 가지치기 + +- 조건에 어긋나서(유망하지 않아서) 그 경로로 갈 시도를 하지 않는 것 +- 즉, 유망하지 않는 노드에 가지 않는 것을 가지치기라고 한다. + +![Untitled](7%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20%E1%84%87%E1%85%A2%E1%86%A8%E1%84%90%E1%85%B3%E1%84%85%E1%85%A2%E1%84%8F%E1%85%B5%E1%86%BC%200d211495e984430b9c066c88793ad07a/Untitled%201.png) + +## 3. 문제 풀이 + +> DFS 등으로 모든 경우의 수를 탐색하는 과정에서, 조건문 등을 걸어 답이 절대로 될 수 없는 상황을 정의하고, 그러한 상황일 경우에는 탐색을 중지시킨 뒤 그 이전으로 돌아가서 다시 다른 경우를 탐색하게끔 구현하는게 일반적. +> +- 보통 DFS만 써서 푸는 문제는 없기 때문에 백트래킹을 DFS의 문제버전이라고 생각하면 좋다. + +//형태(c언어) + +![스크린샷 2023-02-17 오후 2.52.09.png](7%E1%84%8C%E1%85%AE%E1%84%8E%E1%85%A1%20%E1%84%87%E1%85%A1%E1%86%AF%E1%84%91%E1%85%AD%20%E1%84%87%E1%85%A2%E1%86%A8%E1%84%90%E1%85%B3%E1%84%85%E1%85%A2%E1%84%8F%E1%85%B5%E1%86%BC%200d211495e984430b9c066c88793ad07a/%25EC%258A%25A4%25ED%2581%25AC%25EB%25A6%25B0%25EC%2583%25B7_2023-02-17_%25EC%2598%25A4%25ED%259B%2584_2.52.09.png) \ No newline at end of file diff --git a/week8/.DS_Store b/week8/.DS_Store new file mode 100644 index 0000000..930685d Binary files /dev/null and b/week8/.DS_Store differ diff --git a/week8/youngjun/1261 b/week8/youngjun/1261 new file mode 100755 index 0000000..121100c Binary files /dev/null and b/week8/youngjun/1261 differ diff --git a/week8/youngjun/1261.c b/week8/youngjun/1261.c new file mode 100644 index 0000000..7a26051 --- /dev/null +++ b/week8/youngjun/1261.c @@ -0,0 +1,79 @@ +//최단거리 + +#include +#include +int INF = 10000; + + +int M,N; + +int dx[4] = {1,-1,0,0}; +int dy[4] = {0,0,1,-1}; + +void dijkstra(int maze[][M+1],int answer[][M+1]){ + int front = 0, rear = 0; + int q[M*N*4][2]; + q[front][0] = 1; + q[front][1] = 1; + rear++; + answer[1][1] = 0; // 초기 거리값 = 0 + + while (front < rear) + { + int cx = q[front][0]; + int cy = q[front][1]; + front++; + + for (int i = 0; i < 4; i++) + { + int nx = cx + dx[i]; + int ny = cy + dy[i]; + + if (nx < 1 || nx > N || ny < 1 || ny > M) + continue; + if (maze[nx][ny] == 1) //벽을 지날때 + { + if (answer[nx][ny] > answer[cx][cy] + 1) + { + answer[nx][ny] = answer[cx][cy] + 1; + q[rear][0] = nx; // 이 시점에서 rear == front !! + q[rear][1] = ny; + rear++; + } + } + else if (maze[nx][ny] == 0) + { + if (answer[nx][ny] > answer[cx][cy]) //빈 방을 지날 때 + { + answer[nx][ny] = answer[cx][cy]; + q[rear][0] = nx; + q[rear][1] = ny; + rear++; + } + } + } + } +} + +int main(){ + scanf("%d%2d",&M,&N); + int maze[N+1][M+1]; + int answer[N+1][M+1]; + + + for (int i = 1; i <= N; i++) + { + getchar(); + for (int j = 1; j <= M; j++) + { + scanf("%1d",&maze[i][j]); + answer[i][j] = INF; + } + } + + dijkstra(maze,answer); + + printf("\n%d",answer[N][M]); + + return 0; +} \ No newline at end of file