├── 2018-linePlus-1st-codingTest ├── 5.cpp ├── 6.cpp ├── 2.cpp ├── 1.cpp ├── 4.cpp └── 3.cpp ├── kakao-2018-1st-codingTest ├── 6.cpp ├── 5.cpp ├── 4.cpp ├── 2.cpp └── 1.cpp ├── baekjoon ├── 10872.cpp ├── 2747.cpp ├── 2748.cpp ├── 11727.cpp ├── 11762.cpp ├── 9095.cpp ├── 1427.cpp ├── 10989.cpp ├── 2193.cpp ├── 2750.cpp ├── 2751.cpp ├── 11050.cpp ├── gcd.cpp ├── 2163.cpp ├── 1181.cpp ├── 11052.cpp ├── 6591.cpp ├── 1978.cpp ├── 1929.cpp ├── 1026.cpp ├── array.cpp ├── 11053.cpp ├── 1010.cpp ├── 13458.cpp ├── 13458__2.cpp ├── 2167.cpp ├── 13458_1.cpp ├── 4948.cpp ├── 11057.cpp ├── 2606.cpp ├── 6603.cpp ├── 11866.cpp ├── 14501_1.cpp ├── 2581.cpp ├── 1890.cpp ├── 9465.cpp ├── 2407.cpp ├── 2294.cpp ├── 1676.cpp ├── 11048.cpp ├── 11724.cpp ├── 2644.cpp ├── 14501.cpp ├── 2504.cpp ├── 1987.cpp ├── 9020.cpp ├── 1018.cpp ├── combination.cpp ├── 14888_2.cpp ├── 5014.cpp ├── 1389.cpp ├── 10845.cpp ├── 14888.cpp ├── 1325.cpp ├── 1697.cpp ├── 1021.cpp ├── 11403.cpp ├── 5214.cpp ├── 14888_1.cpp ├── 1012.cpp ├── 9466.cpp ├── 2667.cpp ├── 2108.cpp ├── 7562.cpp ├── 14889_1.cpp ├── 1966.cpp ├── 2589.cpp ├── 1707.cpp ├── 2178.cpp ├── 14503_1.cpp ├── 2468.cpp ├── 7576.cpp ├── 14889_2.cpp ├── 1504.cpp ├── 14889.cpp ├── 10216.cpp ├── 2583.cpp ├── 15686.cpp ├── 10866.cpp ├── 14891_1.cpp ├── 15685.cpp ├── 1103.cpp ├── 14500.cpp ├── 7569.cpp ├── 1753.cpp ├── 2206.cpp ├── 3055.cpp ├── 14503.cpp ├── 14502_1.cpp ├── 14502.cpp ├── 14891.cpp ├── 14499_1.cpp ├── 1063.cpp ├── 9019.cpp ├── 1062.cpp ├── 3190.cpp ├── 5430.cpp ├── 13460.cpp ├── 2146.cpp ├── 14499.cpp ├── 1963.cpp ├── 14502_2.cpp ├── 3190_1.cpp ├── 10026.cpp └── 2573.cpp ├── kakao ├── notation.cpp ├── secretmap.cpp ├── cache.cpp ├── news_clustering.cpp ├── compression.cpp ├── friends_4_Block.cpp ├── dart_game.cpp ├── song.cpp ├── file_sort.cpp └── traffic.cpp ├── 2018-winter-coding ├── 2.cpp ├── 1.cpp └── 3.cpp └── SW_EXPERT ├── 4008.cpp ├── 5658_1.cpp ├── 4012.cpp ├── 2117.cpp ├── 5658.cpp ├── 4013_1.cpp ├── 2117_1.cpp ├── 5648_1.cpp ├── 4013.cpp ├── 1953_1.cpp ├── 2112.cpp ├── 1949.cpp ├── 5648.cpp └── 2112_1.cpp /2018-linePlus-1st-codingTest/5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-29. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /2018-linePlus-1st-codingTest/6.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-29. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /kakao-2018-1st-codingTest/6.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main(){ 10 | int* a; 11 | int* b; 12 | int c = 3; 13 | a = b = &c; 14 | cout << *a <<'\t'<< *b << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /baekjoon/10872.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | int arr[13]; 8 | int main(){ 9 | arr[0] = 1; 10 | arr[1] = 1; 11 | int N; 12 | cin >> N; 13 | for(int i = 2 ; i <= N ; i++) 14 | arr[i] = arr[i-1] * i; 15 | 16 | cout << arr[N]; 17 | 18 | } -------------------------------------------------------------------------------- /kakao-2018-1st-codingTest/5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-15. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | struct Node{ 10 | int y,x,num; 11 | }; 12 | vector> nodeinfo = {{5,3},{11,5},{13,3},{3,5},{6,1},{1,3},{8,6},{7,2},{2,2}}; 13 | int main(){ 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /baekjoon/2747.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | int arr[46]; 8 | int main(){ 9 | int N; 10 | arr[1] = 1; 11 | cin >> N; 12 | for(int i = 2 ; i <= N; i++){ 13 | arr[i] = arr[i-1] + arr[i-2]; 14 | } 15 | 16 | cout < 6 | using namespace std; 7 | long long arr[90]; 8 | int main(){ 9 | int N; 10 | arr[1] = 1; 11 | cin >> N; 12 | for(int i = 2 ; i <= N; i++){ 13 | arr[i] = arr[i-1] + arr[i-2]; 14 | } 15 | 16 | cout < 5 | using namespace std; 6 | 7 | int dp[1001]; 8 | 9 | int main(){ 10 | int N; 11 | cin >> N; 12 | dp[0] = 1; 13 | dp[1] = 1; 14 | for(int i = 2 ; i <= N ; i++) 15 | dp[i] = (dp[i-1] + dp[i-2] * 2)%10007; 16 | 17 | cout << dp[N]; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /baekjoon/11762.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-02-02. 3 | // 4 | #include 5 | using namespace std; 6 | 7 | int dp[1001]; 8 | 9 | int main(){ 10 | int N; 11 | cin >> N; 12 | 13 | dp[1] = 1; 14 | dp[2] = 2; 15 | 16 | for(int i = 3 ; i <= N ; i++){ 17 | dp[i] = (dp[i-1] + dp[i-2])%10007; 18 | } 19 | 20 | cout << dp[N] << endl; 21 | 22 | } -------------------------------------------------------------------------------- /baekjoon/9095.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int dp[11]; 4 | 5 | int main() { 6 | dp[1] = 1; 7 | dp[2] = 2; 8 | dp[3] = 4; 9 | 10 | for(int i = 4 ; i <11 ; i++) 11 | dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; 12 | 13 | int N; 14 | int c; 15 | cin >> N; 16 | while(N--){ 17 | cin >> c; 18 | cout< 6 | using namespace std; 7 | int arr[10]; 8 | int main(){ 9 | 10 | int N; 11 | cin >>N; 12 | while(N){ 13 | arr[N%10]++; 14 | N /= 10; 15 | } 16 | 17 | for(int i = 9 ; i > -1 ; i--){ 18 | while(arr[i]--){ 19 | cout << i; 20 | } 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /baekjoon/10989.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | #include 5 | 6 | using namespace std; 7 | int arr[10001]; 8 | int main(){ 9 | int N; 10 | cin >> N; 11 | int a; 12 | 13 | for(int i = 1 ; i <= N ; i++){ 14 | cin >> a; 15 | arr[a]++; 16 | } 17 | 18 | for(int i = 1 ; i <= 10000 ; i++){ 19 | for(int j = 1 ; j <= arr[i] ; j++) 20 | cout << i << '\n'; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /baekjoon/2193.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-02-01. 3 | // 4 | #include 5 | using namespace std; 6 | 7 | 8 | 9 | int main(){ 10 | long long dp[91][2]; 11 | dp[1][0] = 0; 12 | dp[1][1] = 1; 13 | int N; 14 | cin >> N; 15 | 16 | for(int i = 2 ; i <= N ; i++){ 17 | dp[i][0] = dp[i-1][0] + dp[i-1][1]; 18 | dp[i][1] = dp[i-1][0]; 19 | } 20 | 21 | cout << dp[N][0] + dp[N][1]; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /baekjoon/2750.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | int arr[1001]; 10 | int main(){ 11 | 12 | int N; 13 | cin >> N; 14 | for(int i = 1; i <= N; i++){ 15 | cin >> arr[i]; 16 | } 17 | 18 | sort(&arr[1], &arr[N+1]); 19 | 20 | for(int i = 1; i <= N; i++){ 21 | cout << arr[i] << '\n'; 22 | } 23 | 24 | 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /baekjoon/2751.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | int arr[10001]; 10 | int main(){ 11 | int N; 12 | cin >> N; 13 | int a; 14 | for(int i = 1 ; i <= N ; i++){ 15 | cin >> a; 16 | arr[a]++; 17 | } 18 | 19 | sort(arr+1,arr+1+N,comp); 20 | 21 | for(int i = 1 ; i <= N ; i++){ 22 | cout << arr[i] << '\n'; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /baekjoon/11050.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int arr[11][11]; //(N,K) 9 | int main(){ 10 | for(int i = 0; i <= 10 ; i++){ 11 | arr[i][i] = 1; 12 | arr[i][0] = 1; 13 | } 14 | 15 | int N,K; 16 | cin >> N >> K; 17 | for(int i = 2 ; i <= N ; i++){ 18 | for(int j = 1 ; j < i ; j++){ 19 | arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; 20 | } 21 | } 22 | 23 | cout << arr[N][K]; 24 | } -------------------------------------------------------------------------------- /baekjoon/gcd.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-18. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | int a,b; 10 | a = 180; 11 | b = 18*3; 12 | int tmp; 13 | if(a < b){ 14 | tmp = a; 15 | a = b; 16 | b = tmp; 17 | } 18 | while(a % b){ 19 | cout <<"a : "<< a << "," << "b : " << b << endl; 20 | tmp = a%b; 21 | a = b; 22 | b = tmp; 23 | } 24 | cout <<"sol : "<< b << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /baekjoon/2163.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-01. 3 | // 4 | #include 5 | using namespace std; 6 | int dp[301][301]; 7 | int main(){ 8 | int N,M; 9 | cin >> N >> M; 10 | dp[1][1] = 0; 11 | for(int i = 1 ; i <= N ; i++) 12 | dp[i][1] = i-1; 13 | for(int i = 1 ; i <= M ; i++) 14 | dp[1][i] = i-1; 15 | for(int i = 2; i <= N ; i++){ 16 | for(int j = 2 ; j <= M ; j++){ 17 | dp[i][j] = i*dp[1][j] + i-1; 18 | } 19 | } 20 | 21 | cout << dp[N][M] << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /baekjoon/1181.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | set arr[51]; 12 | int main(){ 13 | 14 | int N; 15 | cin >> N; 16 | string a; 17 | while(N--){ 18 | cin >> a; 19 | arr[a.length()].insert(a); 20 | } 21 | 22 | for(int i = 1; i < 51 ; i++){ 23 | for(auto it = arr[i].begin() ; it != arr[i].end() ; it++) 24 | cout << *(it) << '\n'; 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /baekjoon/11052.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-31. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | int price[1001]; 8 | int dp[1001]; 9 | int main(){ 10 | int N; 11 | cin >> N; 12 | for(int i = 1; i <= N ; i++) 13 | cin >> price[i]; 14 | 15 | for(int i = 1; i <= N ; i++){ 16 | int a = 0; 17 | for(int j = 0 ; j < i ; j++){ 18 | if(a < dp[j] + price[i-j]) 19 | a = dp[j] + price[i-j]; 20 | } 21 | dp[i] = a; 22 | } 23 | 24 | cout << dp[N] << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /baekjoon/6591.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | 10 | long long N,K; 11 | while(1) { 12 | cin >> N >> K; 13 | 14 | long long sol = 1; 15 | int s; 16 | if( N == 0 && K == 0 ) 17 | break; 18 | 19 | if(K < N - K) 20 | s = K; 21 | else 22 | s = N - K; 23 | 24 | for(int i = 1; i <= s ; i++){ 25 | sol *= N; 26 | sol /= i; 27 | N--; 28 | } 29 | cout < 2 | #include 3 | 4 | using namespace std; 5 | int **arr; 6 | 7 | int main() { 8 | int N; 9 | cin >> N; 10 | arr = new int *[N]; 11 | for (int i = 0; i < N; i++) 12 | arr[i] = new int[N]; 13 | 14 | //fill_n(&arr[0][0],N*N,1); 15 | for (int i = 0; i < N; i++) { 16 | for (int j = 0; j < N; j++) { 17 | arr[i][j] = i + j; 18 | } 19 | } 20 | 21 | for (int i = 0; i < N; i++) { 22 | for (int j = 0; j < N; j++) { 23 | cout << arr[i][j] << " "; 24 | } 25 | cout << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /baekjoon/1978.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-12. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | int N; 11 | cin >> N; 12 | int a; 13 | int sol = 0; 14 | bool chk; 15 | while(N--){ 16 | cin >> a; 17 | if(a == 1){ 18 | continue; 19 | } 20 | chk = true; 21 | for(int i = 2 ; i <=sqrt((double)a) ; i++){ 22 | if(a % i == 0){ 23 | chk = false; 24 | break; 25 | } 26 | } 27 | if(chk) 28 | sol++; 29 | } 30 | cout << sol; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /baekjoon/1929.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-12. 3 | // 4 | // 5 | // Created by vkdne on 2018-03-12. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int M,N; 14 | cin >> M >> N; 15 | bool chk; 16 | for(int i = M ; i <= N ; i++){ 17 | chk = true; 18 | if(i == 1) 19 | continue; 20 | for(int j = 2 ; j <= sqrt((double)i) ; j++){ 21 | if(i%j == 0){ 22 | chk = false; 23 | break; 24 | } 25 | } 26 | if(chk){ 27 | cout << i << '\n'; 28 | } 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /baekjoon/1026.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-12. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | vector A; 10 | vector B; 11 | int N; 12 | int main(){ 13 | cin >> N; 14 | int a; 15 | for(int i = 0 ; i < N ; i++){ 16 | cin >> a; 17 | A.push_back(a); 18 | } 19 | for(int i = 0 ; i < N ; i++){ 20 | cin >> a; 21 | B.push_back(a); 22 | } 23 | sort(A.begin(),A.end(),greater()); 24 | sort(B.begin(),B.end()); 25 | int sol = 0; 26 | for(int i = 0 ; i < N ; i++) { 27 | sol += A[i]*B[i]; 28 | } 29 | cout << sol << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /baekjoon/array.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-21. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | struct xy{ 9 | int x,y; 10 | // bool operator <(const xy& a)const{ 11 | // return this->y > a.y; 12 | // }; 13 | }; 14 | struct compare{ 15 | bool operator()(const xy a, const xy b){ 16 | return a.y < b.y; 17 | } 18 | }; 19 | priority_queue,compare> pq; 20 | int main(){ 21 | pq.push({1,5}); 22 | pq.push({2,4}); 23 | pq.push({3,3}); 24 | pq.push({4,2}); 25 | pq.push({5,1}); 26 | while(!pq.empty()){ 27 | cout << pq.top().x <<","< 6 | #include 7 | using namespace std; 8 | 9 | int dp[1001]; 10 | int arr[1001]; 11 | 12 | int main(){ 13 | int N; 14 | cin >> N; 15 | for (int i = 1; i <= N; i++) { 16 | dp[i] = 1; 17 | cin >> arr[i]; 18 | } 19 | int sol = 0; 20 | for (int i = 1; i <= N; i++) { 21 | for (int j = 1; j < i; j++) { 22 | if (arr[i] > arr[j]) { 23 | dp[i] = max(dp[i], dp[j] + 1); 24 | } 25 | } 26 | sol = max(sol, dp[i]); 27 | } 28 | cout << sol << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /baekjoon/1010.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-02-02. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int dp[31][31]; 9 | 10 | int main(){ 11 | for(int i = 0 ; i <31 ; i++) { 12 | dp[i][i] = 1; 13 | dp[i][0] = 1; 14 | } 15 | 16 | for(int i = 2; i < 31 ; i++){ 17 | for(int j = 1 ; j != i ; j++){ 18 | dp[i][j] = dp[i-1][j] + dp[i-1][j-1]; 19 | } 20 | } 21 | 22 | int T; 23 | cin >> T; 24 | int a,b; 25 | while(T--){ 26 | cin >> a >> b; 27 | if(a >= b){ 28 | cout << dp[a][b] << endl; 29 | } 30 | else{ 31 | cout << dp[b][a] << endl; 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /baekjoon/13458.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-03. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector room; 10 | int N,B,C; 11 | long long sol; 12 | int main(){ 13 | ios_base::sync_with_stdio(false); 14 | cin >> N; 15 | int tmp; 16 | for(int i = 0 ; i < N ; i++){ 17 | cin >> tmp; 18 | room.push_back(tmp); 19 | } 20 | 21 | cin >> B >> C; 22 | 23 | for(int i = 0 ; i < N ; i++){ 24 | if(room[i] > B){ 25 | if((room[i] - B) % C == 0) 26 | sol += (room[i] - B) / C; 27 | else 28 | sol += ((room[i] - B) / C)+1; 29 | } 30 | } 31 | cout << sol + N << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /baekjoon/13458__2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-19. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | long long N,B,C,sol; 9 | vector v; 10 | int main(){ 11 | ios_base::sync_with_stdio(false); 12 | cin >> N; 13 | v.reserve(N); 14 | int a; 15 | for(int i = 0 ; i < N ; i++) { 16 | cin >> a; 17 | v.push_back(a); 18 | } 19 | cin >> B >> C; 20 | for(int i = 0 ; i < N ; i++){ 21 | a = v[i] - B; 22 | sol++; 23 | if( a> 0){ 24 | if(a % C == 0){ 25 | sol += a/C; 26 | } 27 | else{ 28 | sol += a/C+1; 29 | } 30 | } 31 | } 32 | cout << sol << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /baekjoon/2167.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-31. 3 | // 4 | #include 5 | using namespace std; 6 | int arr[301][301]; 7 | int dp[301][301]; 8 | int main(){ 9 | int N,M; 10 | int sum = 0; 11 | cin >> N >> M; 12 | for(int i = 1 ; i <= N ; i++){ 13 | for(int j = 1; j <= M ; j++){ 14 | cin >> arr[i][j]; 15 | dp[i][j] = arr[i][j] + sum; 16 | sum += arr[i][j]; 17 | } 18 | } 19 | int K; 20 | cin >> K; 21 | while(K--){ 22 | int a,b,c,d; 23 | cin >> a >> b >> c >> d; 24 | int sol = 0; 25 | for(int i = a ; i <= c ; i++){ 26 | sol += (dp[i][d] - dp[i][b] + arr[i][b]); 27 | } 28 | cout << sol << endl; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /baekjoon/13458_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-21. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int N,B,C; 10 | vector v; 11 | long long sol = 0; 12 | int main(){ 13 | ios_base::sync_with_stdio(false); 14 | cin >> N; 15 | int A; 16 | for(int i = 0 ;i < N ; i++){ 17 | cin >> A; 18 | v.push_back(A); 19 | } 20 | cin >> B >> C; 21 | for(int i = 0 ; i < v.size() ; i++){ 22 | if(v[i] - B > 0){ 23 | if((v[i] - B) % C == 0){ 24 | sol += (v[i] - B) / C; 25 | } 26 | else{ 27 | sol += (v[i] - B) / C; 28 | sol++; 29 | } 30 | } 31 | } 32 | cout << N + sol << '\n'; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /baekjoon/4948.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-12. 3 | // 4 | // 5 | // Created by vkdne on 2018-03-12. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | while(1) { 14 | int M; 15 | cin >> M; 16 | if(M == 0) 17 | break; 18 | bool chk; 19 | int sol = 0; 20 | for (int i = M + 1; i <= 2 * M; i++) { 21 | chk = true; 22 | if (i == 1) 23 | continue; 24 | for (int j = 2; j <= sqrt((double) i); j++) { 25 | if (i % j == 0) { 26 | chk = false; 27 | break; 28 | } 29 | } 30 | if (chk) { 31 | sol++; 32 | } 33 | } 34 | cout << sol << '\n'; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /baekjoon/11057.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-01. 3 | // 4 | #include 5 | using namespace std; 6 | int dp[1001][10]; 7 | int main(){ 8 | //while(1) { 9 | int N; 10 | cin >> N; 11 | for (int i = 0; i <= 9; i++) 12 | dp[1][i] = 1; 13 | for (int i = 0; i <= N; i++) 14 | dp[i][0] = 1; 15 | 16 | for (int i = 2; i <= N; i++) { 17 | for (int j = 1; j <= 9; j++) { 18 | int sum = 0; 19 | for (int k = 0; k <= j; k++) { 20 | sum += dp[i - 1][k]; 21 | } 22 | dp[i][j] = sum % 10007; 23 | } 24 | } 25 | 26 | cout << (dp[N][0] + dp[N][1] + dp[N][2] + dp[N][3] + dp[N][4] + dp[N][5] + dp[N][6] + dp[N][7] + dp[N][8] + 27 | dp[N][9])%10007; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /baekjoon/2606.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-20. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | int N; 9 | int arr[101][101]; 10 | int visit[101]; 11 | int cnt = 0; 12 | queue q; 13 | 14 | void BFS(){ 15 | while(!q.empty()){ 16 | int t = q.front(); 17 | q.pop(); 18 | for(int i = 1; i <= N ; i++){ 19 | if(arr[i][t] == 1 && visit[i] == 0){ 20 | cnt++; 21 | visit[i] = 1; 22 | q.push(i); 23 | } 24 | } 25 | } 26 | }; 27 | 28 | int main(){ 29 | cin >> N; 30 | int M; 31 | cin >> M; 32 | int a,b; 33 | for(int i = 1 ; i <= M ; i++){ 34 | cin >> a >> b; 35 | arr[a][b] = arr[b][a] = 1; 36 | } 37 | visit[1] = 1; 38 | q.push(1); 39 | BFS(); 40 | cout << cnt << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /baekjoon/6603.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-27. 3 | // 4 | #include 5 | using namespace std; 6 | int N; 7 | int arr[13]; 8 | int sol[7]; 9 | 10 | void DFS(int depth, int lastldx){ 11 | if(depth == 6){ 12 | for(int i = 1 ; i <= 6 ; i++){ 13 | cout<> N; 28 | if(N == 0){ 29 | break; 30 | } 31 | for(int i = 1 ; i <= N ; i++) 32 | cin >> arr[i]; 33 | 34 | for(int i = 1 ; i <= N - 5 ; i++){ 35 | sol[1] = arr[i]; 36 | DFS(1,i); 37 | } 38 | cout << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /baekjoon/11866.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(){ 11 | queue q; 12 | int N,M; 13 | 14 | cin >> N >> M; 15 | for(int i = 1; i <= N ; i++){ 16 | q.push(i); 17 | } 18 | int tmp; 19 | 20 | if(N == 1){ 21 | cout<<"<"<"; 22 | return 0; 23 | } 24 | 25 | while(!q.empty()){ 26 | for(int i = 1 ; i < M ; i++){ 27 | tmp = q.front(); 28 | q.pop(); 29 | q.push(tmp); 30 | } 31 | if(q.size() == N){ 32 | cout << "<" << q.front() << ", "; 33 | } 34 | else if(q.size() == 1){ 35 | cout << q.front() << ">"; 36 | } 37 | else{ 38 | cout < 5 | 6 | using namespace std; 7 | 8 | int arr[16][2]; //[0] : t, [1] : p 9 | int N; 10 | int sol; 11 | 12 | void dfs(int current, int to, int profit) { 13 | bool chk = false; 14 | for (int i = current + 1; i <= N; i++) { 15 | if (to < i && arr[i][0] + i - 1 <= N) { 16 | chk = true; 17 | dfs(i, i + arr[i][0] - 1, profit + arr[i][1]); 18 | } 19 | } 20 | if (!chk) { 21 | if (sol < profit) 22 | sol = profit; 23 | } 24 | } 25 | 26 | int main() { 27 | cin >> N; 28 | for (int i = 0; i < N; i++) 29 | cin >> arr[i + 1][0] >> arr[i + 1][1]; 30 | 31 | for (int i = 1; i <= N; i++) { 32 | if (arr[i][0] + i - 1 <= N) { 33 | dfs(i, i + arr[i][0] - 1, arr[i][1]); 34 | } 35 | } 36 | cout << sol << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /baekjoon/2581.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-12. 3 | // 4 | // 5 | // Created by vkdne on 2018-03-12. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int M,N; 14 | cin >> M >> N; 15 | int min = 10001; 16 | int sum = 0; 17 | int cnt = 0; 18 | bool chk; 19 | for(int i = M ; i <= N ; i++){ 20 | chk = true; 21 | if(i == 1) 22 | continue; 23 | for(int j = 2 ; j <= sqrt((double)i) ; j++){ 24 | if(i%j == 0){ 25 | chk = false; 26 | break; 27 | } 28 | } 29 | if(chk){ 30 | sum += i; 31 | cnt++; 32 | if(min > i) 33 | min = i; 34 | } 35 | } 36 | 37 | if(cnt == 0) 38 | cout<<"-1"; 39 | else{ 40 | cout< 5 | using namespace std; 6 | int arr[101][101]; 7 | long long dp[101][101]; 8 | int main(){ 9 | int N; 10 | cin >> N; 11 | 12 | for(int i = 1; i <= N ; i++){ 13 | for(int j = 1; j <= N ; j++) 14 | cin >> arr[i][j]; 15 | } 16 | dp[1][1] = 1; 17 | int ni; 18 | int nj; 19 | for(int i = 1 ; i <= N ; i++){ 20 | for(int j = 1; j <= N; j++){ 21 | if(i == N && j == N) 22 | break; 23 | if(dp[i][j] != 0) { 24 | ni = i + arr[i][j]; 25 | nj = j; 26 | if (ni <= N) 27 | dp[ni][nj]+=dp[i][j]; 28 | ni = i; 29 | nj = j + arr[i][j]; 30 | if (nj <= N) 31 | dp[ni][nj]+=dp[i][j]; 32 | } 33 | } 34 | } 35 | cout < 6 | #include 7 | using namespace std; 8 | 9 | int arr[2][100001]; 10 | int dp[3][100001]; 11 | 12 | int main(){ 13 | std::ios_base::sync_with_stdio(false); 14 | int T; 15 | cin >> T; 16 | while(T--){ 17 | int n; 18 | cin >> n; 19 | for(int i = 0 ; i < 2 ; i++){ 20 | for(int j = 1 ; j <= n ; j++) { 21 | cin >> arr[i][j]; 22 | } 23 | } 24 | dp[0][1] = arr[0][1]; 25 | dp[1][1] = arr[1][1]; 26 | dp[2][1] = 0; 27 | 28 | for(int i = 2 ; i <= n ; i++){ 29 | dp[0][i] = max(dp[1][i-1],dp[2][i-1]) + arr[0][i]; 30 | dp[1][i] = max(dp[0][i-1],dp[2][i-1]) + arr[1][i]; 31 | dp[2][i] = max(max(dp[1][i-1],dp[2][i-1]),dp[0][i-1]); 32 | }; 33 | 34 | cout < 6 | using namespace std; 7 | 8 | struct HL{ 9 | long long H = 0; 10 | long long L = 0; 11 | }; 12 | 13 | HL arr[101][101]; 14 | int main(){ 15 | int N; 16 | int K; 17 | cin>> N >> K; 18 | 19 | long long temp = 10000000000000; 20 | for(int i = 0; i <= N ; i++){ 21 | for(int j = 0; j <=i ; j++){ 22 | if(j == 0 || j == i){ 23 | arr[i][j].L = 1; 24 | continue; 25 | } 26 | arr[i][j].H = (arr[i-1][j-1].H + arr[i-1][j].H); 27 | arr[i][j].L = (arr[i-1][j-1].L + arr[i-1][j].L); 28 | 29 | if(arr[i][j].L >= temp){ 30 | arr[i][j].H++; 31 | arr[i][j].L -= temp; 32 | } 33 | } 34 | } 35 | 36 | if(arr[N][K].H == 0) 37 | cout << arr[N][K].L; 38 | else 39 | cout << arr[N][K].H << arr[N][K].L; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /kakao/notation.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-11. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int n = 2; 11 | int t = 4; 12 | int m = 2; 13 | int p = 1; 14 | string total = ""; 15 | string answer = ""; 16 | int main(){ 17 | int cnt = 1; 18 | while(1){ 19 | string str = ""; 20 | int a; 21 | int tmp = cnt; 22 | while (tmp) { 23 | a = tmp % n; 24 | if(a<10) 25 | str = to_string(tmp % n)+str; 26 | else 27 | str = (char)(55+a)+str; 28 | tmp /= n; 29 | } 30 | total = total+str; 31 | cnt++; 32 | if(total.size() > p+(t-1)*m) { 33 | total = "0"+total; 34 | break; 35 | } 36 | } 37 | cout << total << endl; 38 | for(int i = 0 ; i < t ; i++){ 39 | answer += total[p-1+m*i]; 40 | } 41 | cout << answer << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /baekjoon/2294.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-01. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int dp[10001]; 10 | int c[101]; 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(false); 14 | int n,k; 15 | cin >> n >> k; 16 | for(int i = 1 ; i <= n ; i++) 17 | cin >> c[i]; 18 | 19 | for(int i = 1; i <= n; i++) { 20 | if(c[i] <= 10000) 21 | dp[c[i]] = 1; 22 | } 23 | 24 | for(int i = 1; i <= k ; i++){ 25 | if(dp[i] != 0) { 26 | for (int j = 1; j <= n; j++) { 27 | for (int pos = i + c[j]; pos <= k; pos += c[j]) { 28 | if (dp[pos] > dp[pos - c[j]] + 1 || dp[pos] == 0) { 29 | dp[pos] = dp[pos - c[j]] + 1; 30 | } 31 | } 32 | } 33 | } 34 | } 35 | if(dp[k] == 0) 36 | cout <<"-1"< 2 | // #include // uncomment to use scanf/printf instead of cin/cout 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int sol = 20000; 10 | string line; 11 | getline(cin, line); 12 | stringstream ss(line); 13 | int cost; 14 | for (int distance; !(ss >> distance).fail(); ) { 15 | // @todo Write your code here. 16 | //cou << distance << endl; 17 | if(distance < 4 || distance > 178){ 18 | cout << sol << endl; 19 | return 0; 20 | } 21 | if(distance >= 4 && distance <= 40){ 22 | cost = 720; 23 | } 24 | else{ 25 | cost = 720; 26 | cost += (((distance - 41)/8 + 1)*80); 27 | } 28 | if(sol - cost >= 0) 29 | sol -= cost; 30 | else 31 | break; 32 | } 33 | // @todo Write your code here. 34 | cout << sol << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /baekjoon/1676.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-15. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | long long arr[501][2]; 8 | int main(){ 9 | arr[0][0] = 0; 10 | arr[0][1] = 0; 11 | arr[1][0] = 0; 12 | arr[1][1] = 0; 13 | 14 | int N; 15 | cin >> N; 16 | 17 | for(int i = 2 ; i <= N ; i++){ 18 | int a = 0; //2의 개수 19 | int b = 0; //5의 개수 20 | int k = i; 21 | while(1){ 22 | if(k % 2 == 0){ 23 | a++; 24 | k /= 2; 25 | } 26 | else 27 | break; 28 | } 29 | 30 | while(1){ 31 | if(k % 5 == 0){ 32 | b++; 33 | k /= 5; 34 | } 35 | else 36 | break; 37 | } 38 | arr[i][0] = arr[i-1][0] + a; 39 | arr[i][1] = arr[i-1][1] +b; 40 | } 41 | 42 | if(arr[N][0] > arr[N][1]) 43 | cout << arr[N][1]; 44 | else 45 | cout << arr[N][0]; 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /2018-linePlus-1st-codingTest/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // #include // uncomment to use scanf/printf instead of cin/cout 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int n; 10 | cin >> n; 11 | string line; 12 | getline(cin, line); // drop a newline traling n 13 | while (n-- > 0) { 14 | getline(cin, line); 15 | stringstream ss(line); 16 | string method, url, body; 17 | string type,username,data; 18 | getline(ss, method, ' '); 19 | getline(ss, url, ' '); 20 | getline(ss, body); 21 | 22 | type = url.substr(0,6); 23 | 24 | cout <<"*******************************************"< 6 | #include 7 | using namespace std; 8 | int arr[1001][1001][2]; 9 | int dx[3] = {1,1,0}; 10 | int dy[3] = {0,1,1}; 11 | int dp[1001][1001]; 12 | int main(){ 13 | ios_base::sync_with_stdio(false); 14 | int N, M; 15 | cin >> N >> M; 16 | for (int i = 1; i <= N; i++) { 17 | for (int j = 1; j <= M; j++) { 18 | cin >> arr[i][j][0]; 19 | } 20 | } 21 | 22 | dp[1][1] = arr[1][1][0]; 23 | arr[1][1][1] = 1; 24 | for (int i = 1; i <= N; i++) { 25 | for (int j = 1; j <= M; j++) { 26 | for (int k = 0; k < 3; k++) { 27 | int y = i + dy[k]; 28 | int x = j + dx[k]; 29 | if (y <= N && x <= M) { 30 | dp[y][x] = max(dp[y][x], (dp[i][j] + arr[y][x][0])); 31 | arr[y][x][1] = 1; 32 | } 33 | } 34 | } 35 | } 36 | 37 | cout << dp[N][M] << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /baekjoon/11724.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int N,M; 11 | vector graph[1001]; 12 | queue q; 13 | int visit[1001]; 14 | int cnt; 15 | 16 | void BFS(){ 17 | while(q.size()){ 18 | int tmp = q.front(); 19 | q.pop(); 20 | for(auto it = graph[tmp].begin(); it != graph[tmp].end(); it++){ 21 | if(visit[*it] == 0){ 22 | visit[*it] = 1; 23 | q.push(*it); 24 | } 25 | } 26 | } 27 | } 28 | 29 | 30 | int main(){ 31 | cin >> N >> M; 32 | int a,b; 33 | while(M--){ 34 | cin >> a >> b; 35 | graph[a].push_back(b); 36 | graph[b].push_back(a); 37 | } 38 | 39 | for(int i = 1; i <= N ; i++){ 40 | if(visit[i] == 0){ 41 | cnt++; 42 | visit[i] = 1; 43 | q.push(i); 44 | BFS(); 45 | } 46 | } 47 | cout << cnt<< endl; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /baekjoon/2644.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-06. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int arr[101][101]; 10 | int visit[101]; 11 | struct chon{ 12 | int v,d; 13 | }; 14 | queue q; 15 | int sol = -1; 16 | int N; 17 | int s,e; 18 | 19 | void bfs(){ 20 | q.push({s,0}); 21 | visit[s] = 1; 22 | while(!q.empty()){ 23 | chon tmp = q.front(); 24 | q.pop(); 25 | for(int i = 1 ; i <= N ; i++){ 26 | if(arr[i][tmp.v] == 1 && visit[i] == 0){ 27 | if(i == e){ 28 | sol = tmp.d + 1; 29 | return; 30 | } 31 | visit[i] = 1; 32 | q.push({i,tmp.d+1}); 33 | } 34 | } 35 | } 36 | } 37 | 38 | int main(){ 39 | cin >> N >> s >> e; 40 | int m; 41 | cin >> m; 42 | int a,b; 43 | while(m--){ 44 | cin >> a >> b; 45 | arr[a][b] = 1; 46 | arr[b][a] = 1; 47 | } 48 | bfs(); 49 | cout << sol << endl; 50 | return 0; 51 | } -------------------------------------------------------------------------------- /baekjoon/14501.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-03. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int arr[16][2]; // 0 : 시간 1 : 이득 10 | int sol; 11 | int N; 12 | struct counsil{ 13 | int profit; 14 | int possible; 15 | }; 16 | queue q; 17 | 18 | void bfs(){ 19 | while(q.size()){ 20 | counsil tmp = q.front(); 21 | if(sol < tmp.profit) 22 | sol = tmp.profit; 23 | q.pop(); 24 | for(int i = tmp.possible ; i <= N ; i++){ 25 | counsil next; 26 | next.profit = tmp.profit + arr[i][1]; 27 | next.possible = i + arr[i][0]; 28 | if(next.possible <= N+1) 29 | q.push(next); 30 | } 31 | } 32 | } 33 | 34 | int main(){ 35 | cin >> N; 36 | for(int i = 1 ; i <= N ; i++){ 37 | cin >> arr[i][0] >> arr[i][1]; 38 | } 39 | 40 | for(int i = 1 ; i <= N ; i++) { 41 | if(i+arr[i][0] <= N+1) { 42 | q.push({arr[i][1], i + arr[i][0]}); 43 | bfs(); 44 | } 45 | } 46 | cout << sol << endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /baekjoon/2504.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | string str; 11 | stack st; 12 | int main(){ 13 | cin >> str; 14 | 15 | int sol = 0; 16 | int mul = 1; 17 | for(int i = 0 ; i < str.length() ; i++){ 18 | if(str[i] == '['){ 19 | mul *= 3; 20 | st.push(str[i]); 21 | } 22 | else if(str[i] == '('){ 23 | mul *= 2; 24 | st.push(str[i]); 25 | } 26 | else if(str[i] == ']'){ 27 | if(str[i-1] == '['){ 28 | sol += mul; 29 | } 30 | 31 | if(st.top() == '[') 32 | st.pop(); 33 | 34 | mul /= 3; 35 | } 36 | else{ 37 | if(str[i-1] == '('){ 38 | sol += mul; 39 | } 40 | 41 | if(st.top() == '(') 42 | st.pop(); 43 | 44 | mul /= 2; 45 | } 46 | } 47 | 48 | if(st.empty()) 49 | cout << sol; 50 | else 51 | cout << '0'; 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /baekjoon/1987.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-27. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | char arr[21][21]; 9 | int visit[21][21]; 10 | int alph[26]; 11 | int dx[4] = {1,-1,0,0}; 12 | int dy[4] = {0,0,1,-1}; 13 | 14 | int R,C; 15 | int sol = -1; 16 | void DFS(int a, int b,int c){ 17 | int nx,ny; 18 | for(int i = 0 ; i < 4 ; i++){ 19 | nx = a + dx[i]; 20 | ny = b + dy[i]; 21 | if(nx >= 1 && nx <= C && ny >= 1 && ny <= R && visit[ny][nx] == 0 && alph[(int)arr[ny][nx]-65] == 0){ 22 | visit[ny][nx] = 1; 23 | alph[(int)arr[ny][nx]-65] = 1; 24 | DFS(nx,ny,c+1); 25 | visit[ny][nx] = 0; 26 | alph[(int)arr[ny][nx]-65] = 0; 27 | } 28 | } 29 | if(sol < c) 30 | sol = c; 31 | return; 32 | } 33 | 34 | int main(){ 35 | cin >> R >> C; 36 | 37 | for(int i = 1 ; i <= R ; i++){ 38 | for(int j = 1 ; j <= C ; j++){ 39 | cin >>arr[i][j]; 40 | } 41 | } 42 | visit[1][1] = 1; 43 | alph[(int)arr[1][1]-65] = 1; 44 | DFS(1,1,1); 45 | 46 | cout << sol << endl; 47 | 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /baekjoon/9020.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-12. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main(){ 11 | 12 | int N; 13 | int a; 14 | 15 | int A,B; 16 | int solA,solB; 17 | bool chkA,chkB; 18 | cin >> N; 19 | while(N--){ 20 | cin >> a; 21 | for(int i = 2 ; i <= a/2 ; i++){ 22 | A = i; 23 | B = a - i; 24 | chkA = true; 25 | chkB = true; 26 | for(int j = 2 ; j <=sqrt((double)A);j++){ 27 | if(A % j == 0){ 28 | chkA = false; 29 | break; 30 | } 31 | } 32 | if(!chkA) 33 | continue; 34 | 35 | for(int j = 2 ; j <=sqrt((double)B);j++){ 36 | if(B % j == 0){ 37 | chkB = false; 38 | break; 39 | } 40 | } 41 | if(chkA && chkB){ 42 | solA = A; 43 | solB = B; 44 | } 45 | } 46 | cout << solA << ' ' << solB << '\n'; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /2018-winter-coding/2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int skillChk[26]; 12 | string skill = "CBD"; 13 | vector skill_trees = {"BACDE", "CBADF", "AECB", "BDA"}; 14 | int stck[26]; 15 | int main(){ 16 | 17 | int answer = 0; 18 | bool chk; 19 | int top = -1; 20 | for(int i = skill.size()-1 ; i > -1 ; i--){ 21 | skillChk[skill[i]-'A']++; 22 | stck[++top] = skill[i] -'A'; 23 | } 24 | for(int i = 0 ; i < skill_trees.size() ; i++){ 25 | int ntop = top; 26 | chk = true; 27 | for(int j = 0 ; j < skill_trees[i].size(); j++){ 28 | //스택에 있으면 29 | if(skillChk[skill_trees[i][j] - 'A']){ 30 | if(stck[ntop] == skill_trees[i][j] - 'A'){ 31 | ntop--; 32 | } 33 | else{ 34 | chk = false; 35 | break; 36 | } 37 | } 38 | } 39 | if(chk) 40 | answer++; 41 | } 42 | cout << answer << endl; 43 | return answer; 44 | } 45 | -------------------------------------------------------------------------------- /kakao-2018-1st-codingTest/4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-15. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | vector food_times = {3, 1, 2}; 10 | long long k = 5; 11 | long long total = 0; 12 | 13 | int main(){ 14 | int min = 999999999; 15 | int size = food_times.size(); 16 | for(int i = 0 ; i < size; i++) { 17 | total += food_times[i]; 18 | if(min > food_times[i]) 19 | min = food_times[i]; 20 | } 21 | if(total <= k) 22 | return -1; 23 | 24 | int chknum = min; 25 | k -= min*size; 26 | 27 | 28 | int idx = 0; 29 | while(k--){ 30 | if(food_times[idx] != chknum) 31 | food_times[idx]--; 32 | else 33 | k++; 34 | if(idx == size -1) 35 | idx = 0; 36 | else 37 | idx++; 38 | } 39 | while(1){ 40 | if(food_times[idx] == chknum){ 41 | if(idx == size -1) 42 | idx = 0; 43 | else 44 | idx++; 45 | } 46 | else { 47 | cout << idx +1 << endl; 48 | return idx + 1; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /baekjoon/1018.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-12. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | int N,M; 9 | char map[50][50]; 10 | int sol = 2500; 11 | void simulation(int y, int x){ 12 | //처음이 B로 시작 13 | int cntB = 0; 14 | for(int i = y ; i < y+8 ; i++){ 15 | for(int j = x ; j < x+8 ; j++){ 16 | if((i+j)%2 == 0){ 17 | if(map[i][j] != 'B'){ 18 | cntB++; 19 | } 20 | } 21 | else{ 22 | if(map[i][j] == 'B'){ 23 | cntB++; 24 | } 25 | } 26 | } 27 | } 28 | if(cntB > 64-cntB) 29 | cntB = 64-cntB; 30 | //cout << cntB< cntB) 32 | sol = cntB; 33 | } 34 | int main(){ 35 | cin >> N >> M; 36 | for(int i = 0 ; i < N ; i++) 37 | for(int j = 0 ; j < M ; j++) 38 | cin >> map[i][j]; 39 | 40 | int cntB = 0; 41 | for(int i = 0 ;i <= N-8 ; i++){ 42 | for(int j = 0 ; j <= M-8 ; j++){ 43 | //cout << i<<","< 6 | #include 7 | using namespace std; 8 | 9 | vector v; 10 | int d[11]; 11 | int N; 12 | void print(){ 13 | for(int i = 1 ; i <= N ; i++){ 14 | if(d[i] == 1) 15 | cout << v[i-1]; 16 | } 17 | cout << endl; 18 | } 19 | //현재 인덱스, 현재 크기, 목표 크기 20 | void combination(int idx, int size, int target){ 21 | if(size == target){ 22 | print(); 23 | return; 24 | } 25 | for(int i = idx+1 ; i <= N - target + size + 1 ; i++){ 26 | d[i] = 1; 27 | combination(i,size+1,target); 28 | d[i] = 0; 29 | } 30 | } 31 | void makeCombination(int N){ 32 | //몇개로 조합을 만들지? 1~N개 33 | for(int i = 1; i <= N ; i++){ 34 | //뭐부터 넣을까? >> 뭐로 시작할까? 35 | //i개라면 N - i + 1까지는 시작해도되는데 N - i + 2부터는 안됨 36 | for(int j = 1; j <= N - i + 1 ; j++){ 37 | d[j] = 1; 38 | combination(j,1,i); 39 | d[j] = 0; 40 | } 41 | } 42 | } 43 | int main(){ 44 | N = 4; //1~N까지 배열 45 | //배열 생성 46 | for(int i = 1 ; i <= N ; i++) 47 | v.push_back(i); 48 | 49 | makeCombination(N); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /kakao/secretmap.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-03. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int a1[16][16]; 12 | int a2[16][16]; 13 | vector ans; 14 | 15 | int main() { 16 | int n = 5; 17 | int arr1[5] = {9, 20, 28, 18, 11}; 18 | int arr2[5] = {30, 1, 21, 17, 28}; 19 | int tmp1; 20 | int tmp2; 21 | int idx1; 22 | int idx2; 23 | for (int i = 0; i < n; i++) { 24 | tmp1 = arr1[i]; 25 | idx1 = n - 1; 26 | tmp2 = arr2[i]; 27 | idx2 = n - 1; 28 | while (tmp1) { 29 | a1[i][idx1] = (tmp1 % 2); 30 | tmp1 /= 2; 31 | idx1--; 32 | } 33 | while (tmp2) { 34 | a2[i][idx2] = (tmp2 % 2); 35 | tmp2 /= 2; 36 | idx2--; 37 | } 38 | string str = ""; 39 | for (int j = 0; j < n; j++) { 40 | if(a1[i][j] == 1 || a2[i][j] == 1) 41 | str += "#"; 42 | else 43 | str += " "; 44 | } 45 | ans.push_back(str); 46 | } 47 | 48 | for(int i = 0 ; i < n ; i++) 49 | cout< 2 | using namespace std; 3 | 4 | int N; //숫자의 개수 5 | int num[100]; 6 | int operand[4]; // +-*/ 7 | int minsol = 1000000001; 8 | int maxsol = -1000000001; 9 | 10 | void dfs(int value, int cnt) { 11 | if (cnt == N) { 12 | if (value > maxsol) 13 | maxsol = value; 14 | if (value < minsol) 15 | minsol = value; 16 | return; 17 | } 18 | if (operand[0]) { 19 | operand[0]--; 20 | dfs(value + num[cnt], cnt + 1); 21 | operand[0]++; 22 | } 23 | if (operand[1]) { 24 | operand[1]--; 25 | dfs(value - num[cnt], cnt + 1); 26 | operand[1]++; 27 | } 28 | if (operand[2]) { 29 | operand[2]--; 30 | dfs(value * num[cnt], cnt + 1); 31 | operand[2]++; 32 | } 33 | if (operand[3]) { 34 | operand[3]--; 35 | dfs(value / num[cnt], cnt + 1); 36 | operand[3]++; 37 | } 38 | } 39 | int main() { 40 | cin >> N; 41 | for (int i = 0; i < N; i++) 42 | cin >> num[i]; 43 | 44 | for (int i = 0; i < 4; i++) 45 | cin >> operand[i]; 46 | 47 | dfs(num[0], 1); 48 | cout << maxsol << endl << minsol << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /baekjoon/5014.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-06. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | struct f{ 9 | int floor,d; 10 | }; 11 | queueq; 12 | int visit[1000001]; 13 | int sol = -1; 14 | int F,S,G,U,D; 15 | void bfs(){ 16 | while(!q.empty()){ 17 | f tmp = q.front(); 18 | q.pop(); 19 | int u = tmp.floor + U; 20 | int d = tmp.floor - D; 21 | if(u == G){ 22 | sol = tmp.d +1; 23 | return; 24 | } 25 | if(d == G){ 26 | sol = tmp.d +1; 27 | return; 28 | } 29 | if(u <= F && visit[u] == 0){ 30 | visit[u] = 1; 31 | q.push({u,tmp.d+1}); 32 | } 33 | if(d >=1 && visit[d] == 0){ 34 | visit[d] = 1; 35 | q.push({d,tmp.d+1}); 36 | } 37 | } 38 | } 39 | 40 | int main(){ 41 | cin >> F >> S >> G >> U >> D; 42 | if(S==G){ 43 | cout <<0< 5 | #include 6 | using namespace std; 7 | int arr[101][101]; 8 | int visit[101]; 9 | int sol[101]; 10 | struct f{ 11 | int v; 12 | int d; 13 | }; 14 | queue q; 15 | int N,M; 16 | void bfs(int a){ 17 | while(!q.empty()){ 18 | f tmp = q.front(); 19 | q.pop(); 20 | sol[a] += tmp.d; 21 | for(int i = 1 ; i <= N ; i++){ 22 | if(arr[i][tmp.v] == 1 && visit[i] == 0){ 23 | q.push({i,tmp.d+1}); 24 | visit[i] = 1; 25 | } 26 | } 27 | } 28 | } 29 | int main(){ 30 | cin >> N >> M; 31 | int a,b; 32 | //그래프 생성 33 | for(int i = 0 ; i < M ; i++) { 34 | cin >> a >> b; 35 | arr[a][b] = 1; 36 | arr[b][a] = 1; 37 | } 38 | for(int i = 1 ; i <= N ; i++){ 39 | visit[i] = 1; 40 | q.push({i,0}); 41 | bfs(i); 42 | fill_n(visit,101,0); 43 | } 44 | int m = 9999999; 45 | int idx = -1; 46 | for(int i = 1; i <= N ; i++){ 47 | if(sol[i] < m){ 48 | m = sol[i]; 49 | idx = i; 50 | } 51 | } 52 | cout << idx << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /baekjoon/10845.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | queue q; 12 | string s; 13 | 14 | int N; 15 | cin >> N; 16 | 17 | int a; 18 | while(N--){ 19 | cin >> s; 20 | 21 | if(s == "push"){ 22 | cin >> a; 23 | q.push(a); 24 | continue; 25 | } 26 | 27 | if(s == "pop"){ 28 | if(q.empty()) 29 | cout <<"-1"; 30 | else { 31 | cout << q.front(); 32 | q.pop(); 33 | } 34 | } 35 | 36 | if(s == "size"){ 37 | cout << q.size(); 38 | } 39 | 40 | if(s == "empty"){ 41 | cout << q.empty() ? "1" : "0"; 42 | } 43 | 44 | if(s == "front"){ 45 | if(q.empty()) 46 | cout << "-1"; 47 | else 48 | cout < 6 | using namespace std; 7 | int Max_sol = -1000000001; 8 | int Min_sol = 1000000001; 9 | int N; 10 | int operand[12]; //1~11 11 | int operat[4]; //+,-,*,/ 12 | 13 | void dfs(int d, int v){ 14 | if(d == N){ 15 | if(Max_sol < v) 16 | Max_sol = v; 17 | if(Min_sol > v) 18 | Min_sol = v; 19 | return; 20 | } 21 | 22 | if(operat[0]){ 23 | operat[0]--; 24 | dfs(d+1,v+operand[d+1]); 25 | operat[0]++; 26 | } 27 | 28 | if(operat[1]){ 29 | operat[1]--; 30 | dfs(d+1,v-operand[d+1]); 31 | operat[1]++; 32 | } 33 | 34 | if(operat[2]){ 35 | operat[2]--; 36 | dfs(d+1,v*operand[d+1]); 37 | operat[2]++; 38 | } 39 | 40 | if(operat[3]){ 41 | operat[3]--; 42 | dfs(d+1,v/operand[d+1]); 43 | operat[3]++; 44 | } 45 | } 46 | 47 | int main(){ 48 | cin >> N; 49 | 50 | for(int i = 1 ; i <= N ; i++) 51 | cin >> operand[i]; 52 | for(int i = 0 ; i < 4 ; i++) 53 | cin >> operat[i]; 54 | 55 | dfs(1,operand[1]); 56 | 57 | cout< 5 | using namespace std; 6 | 7 | int num[13]; 8 | int op[5]; 9 | int N; //숫자 개수, 연산자 개수 -> N-1개 10 | int minValue = 100000001; 11 | int maxValue = -100000001; 12 | 13 | 14 | void dfs(int cnt , int value) { 15 | if (cnt == N) { 16 | if (minValue > value) 17 | minValue = value; 18 | if (maxValue < value) 19 | maxValue = value; 20 | return; 21 | } 22 | if (op[1]) { 23 | op[1]--; 24 | dfs(cnt+1,value+num[cnt+1]); 25 | op[1]++; 26 | } 27 | if (op[2]) { 28 | op[2]--; 29 | dfs(cnt+1,value-num[cnt+1]); 30 | op[2]++; 31 | } 32 | if (op[3]) { 33 | op[3]--; 34 | dfs(cnt+1,value*num[cnt+1]); 35 | op[3]++; 36 | } 37 | if (op[4]) { 38 | op[4]--; 39 | dfs(cnt+1,value/num[cnt+1]); 40 | op[4]++; 41 | } 42 | } 43 | 44 | int main() { 45 | int T; 46 | cin >> T; 47 | int c = 1; 48 | while (T--) { 49 | cin >> N; 50 | //연산자 개수 입력 51 | for (int i = 1; i < 5; i++) { 52 | cin >> op[i]; 53 | } 54 | //숫자 입력 55 | for (int i = 1; i <= N; i++) { 56 | cin >> num[i]; 57 | } 58 | 59 | dfs(1,num[1]); 60 | cout <<"#" << c << " " << maxValue - minValue << endl; 61 | minValue = 100000001; 62 | maxValue = -100000001; 63 | c++; 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /baekjoon/1325.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-21. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | vector arr[10001]; 12 | int N,M; 13 | int sol[10001]; 14 | int visit[10001]; 15 | queue q; 16 | int maxv; 17 | void count(int i){ 18 | visit[i] = 1; 19 | for(auto it = arr[i].begin() ; it != arr[i].end() ; it++){ 20 | q.push(*it); 21 | visit[*it] = 1; 22 | } 23 | while(q.size()){ 24 | int t = q.front(); 25 | q.pop(); 26 | sol[i]++; 27 | for(auto it = arr[t].begin() ; it != arr[t].end() ; it++){ 28 | if(visit[*it] == 0) { 29 | visit[*it] = 1; 30 | q.push(*it); 31 | } 32 | } 33 | } 34 | if(maxv < sol[i]) 35 | maxv = sol[i]; 36 | return; 37 | }; 38 | 39 | int main(){ 40 | cin >> N >> M; 41 | int A,B; 42 | while(M--){ 43 | cin >> A >> B; 44 | arr[B].push_back(A); 45 | } 46 | for(int i = 1; i <= N ; i++ ){ 47 | count(i); 48 | fill_n(visit+1,N,0); 49 | } 50 | 51 | for(int i = 1; i <= N ; i++){ 52 | if(sol[i] == maxv) 53 | cout< 6 | #include 7 | using namespace std; 8 | 9 | struct aa{ 10 | int v; 11 | int cnt; 12 | }; 13 | queue q; 14 | int N,K; 15 | int sol; 16 | int front,cnt,a,b,c; 17 | int arr[100001]; 18 | void BFS(){ 19 | while(q.size()){ 20 | front = q.front().v; 21 | cnt = q.front().cnt; 22 | q.pop(); 23 | if(front == K){ 24 | cout << cnt << endl; 25 | return; 26 | } 27 | a = front - 1; 28 | b = front + 1; 29 | c = front * 2; 30 | if(a >= 0 && arr[a] == 0){ 31 | arr[a] = cnt + 1; 32 | q.push({a,cnt+1}); 33 | } 34 | 35 | if(b <= 100000 && arr[b] == 0){ 36 | arr[b] = cnt + 1; 37 | q.push({b,cnt+1}); 38 | } 39 | 40 | if(c <= 100000 && arr[c] == 0){ 41 | arr[c] = cnt + 1; 42 | q.push({c,cnt+1}); 43 | } 44 | 45 | } 46 | } 47 | 48 | int main(){ 49 | cin >> N >> K; 50 | if(N > K) 51 | cout << N-K< 5 | #include 6 | using namespace std; 7 | int arr[51]; 8 | int main(){ 9 | deque dq; 10 | int N,M; 11 | int sol = 0; 12 | cin >> N >> M; 13 | for(int i = 1 ; i <= N ; i++) 14 | dq.push_back(i); 15 | 16 | for(int i = 1; i <= M; i++) 17 | cin >> arr[i]; 18 | 19 | //답찾기 20 | for(int i = 1; i <=M; i++){ 21 | int cnt = 0; 22 | int tmp; 23 | for( auto it = dq.begin(); *it != arr[i]; it++){ 24 | cnt++; 25 | } 26 | 27 | //front가 유리 28 | if(cnt < dq.size() - cnt){ 29 | for(int i = 1 ; i <= cnt ; i++){ 30 | tmp = dq.front(); 31 | dq.pop_front(); 32 | dq.push_back(tmp); 33 | } 34 | 35 | sol += cnt; 36 | dq.pop_front(); 37 | } 38 | //back이 유리 39 | else{ 40 | for(int i = 1 ; i <= dq.size() - cnt ; i++){ 41 | tmp = dq.back(); 42 | dq.pop_back(); 43 | dq.push_front(tmp); 44 | } 45 | 46 | sol += (dq.size() - cnt); 47 | dq.pop_front(); 48 | } 49 | } 50 | 51 | cout << sol; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /baekjoon/11403.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-26. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | vector arr[101]; 11 | queue q; 12 | int N; 13 | int visit[101][101]; 14 | int main(){ 15 | cin >> N; 16 | 17 | int a; 18 | for(int i = 1; i <= N ; i++){ 19 | for(int j = 1; j <= N ; j++){ 20 | cin >> a; 21 | if(a){ 22 | arr[i].push_back(j); 23 | } 24 | } 25 | } 26 | 27 | for(int i = 1; i <= N ; i++) { 28 | q.push(i); 29 | while(q.size()){ 30 | int tmp = q.front(); 31 | q.pop(); 32 | for(auto it = arr[tmp].begin() ; it != arr[tmp].end(); it++){ 33 | if(visit[i][*it] == 0){ 34 | visit[i][*it] = 1; 35 | q.push(*it); 36 | } 37 | } 38 | } 39 | } 40 | 41 | for(int i = 1; i <= N ; i++) { 42 | for (int j = 1; j <= N; j++) { 43 | cout << visit[i][j] << " "; 44 | } 45 | cout < 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector v[101002]; 10 | int visit[101002]; 11 | struct xx{ 12 | int n,c; 13 | }; 14 | queue q; 15 | int N,M,K; 16 | int sol = -1; 17 | void bfs(){ 18 | q.push({1,1}); 19 | visit[1] = 1; 20 | xx front; 21 | while(!q.empty()){ 22 | front = q.front(); 23 | q.pop(); 24 | for(auto a : v[front.n]){ 25 | if(a == N){ 26 | sol = front.c + 1; 27 | return; 28 | } 29 | if(visit[a] == 0){ 30 | visit[a] = 1; 31 | if(a>N) 32 | q.push({a,front.c}); 33 | else 34 | q.push({a, front.c + 1}); 35 | 36 | } 37 | } 38 | } 39 | } 40 | 41 | int main(){ 42 | ios_base::sync_with_stdio(false); 43 | cin >> N >> K >> M; 44 | 45 | //int a,b,c; 46 | //그래프 생성 47 | int t = N+1; 48 | int a; 49 | while(M--){ 50 | for(int i = 1; i <= K ; i++) { 51 | cin >> a; 52 | v[t].push_back(a); 53 | v[a].push_back(t); 54 | } 55 | t++; 56 | } 57 | //계산 58 | bfs(); 59 | cout << sol << endl; 60 | return 0; 61 | } -------------------------------------------------------------------------------- /kakao-2018-1st-codingTest/2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-15. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int solmap[502][2]; 12 | double failure[501]; 13 | vector,int>> f; 14 | int N = 5; 15 | vector stages = {2, 1, 2, 6, 2, 4, 3, 3}; 16 | vector answer; 17 | 18 | bool compare(pair,int> a,pair,int> b){ 19 | if(a.first.first*b.first.second == b.first.first*a.first.second) 20 | return a.second < b.second; 21 | return a.first.first*b.first.second > b.first.first*a.first.second; 22 | } 23 | int main() { 24 | for (int i = 0; i < stages.size(); i++) { 25 | for (int j = 1; j < stages[i]; j++) { 26 | solmap[j][0]++; 27 | solmap[j][1]++; 28 | } 29 | solmap[stages[i]][1]++; 30 | } 31 | 32 | for(int i = 1 ; i <= N ; i++){ 33 | f.push_back(make_pair(make_pair(solmap[i][1]- solmap[i][0],solmap[i][1]),i)); 34 | } 35 | 36 | stable_sort(f.begin(),f.end(),compare); 37 | for(int i = 0 ; i < f.size() ; i++){ 38 | answer.push_back(f[i].second); 39 | } 40 | cout << "++++++++++++++++++++"< 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int T, N, K; 14 | vector v; 15 | set> s; 16 | 17 | void simulation(int idx) { 18 | int a, sum; 19 | for (int i = idx; i < v.size() - N / 4 + 1; i += N / 4) { 20 | a = 1; 21 | sum = 0; 22 | for (int j = i + N / 4 - 1; j >= i; j--) { 23 | sum += a * v[j]; 24 | a *= 16; 25 | } 26 | s.insert(sum); 27 | } 28 | } 29 | 30 | int main() { 31 | //'0' == 48, 'A' == 65 32 | cin >> T; 33 | int a = 1; 34 | char c; 35 | while (T--) { 36 | cin >> N >> K; 37 | for (int i = 0; i < N; i++) { 38 | cin >> c; 39 | if (c >= '0' && c <= '9') 40 | v.push_back(c - '0'); 41 | else 42 | v.push_back(c - 'A' + 10); 43 | } 44 | for (int i = 0; i < N / 4; i++) { 45 | simulation(i); 46 | v.push_back(v[i]); 47 | } 48 | auto it = s.begin(); 49 | while (K--) 50 | it++; 51 | cout << "#" << a++ << " " << *(--it) << endl; 52 | v.clear(); 53 | s.clear(); 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /kakao/cache.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-03. 3 | // 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int main(){ 9 | 10 | int cacheSize = 2; 11 | string cities[4] = {"Jeju", "Jeju","s","S"}; 12 | int citiesSize = 4; 13 | vector cache; 14 | int curCacheSize; 15 | bool chk = false; 16 | int answer = 0; 17 | for(int i = 0 ; i < citiesSize; i++){ 18 | curCacheSize = cache.size(); 19 | chk = false; 20 | for(int k = 0 ; k < cities[i].size(); k++){ 21 | if(cities[i][k] >= 97 && (int)cities[i][k] <= 122){ 22 | cities[i][k] = (char)(int)cities[i][k] - 32; 23 | } 24 | } 25 | for(int j = 0 ; j 6 | 7 | using namespace std; 8 | int N; 9 | int number[100]; 10 | int operand[4]; //+-*/ 11 | int maxsol = -1000000000; 12 | int minsol = 1000000000; 13 | void dfs(int cnt, int val){ 14 | if(cnt == N - 1){ 15 | if(maxsol < val) 16 | maxsol = val; 17 | if(minsol > val) 18 | minsol = val; 19 | return; 20 | } 21 | if(operand[0]){ 22 | operand[0]--; 23 | dfs(cnt+1,val+number[cnt+1]); 24 | operand[0]++; 25 | } 26 | if(operand[1]){ 27 | operand[1]--; 28 | dfs(cnt+1,val-number[cnt+1]); 29 | operand[1]++; 30 | } 31 | if(operand[2]){ 32 | operand[2]--; 33 | dfs(cnt+1,val*number[cnt+1]); 34 | operand[2]++; 35 | } 36 | if(operand[3]){ 37 | operand[3]--; 38 | if(val < 0 && number[cnt+1] > 0){ 39 | dfs(cnt+1,((-1*val)/number[cnt+1])*-1); 40 | } 41 | else 42 | dfs(cnt+1,val/number[cnt+1]); 43 | operand[3]++; 44 | } 45 | } 46 | 47 | int main(){ 48 | cin >> N; 49 | for(int i = 0 ; i < N ; i++) 50 | cin >> number[i]; 51 | for(int i = 0 ; i < 4 ; i++) 52 | cin >> operand[i]; 53 | 54 | dfs(0,number[0]); 55 | cout << maxsol << endl; 56 | cout << minsol << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /baekjoon/1012.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-26. 3 | // 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int arr[50][50]; 10 | int dx[4] = {0,0,1,-1}; 11 | int dy[4] = {1,-1,0,0}; 12 | int T,M,N,K; 13 | int x,y; 14 | int sol; 15 | queue> q; 16 | void BFS(){ 17 | while(q.size()){ 18 | int y = q.front().first; 19 | int x = q.front().second; 20 | arr[y][x] = 0; 21 | q.pop(); 22 | for(int i = 0 ; i < 4 ; i++){ 23 | int nx = x + dx[i]; 24 | int ny = y + dy[i]; 25 | if(nx >= 0 && nx < M && ny >= 0 && ny < N && arr[ny][nx] == 1){ 26 | arr[ny][nx] = 0; 27 | q.push({ny,nx}); 28 | } 29 | } 30 | } 31 | }; 32 | 33 | int main(){ 34 | cin >> T; 35 | while(T--){ 36 | fill_n(&arr[0][0],50*50,0); 37 | sol = 0; 38 | cin >> M >> N >> K; 39 | 40 | while(K--){ 41 | cin >> x >> y; 42 | arr[y][x] = 1; 43 | } 44 | for(int i = 0 ; i < N ; i++){ 45 | for(int j = 0 ; j < M ; j++){ 46 | if(arr[i][j] == 1){ 47 | sol++; 48 | arr[i][j] = 0; 49 | q.push({i,j}); 50 | BFS(); 51 | } 52 | } 53 | } 54 | cout << sol << endl; 55 | } 56 | return 0; 57 | } -------------------------------------------------------------------------------- /baekjoon/9466.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-08. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | int arr[100001]; 8 | int visit[100001]; 9 | int sol = 0; 10 | bool dfs(int current,int target,int cnt){ 11 | visit[current] = 1; 12 | 13 | if(current == target){ 14 | sol += (cnt+1); 15 | return true; 16 | } 17 | 18 | if(visit[arr[current]] == 0){ 19 | if(dfs(arr[current],target,cnt+1)){ 20 | return true; 21 | } 22 | else{ 23 | visit[current] = 0; 24 | return false; 25 | } 26 | } 27 | else { 28 | visit[current] = 0; 29 | return false; 30 | } 31 | } 32 | int main(){ 33 | ios_base::sync_with_stdio(false); 34 | cin.tie(NULL); 35 | int T; 36 | cin >> T; 37 | int N; 38 | while(T--){ 39 | cin >> N; 40 | for(int i = 1; i <= N ; i++) { 41 | cin >> arr[i]; 42 | if(arr[i] == i){ 43 | sol++; 44 | visit[i] = 1; 45 | } 46 | } 47 | for(int i = 1 ; i <= N ; i++){ 48 | if(!visit[i] &&!visit[arr[i]]) { 49 | if(!dfs(arr[i], i, 0)){ 50 | visit[i] = 1; 51 | } 52 | } 53 | } 54 | 55 | cout << N - sol << '\n'; 56 | sol = 0 ; 57 | fill_n(visit,N+1,0); 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /baekjoon/2667.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-20. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int N; 9 | int arr[26][26]; 10 | int visit[26][26]; 11 | int sol[626]; 12 | int cnt; 13 | int dx[4] = {0,0,1,-1}; 14 | int dy[4] = {-1,1,0,0}; 15 | void DFS(int x, int y){ 16 | int nx,ny; 17 | for(int i = 0 ; i <= 3 ; i++){ 18 | nx = x + dx[i]; 19 | ny = y + dy[i]; 20 | if(nx > 0 && nx <= N && ny > 0 && ny <= N && visit[nx][ny] == 0 && arr[nx][ny] == 1){ 21 | sol[cnt]++; 22 | visit[nx][ny] = 1; 23 | DFS(nx,ny); 24 | } 25 | } 26 | }; 27 | 28 | int main(){ 29 | cin >> N; 30 | char a; 31 | for(int i = 1; i <= N ; i++){ 32 | for(int j = 1; j <= N ; j++) { 33 | cin >> a; 34 | if(a == '0') 35 | arr[i][j] = 0; 36 | else 37 | arr[i][j] = 1; 38 | } 39 | } 40 | for(int i = 1; i <= N ; i++) { 41 | for (int j = 1; j <= N; j++) { 42 | if(visit[i][j] == 0 && arr[i][j] == 1){ 43 | cnt++; 44 | visit[i][j] = 1; 45 | sol[cnt]++; 46 | DFS(i,j); 47 | } 48 | } 49 | } 50 | if(cnt) 51 | sort(&sol[1],&sol[cnt+1]); 52 | 53 | cout << cnt < 6 | #include 7 | using namespace std; 8 | int arr[8001]; 9 | int main(){ 10 | int N; 11 | cin >> N; 12 | int a; 13 | int sum = 0; 14 | int max = -4001; 15 | int min = 4001; 16 | //평균, 범위 17 | for(int i = 0 ; i < N ; i++){ 18 | cin >> a; 19 | arr[a+4000]++; 20 | sum += a; 21 | if(max < a) 22 | max = a; 23 | if(min > a) 24 | min = a; 25 | } 26 | //최빈값 27 | int most = 0; 28 | bool second = false; 29 | for(int i = 1 ; i <= 8000 ; i++){ 30 | if(arr[most] == arr[i]){ 31 | if(!second){ 32 | most = i; 33 | second = true; 34 | } 35 | } 36 | if(arr[most] < arr[i]){ 37 | most = i; 38 | second = false; 39 | } 40 | } 41 | 42 | //중앙값 43 | int mid; 44 | if(arr[0] >=((N/2)+1)) 45 | mid = 0; 46 | else { 47 | for (int i = 1; i <= 8000; i++) { 48 | arr[i] += arr[i - 1]; 49 | if (arr[i] >= ((N / 2) + 1)) { 50 | mid = i; 51 | break; 52 | } 53 | } 54 | } 55 | 56 | cout < 6 | #include 7 | using namespace std; 8 | int visit[300][300]; 9 | int T,L; 10 | int sx,ex,sy,ey; 11 | int dx[8] = {2,2,1,1,-1,-1,-2,-2}; 12 | int dy[8] = {1,-1,2,-2,2,-2,1,-1}; 13 | struct xy{ 14 | int x,y,cnt; 15 | }; 16 | queue q; 17 | int sol; 18 | void BFS(){ 19 | while(q.size()){ 20 | int x = q.front().x; 21 | int y = q.front().y; 22 | int cnt = q.front().cnt; 23 | q.pop(); 24 | for(int i = 0; i < 8 ; i++){ 25 | int nx = x + dx[i]; 26 | int ny = y + dy[i]; 27 | if(nx == ex && ny == ey){ 28 | sol = cnt + 1; 29 | return; 30 | } 31 | if(nx >= 0 && nx < L && ny >= 0 && ny < L && visit[ny][nx] == 0){ 32 | visit[ny][nx] = 1; 33 | q.push({nx,ny,cnt+1}); 34 | } 35 | 36 | } 37 | 38 | } 39 | return; 40 | } 41 | 42 | int main(){ 43 | cin >> T; 44 | while(T--){ 45 | sol = 0; 46 | while(q.size()){ 47 | q.pop(); 48 | } 49 | fill_n(&visit[0][0],300*300,0); 50 | cin >> L >> sx >> sy >> ex >> ey; 51 | if(sx == ex && sy == ey){ 52 | cout << sol<< endl; 53 | continue; 54 | } 55 | visit[sy][sx] = 1; 56 | q.push({sx,sy,0}); 57 | BFS(); 58 | cout << sol << endl; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /baekjoon/14889_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-24. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | int N; 10 | int s[21][21]; 11 | int visit[21]; 12 | int sol = 99999999; 13 | 14 | void calc() { 15 | vector t1; 16 | vector t2; 17 | for (int i = 1; i <= N; i++) { 18 | if (visit[i]) 19 | t1.push_back(i); 20 | else 21 | t2.push_back(i); 22 | } 23 | int s1 = 0; 24 | for (int i = 0; i < N / 2 - 1; i++) { 25 | for (int j = i + 1; j <= N / 2 - 1; j++) { 26 | s1 += s[t1[i]][t1[j]]; 27 | s1 += s[t1[j]][t1[i]]; 28 | s1 -= s[t2[i]][t2[j]]; 29 | s1 -= s[t2[j]][t2[i]]; 30 | } 31 | } 32 | if (sol > abs(s1)) 33 | sol = abs(s1); 34 | } 35 | 36 | void dfs(int current, int cnt) { 37 | if (cnt == N / 2) { 38 | //계산! 39 | calc(); 40 | // cout << "calc" << endl; 41 | return; 42 | } 43 | for (int i = current + 1; i <= N / 2 + cnt + 1; i++) { 44 | visit[i] = 1; 45 | dfs(i, cnt + 1); 46 | visit[i] = 0; 47 | } 48 | } 49 | 50 | int main() { 51 | cin >> N; 52 | for (int i = 1; i <= N; i++) { 53 | for (int j = 1; j <= N; j++) 54 | cin >> s[i][j]; 55 | } 56 | for (int i = 1; i <= N / 2 + 1; i++) { 57 | visit[i] = 1; 58 | dfs(i, 1); 59 | visit[i] = 0; 60 | } 61 | cout << sol << endl; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /baekjoon/1966.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-13. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | int main(){ 12 | int T; 13 | cin >> T; 14 | int N, M; 15 | int a; 16 | while(T--){ 17 | queue q; 18 | priority_queue pq; 19 | cin >> N >> M; 20 | //int max = -1; 21 | int sol = 0; 22 | for(int i = 0; i < N ; i++){ 23 | cin >> a; 24 | q.push(a); 25 | pq.push(a); 26 | } 27 | int tmp; 28 | int idx = M; 29 | while(1) { 30 | //대상물 전까지 push, pop 31 | for (int i = 0; i < idx; i++) { 32 | if (q.front() == pq.top()) { 33 | q.pop(); 34 | pq.pop(); 35 | sol++; 36 | } 37 | else { 38 | tmp = q.front(); 39 | q.pop(); 40 | q.push(tmp); 41 | } 42 | } 43 | //대상물 push or pop 44 | if (q.front() == pq.top()) { 45 | q.pop(); 46 | pq.pop(); 47 | sol++; 48 | cout < 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int map[11][11]; //맵 12 | int road[11][11][11][11]; //from_y,from_x, to_y, to_x 13 | int answer = 0; 14 | string dirs = "LULLLLLLU"; 15 | //U(y++),D(y--),R(x++),L(x--) 16 | int dx[4] = {0, 0, 1, -1}; 17 | int dy[4] = {1, -1, 0, 0}; 18 | 19 | void markRoad(int sy, int sx, int ny, int nx) { 20 | road[sy][sx][ny][nx] = 1; 21 | road[ny][nx][sy][sx] = 1; 22 | } 23 | 24 | bool chkRoad(int sy, int sx, int ny, int nx) { 25 | if (!road[sy][sx][ny][nx] && !road[ny][nx][sy][sx]) { 26 | return true; 27 | } 28 | return false; 29 | } 30 | 31 | bool chkBound(int y, int x) { 32 | if (x >= 0 && x < 11 && y >= 0 && y < 11) 33 | return true; 34 | return false; 35 | } 36 | 37 | int main() { 38 | //시작점 39 | int sx = 5; 40 | int sy = 5; 41 | int nx, ny; 42 | int d; 43 | for (int i = 0; i < dirs.size(); i++) { 44 | if (dirs[i] == 'U') d = 0; 45 | else if (dirs[i] == 'D') d = 1; 46 | else if (dirs[i] == 'R') d = 2; 47 | else d = 3; 48 | nx = sx + dx[d]; 49 | ny = sy + dy[d]; 50 | if (chkBound(ny, nx)) { 51 | if (chkRoad(sy, sx, ny, nx)) { 52 | answer++; 53 | markRoad(sy, sx, ny, nx); 54 | } 55 | sx = nx; 56 | sy = ny; 57 | } 58 | } 59 | cout << answer << endl; 60 | } -------------------------------------------------------------------------------- /baekjoon/2589.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-06. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | int dx[4] = {1,-1,0,0}; 9 | int dy[4] = {0,0,1,-1}; 10 | struct xy{ 11 | int x,y; 12 | int d; 13 | }; 14 | queue q; 15 | int map[51][51]; 16 | int visit[51][51]; 17 | int sol; 18 | int N,M; 19 | 20 | void bfs(){ 21 | int nx,ny; 22 | while(!q.empty()){ 23 | xy tmp = q.front(); 24 | q.pop(); 25 | for(int i = 0 ; i < 4 ; i++){ 26 | nx = tmp.x + dx[i]; 27 | ny = tmp.y + dy[i]; 28 | if(map[ny][nx] == 1 && nx >= 1 && nx <= M && ny >= 1 && ny <= N && visit[ny][nx] ==0){ 29 | visit[ny][nx] = 1; 30 | q.push({nx,ny,tmp.d+1}); 31 | if(sol < tmp.d+1) 32 | sol = tmp.d+1; 33 | } 34 | } 35 | } 36 | 37 | } 38 | 39 | int main(){ 40 | cin >> N >> M; 41 | char a; 42 | for(int i = 1 ; i <= N ; i++){ 43 | for(int j = 1; j <= M ;j++){ 44 | cin >> a; 45 | if(a == 'W') 46 | map[i][j] = 0; 47 | else 48 | map[i][j] = 1; 49 | } 50 | } 51 | 52 | for(int i = 1; i <= N ; i++){ 53 | for(int j = 1; j <= M ; j++){ 54 | if(map[i][j] == 1){ 55 | visit[i][j] = 1; 56 | q.push({j,i,0}); 57 | bfs(); 58 | fill_n(&visit[0][0],51*51,0); 59 | } 60 | } 61 | } 62 | cout << sol; 63 | return 0; 64 | } -------------------------------------------------------------------------------- /baekjoon/1707.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | int T,V,E; 11 | int visit[20001]; 12 | vector arr[20001]; 13 | 14 | 15 | void DFS(int a,bool check){ 16 | for(auto it = arr[a].begin(); it != arr[a].end(); it++){ 17 | if(visit[*it] == 0){ 18 | if(check) 19 | visit[*it] = 1; 20 | else 21 | visit[*it] = 2; 22 | 23 | DFS(*it,!check); 24 | } 25 | } 26 | } 27 | 28 | 29 | int main(){ 30 | cin >> T; 31 | int a,b; 32 | while(T--) { 33 | fill_n(visit, 20001, 0); 34 | for (int i = 1; i <= V; i++) 35 | arr[i].clear(); 36 | cin >> V >> E; 37 | while (E--) { 38 | cin >> a >> b; 39 | arr[a].push_back(b); 40 | arr[b].push_back(a); 41 | } 42 | bool check = true; 43 | for (int i = 1; i <= V; i++) { 44 | if (visit[i] == 0) { 45 | visit[i] = 1; 46 | DFS(i,!check); 47 | } 48 | } 49 | bool sol = true; 50 | for (int i = 1; i <= V; i++){ 51 | for(auto it = arr[i].begin(); it != arr[i].end(); it++){ 52 | if(visit[i] == visit[*it]){ 53 | sol = false; 54 | } 55 | } 56 | } 57 | 58 | if(sol) 59 | cout << "YES" << endl; 60 | else 61 | cout << "NO" << endl; 62 | } 63 | return 0; 64 | } -------------------------------------------------------------------------------- /kakao/news_clustering.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-05. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int arr1[26][26]; 9 | int arr2[26][26]; 10 | 11 | string toupper(string str){ 12 | int size = str.size(); 13 | string temp = str; 14 | for(int i = 0 ; i < size ; i++){ 15 | if(str[i] >= 'a' && str[i] <= 'z'){ 16 | temp[i] = str[i] - 32; 17 | } 18 | } 19 | return temp; 20 | } 21 | 22 | int main(){ 23 | string str1 = "FRANCE"; 24 | string str2 = "french"; 25 | 26 | str1 = toupper(str1); 27 | str2 = toupper(str2); 28 | 29 | int size1 = str1.size(); 30 | int size2 = str2.size(); 31 | 32 | for(int i = 0 ; i < size1 - 1 ; i++){ 33 | //유효하면~ 34 | if(str1[i] >= 'A' && str1[i] <= 'Z' && str1[i+1] >= 'A' && str1[i+1] <= 'Z'){ 35 | arr1[(str1[i]-65)][(str1[i+1]-65)]++; 36 | } 37 | } 38 | 39 | for(int i = 0 ; i < size2 - 1 ; i++){ 40 | //유효하면~ 41 | if(str2[i] >= 'A' && str2[i] <= 'Z' && str2[i+1] >= 'A' && str2[i+1] <= 'Z'){ 42 | arr2[(str2[i]-65)][(str2[i+1]-65)]++; 43 | } 44 | } 45 | int union_num = 0; 46 | int intersection_num =0; 47 | for(int i = 0 ; i < 26 ; i++){ 48 | for(int j = 0 ; j < 26 ; j++){ 49 | union_num += max(arr1[i][j],arr2[i][j]); 50 | intersection_num += min(arr1[i][j],arr2[i][j]); 51 | } 52 | } 53 | 54 | double solution = ((double)intersection_num/(double)union_num)*65536; 55 | cout << solution << endl; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /baekjoon/2178.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-20. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | struct xy{ 9 | int x = 0; 10 | int y = 0; 11 | }; 12 | int arr[101][101]; 13 | int visit[101][101]; 14 | queue q; 15 | int dx[4] = {0,0,1,-1}; 16 | int dy[4] = {-1,1,0,0}; 17 | int N,M; 18 | int sol = 10001; 19 | void BFS(int cnt){ 20 | int n = q.size(); 21 | for(int i = 1; i <= n ; i++) { 22 | xy t = q.front(); 23 | q.pop(); 24 | for (int i = 0; i <= 3; i++) { 25 | int nx = t.x + dx[i]; 26 | int ny = t.y + dy[i]; 27 | if (nx == N && ny == M) { 28 | if (sol > cnt + 1) { 29 | sol = cnt + 1; 30 | return; 31 | } 32 | } 33 | if (nx > 0 && nx <= N && ny > 0 && ny <= M && arr[nx][ny] == 1 && visit[nx][ny] == 0) { 34 | visit[nx][ny] = 1; 35 | xy temp; 36 | temp.x = nx; 37 | temp.y = ny; 38 | q.push(temp); 39 | } 40 | } 41 | } 42 | BFS(cnt+1); 43 | }; 44 | 45 | int main(){ 46 | cin >> N >> M; 47 | char a; 48 | for(int i = 1 ; i <= N ; i++){ 49 | for(int j = 1 ; j <= M ; j++){ 50 | cin >> a; 51 | if(a == '1') 52 | arr[i][j] = 1; 53 | else 54 | arr[i][j] = 0; 55 | } 56 | } 57 | visit[1][1] = 1; 58 | xy t; 59 | t.x = 1; 60 | t.y = 1; 61 | q.push(t); 62 | BFS(1); 63 | cout << sol < 6 | 7 | using namespace std; 8 | 9 | int map[50][50]; 10 | int N, M, sy, sx, d; 11 | int sol; 12 | int dx[4] = {0, 1, 0, -1}; 13 | int dy[4] = {-1, 0, 1, 0}; 14 | 15 | void clean() { 16 | int ny,nx,nd; 17 | nd = d; 18 | bool chk = false; 19 | //4방향 탐색 20 | for(int i = 0 ;i < 4; i++){ 21 | //왼쪽으로 회전 22 | if(nd == 0) 23 | nd = 3; 24 | else 25 | nd--; 26 | //회전하고 전방에 있는 칸 검사 27 | nx = sx + dx[nd]; 28 | ny = sy + dy[nd]; 29 | if(nx >= 0 && nx < M && ny >= 0 && ny < N && map[ny][nx] == 0){ 30 | map[ny][nx] = 3; 31 | sol++; 32 | sx = nx; 33 | sy = ny; 34 | d = nd; 35 | chk = true; 36 | break; 37 | } 38 | } 39 | //4방향이 다 청소되었거나 벽인 경우 40 | if(!chk){ 41 | nd = (d+2)%4; 42 | nx = sx + dx[nd]; 43 | ny = sy + dy[nd]; 44 | //한칸 뒤로 후진, 후진할 수 없으면 정지 45 | if(nx >= 0 && nx < M && ny >= 0 && ny < N && map[ny][nx] != 1){ 46 | sx = nx; 47 | sy = ny; 48 | clean(); 49 | } 50 | else{ 51 | return; 52 | } 53 | } 54 | //4방향중 청소할곳이 있으면 그곳으로 이동후 다시 탐색 55 | if(chk) 56 | clean(); 57 | } 58 | 59 | int main() { 60 | cin >> N >> M >> sy >> sx >> d; 61 | for (int i = 0; i < N; i++) 62 | for (int j = 0; j < M; j++) 63 | cin >> map[i][j]; 64 | 65 | map[sy][sx] = 3; 66 | sol++; 67 | clean(); 68 | cout << sol << endl; 69 | return 0; 70 | } -------------------------------------------------------------------------------- /baekjoon/2468.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int arr[101][101]; 11 | int visit[101][101]; 12 | struct xy{ 13 | int x,y; 14 | }; 15 | queue q; 16 | set s; 17 | int dx[4] = {1,-1,0,0}; 18 | int dy[4] = {0,0,1,-1}; 19 | int N; 20 | int sol = -1; 21 | int cnt; 22 | void BFS(int a){ 23 | while(q.size()){ 24 | int x = q.front().x; 25 | int y = q.front().y; 26 | q.pop(); 27 | for(int i = 0 ; i < 4 ; i++){ 28 | int nx = x + dx[i]; 29 | int ny = y + dy[i]; 30 | if(nx > 0 && nx <= N && ny > 0 && ny <= N && visit[nx][ny] == 0 && arr[nx][ny] >= a){ 31 | visit[nx][ny] = 1; 32 | q.push({nx,ny}); 33 | } 34 | } 35 | } 36 | } 37 | 38 | int main(){ 39 | cin >> N; 40 | for(int i = 1; i <= N ; i++){ 41 | for(int j = 1; j <= N ; j++){ 42 | cin >> arr[i][j]; 43 | s.insert(arr[i][j]); 44 | } 45 | } 46 | 47 | for(auto it = s.begin(); it != s.end(); it++){ 48 | fill_n(&visit[0][0],101*101,0); 49 | cnt = 0; 50 | for(int i = 1; i <= N ; i++){ 51 | for(int j = 1; j <= N ; j++){ 52 | if(visit[i][j] == 0 && arr[i][j] >= *it){ 53 | cnt++; 54 | visit[i][j] = 1; 55 | q.push({i,j}); 56 | BFS(*it); 57 | } 58 | } 59 | } 60 | if(cnt > sol) 61 | sol = cnt; 62 | } 63 | 64 | cout << sol << endl; 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /baekjoon/7576.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-16. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int dx[4] = {0,1,0,-1}; 10 | int dy[4] = {1,0,-1,0}; 11 | int arr[1001][1001]; 12 | int M,N; 13 | 14 | struct xy{ 15 | int x; 16 | int y; 17 | }; 18 | 19 | queue q; 20 | 21 | void BFS(int cnt){ 22 | int t = q.size(); 23 | xy tmp; 24 | xy tmp1; 25 | while(t--){ 26 | tmp = q.front(); 27 | q.pop(); 28 | for(int i = 0; i <= 3; i++){ 29 | int x = tmp.x + dx[i]; 30 | int y = tmp.y + dy[i]; 31 | if(x >= 1 && x <= N && y >= 1 && y <= M){ 32 | if(arr[x][y] == 0){ 33 | arr[x][y] = 1; 34 | tmp1.x = x; 35 | tmp1.y = y; 36 | q.push(tmp1); 37 | } 38 | } 39 | } 40 | } 41 | 42 | if(q.empty()){ 43 | for(int i = 1; i <= N ; i++){ 44 | for(int j = 1; j <= M ; j++){ 45 | if(arr[i][j] == 0){ 46 | cout <<"-1"<> M >> N; 61 | for(int i = 1 ; i <= N ; i++){ 62 | for(int j = 1 ; j <= M ; j++){ 63 | cin >> arr[i][j]; 64 | if(arr[i][j] == 1){ 65 | tmp.x = i; 66 | tmp.y = j; 67 | q.push(tmp); 68 | } 69 | } 70 | } 71 | BFS(0); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /2018-winter-coding/3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-27. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | int cookieSum[2000][2000]; 8 | vector cookie = {1,2}; 9 | int answer = 0; 10 | void calc(int l,int m, int r){ 11 | int first = cookieSum[l][m]; 12 | int second = cookieSum[m+1][r]; 13 | } 14 | int main() { 15 | int answer = 0; 16 | int size = cookie.size(); 17 | if(cookie.size() == 1) { 18 | cout <<"answer:"<<0< 6 | using namespace std; 7 | int N; 8 | int sol = 99999999; 9 | int s[20][20]; 10 | int com[20]; 11 | int t1[10]; 12 | int t2[10]; 13 | void calc() { 14 | int cnt1 = 0; 15 | int cnt2 = 0; 16 | int t1_s = 0; 17 | for (int i = 0; i < N; i++) { 18 | if (!com[i]){ 19 | t1[cnt1] = i; 20 | cnt1++; 21 | } 22 | else { 23 | t2[cnt2] = i; 24 | cnt2++; 25 | } 26 | } 27 | for (int i = 0; i < N / 2 - 1; i++) { 28 | for (int j = i + 1; j < N / 2; j++) { 29 | t1_s += s[t1[i]][t1[j]]; 30 | t1_s += s[t1[j]][t1[i]]; 31 | t1_s -= s[t2[i]][t2[j]]; 32 | t1_s -= s[t2[j]][t2[i]]; 33 | } 34 | } 35 | t1_s = abs(t1_s); 36 | if (sol > t1_s) 37 | sol = t1_s; 38 | } 39 | 40 | void dfs(int cur, int cnt) { 41 | if (cnt == N / 2) { 42 | /*cout << "**********************" << endl; 43 | for (int i = 0; i < N; i++) { 44 | cout << com[i] << " "; 45 | } 46 | cout << endl;*/ 47 | calc(); 48 | return; 49 | } 50 | for (int i = cur + 1; i < N / 2 + cnt + 1; i++) { 51 | com[i] = 1; 52 | dfs(i, cnt + 1); 53 | com[i] = 0; 54 | } 55 | } 56 | void makeTeam() { 57 | com[0] = 1; 58 | dfs(0, 1); 59 | } 60 | int main() { 61 | ios_base::sync_with_stdio(false); 62 | cin.tie(NULL); 63 | cin >> N; 64 | for (int i = 0; i < N; i++) { 65 | for (int j = 0; j < N; j++) { 66 | cin >> s[i][j]; 67 | } 68 | } 69 | makeTeam(); 70 | cout << sol << '\n'; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /kakao/compression.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-11. 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | string msg = "ABABABABABABABAB"; 11 | 12 | int main(){ 13 | map m; 14 | m["A"] = 1;m["B"] = 2;m["C"] = 3;m["D"] = 4;m["E"] = 5;m["F"] = 6;m["G"] = 7;m["H"] = 8;m["I"] = 9;m["J"] = 10;m["K"] = 11;m["L"] = 12;m["M"] = 13;m["N"] = 14;m["O"] = 15;m["P"] = 16; 15 | m["Q"] = 17;m["R"] = 18;m["S"] = 19;m["T"] = 20;m["U"] = 21;m["V"] = 22;m["W"] = 23;m["X"] = 24;m["Y"] = 25;m["Z"] = 26; 16 | vector answer; 17 | int size = m.size(); 18 | int ss = msg.size(); 19 | string cur = ""; 20 | for(int i = 0 ; i < ss ; i++){ 21 | if(i == ss -1){ 22 | //cout << "p :" < 6 | using namespace std; 7 | 8 | int food[17][17]; 9 | int team1[9]; 10 | int team2[9]; 11 | int N; 12 | int sol = 99999999; 13 | int visit[17]; 14 | void calc(){ 15 | for(int i = 1; i <= N/2 ; i++){ 16 | visit[team1[i]] = 1; 17 | } 18 | for(int i = 1,j = 1; i <= N ; i++){ 19 | if(visit[i] == 0){ 20 | team2[j] = i; 21 | j++; 22 | } 23 | } 24 | 25 | int s1 = 0; 26 | int s2 = 0; 27 | int diff = 0; 28 | for(int i = 1 ; i < N/2 ; i++){ 29 | for(int j = i+1 ; j <= N/2 ; j++){ 30 | s1 += food[team1[i]][team1[j]]; 31 | s1 += food[team1[j]][team1[i]]; 32 | s2 += food[team2[i]][team2[j]]; 33 | s2 += food[team2[j]][team2[i]]; 34 | } 35 | } 36 | if(s1 > s2) 37 | diff = s1 - s2; 38 | else 39 | diff = s2 - s1; 40 | if(sol > diff) 41 | sol = diff; 42 | 43 | fill_n(visit,17,0); 44 | } 45 | void dfs(int max, int cnt){ 46 | //조합이 완성되면 시너지 계산 47 | if(cnt == N/2){ 48 | calc(); 49 | return; 50 | } 51 | for(int i = max + 1 ; i <= N/2 + cnt +1 ; i++) { 52 | team1[cnt+1] = i; 53 | dfs(i,cnt+1); 54 | } 55 | 56 | } 57 | int main(){ 58 | int T; 59 | cin >> T; 60 | int c = 1; 61 | while(T--){ 62 | cin >> N; 63 | //시너지 입력 64 | for(int i = 1; i <= N ; i++){ 65 | for(int j = 1; j <= N ; j++) 66 | cin >> food[i][j]; 67 | } 68 | 69 | for(int i = 1; i <= N/2+1 ; i++){ 70 | team1[1] = i; 71 | dfs(i,1); 72 | } 73 | cout <<"#"< 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | #define INF 99999999 11 | 12 | struct edge{ 13 | int toV; 14 | int w; 15 | }; 16 | 17 | struct comp{ 18 | bool operator()(edge a,edge b){ 19 | return a.w > b.w; 20 | }; 21 | }; 22 | 23 | int N,E; 24 | vector graph[801]; 25 | int sol[801]; 26 | void findD(int a){ 27 | fill_n(sol,801,INF); 28 | sol[a] = 0; 29 | priority_queue,comp> q; 30 | q.push({a,0}); 31 | while(q.size()){ 32 | int v = q.top().toV; 33 | q.pop(); 34 | for(auto it = graph[v].begin() ; it != graph[v].end() ; it++){ 35 | int nv = (*it).toV; 36 | int nw = (*it).w; 37 | if(sol[nv] == INF || sol[nv] > sol[v] + nw){ 38 | sol[nv] = sol[v] + nw; 39 | q.push({nv,sol[nv]}); 40 | } 41 | } 42 | } 43 | } 44 | int main(){ 45 | cin >> N >> E; 46 | int a,b,c; 47 | while(E--){ 48 | cin >> a >> b >> c; 49 | graph[a].push_back({b,c}); 50 | graph[b].push_back({a,c}); 51 | } 52 | 53 | int v1,v2; 54 | cin >> v1 >> v2; 55 | findD(1); 56 | int one_v1 = sol[v1]; 57 | int one_v2 = sol[v2]; 58 | findD(N); 59 | int v1_N = sol[v1]; 60 | int v2_N = sol[v2]; 61 | findD(v1); 62 | int v1_v2 = sol[v2]; 63 | 64 | if(one_v1 == INF || one_v2 == INF || v1_N == INF || v2_N == INF || v1_v2 == INF){ 65 | cout << "-1" << endl; 66 | return 0; 67 | } 68 | 69 | int sol1 = one_v1 + v1_v2 + v2_N; 70 | int sol2 = one_v2 + v1_v2 + v1_N; 71 | if(sol1 < sol2) 72 | cout << sol1; 73 | else 74 | cout << sol2; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /kakao/friends_4_Block.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-05. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int m = 6; 12 | int n = 6; 13 | int visit[30][30]; 14 | vector board = {"TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"}; 15 | vector map[30]; 16 | int answer = 0; 17 | 18 | void findMatch() { 19 | bool chk = false; 20 | for (int i = 0; i < n; i++) 21 | for (int j = 0; j < m; j++) 22 | visit[i][j] = 0; 23 | 24 | for (int i = 0; i < n - 1; i++) { 25 | for (int j = 0; j < m - 1; j++) { 26 | if (map[i][j] != 'X' && map[i][j] == map[i + 1][j] && map[i][j] == map[i + 1][j + 1] && 27 | map[i][j] == map[i][j + 1]) { 28 | visit[i][j] = 1; 29 | visit[i][j + 1] = 1; 30 | visit[i + 1][j] = 1; 31 | visit[i + 1][j + 1] = 1; 32 | chk = true; 33 | } 34 | } 35 | } 36 | for (int i = 0; i < n; i++) { 37 | for (int j = 0; j < m; j++) { 38 | if (visit[i][j]) { 39 | map[i][j] = 'X'; 40 | answer++; 41 | } 42 | } 43 | } 44 | 45 | for (int i = 0; i < n; i++) { 46 | for (int j = m - 1; j > -1; j--) { 47 | if (map[i][j] == 'X') { 48 | map[i].erase(map[i].begin() + j); 49 | map[i].push_back('X'); 50 | } 51 | } 52 | } 53 | 54 | if (chk) 55 | findMatch(); 56 | else 57 | return; 58 | } 59 | 60 | int main() { 61 | for (int i = m - 1; i > -1; i--) { 62 | for (int j = 0; j < n; j++) { 63 | map[j].push_back(board[i][j]); 64 | } 65 | } 66 | 67 | findMatch(); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /baekjoon/14889.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-05. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int N; 10 | int p[21][21]; 11 | int team1[11]; 12 | int sol = 99999999; 13 | int calcSin(){ 14 | vector team2; 15 | int visit[21]; 16 | for(int i = 1; i <= N ; i++) 17 | visit[i] = 0; 18 | for(int i = 1; i <=N/2 ; i++) 19 | visit[team1[i]] = 1; 20 | //team2생성 21 | for(int i = 1; i <= N ; i++){ 22 | if(visit[i] == 0) 23 | team2.push_back(i); 24 | } 25 | //team1 시너지 계산 26 | int t1 = 0; 27 | int t2 = 0; 28 | for(int i = 1; i <= N/2 ; i++){ 29 | for(int j = 1 ; j <= N/2 ; j++){ 30 | if(i != j) 31 | t1 += p[team1[i]][team1[j]]; 32 | } 33 | } 34 | 35 | for(int i = 0; i < N/2 ; i++){ 36 | for(int j = 0 ; j < N/2 ; j++){ 37 | if(i != j) 38 | t2 += p[team2[i]][team2[j]]; 39 | } 40 | } 41 | int diff; 42 | if(t1 > t2) 43 | diff = t1 - t2; 44 | else 45 | diff = t2 - t1; 46 | 47 | return diff; 48 | } 49 | void makeTeam(int max, int depth){ 50 | //team 완성되었을 때 51 | if(depth == N/2){ 52 | int min = calcSin(); 53 | if(sol > min) 54 | sol = min; 55 | return; 56 | } 57 | 58 | for(int i = max + 1 ; i <= N/2 + depth +1 ; i++){ 59 | team1[depth + 1] = i; 60 | makeTeam(i,depth+1); 61 | } 62 | } 63 | 64 | 65 | int main(){ 66 | cin >> N; 67 | for(int i = 1; i <= N ; i++) 68 | for(int j = 1; j <= N ; j++) 69 | cin >> p[i][j]; 70 | 71 | //dfs로 팀을 만듬 72 | for(int i = 1; i <= (N/2 + 1) ; i++){ 73 | team1[1] = i; 74 | makeTeam(i,1); 75 | } 76 | 77 | cout << sol << endl; 78 | return 0; 79 | } -------------------------------------------------------------------------------- /baekjoon/10216.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int enemy[3001][3];//(x,y,R) 7 | int adj[3001][3001]; 8 | int visit[3001]; 9 | queue q; 10 | int N = 0; 11 | void BFS(){ 12 | while(!q.empty()){ 13 | int x = q.front(); 14 | q.pop(); 15 | for(int i = 1; i <= N ; i++){ 16 | if(visit[i] == 0 && adj[x][i] == 1){ 17 | visit[i] = 1; 18 | q.push(i); 19 | } 20 | } 21 | } 22 | return; 23 | } 24 | int main(){ 25 | int T,x,y,R,sol; 26 | cin >> T; 27 | while(T--){ 28 | fill_n(&enemy[1][0],3000*3,0); 29 | fill_n(&adj[1][0],3000*3000,0); 30 | fill_n(&visit[1],3000,0); 31 | sol = 0; 32 | cin >> N; 33 | //입력받고 34 | for(int i = 1 ; i <= N ; i++){ 35 | cin >> enemy[i][0] >> enemy[i][1] >> enemy[i][2]; 36 | } 37 | //인접행렬 구하고 38 | int xs,xd,ys,yd,R1,R2,D; 39 | for(int i = 1 ; i <= N ; i++){ 40 | for(int j = i+1 ; j <= N ; j++){ 41 | xs = enemy[i][0]; 42 | xd = enemy[j][0]; 43 | ys = enemy[i][1]; 44 | yd = enemy[j][1]; 45 | R1 = enemy[i][2]; 46 | R2 = enemy[j][2]; 47 | D = (xd-xs)*(xd-xs) + (yd-ys)*(yd-ys); 48 | if((R1+R2)*(R1+R2) >= D){ 49 | adj[i][j] = 1; 50 | adj[j][i] = 1; 51 | } 52 | } 53 | } 54 | for(int i = 1; i <= N ; i++){ 55 | if(visit[i] == 0){ 56 | sol++; 57 | visit[i] = 1; 58 | q.push(i); 59 | BFS(); 60 | } 61 | } 62 | cout << sol < 2 | // #include // uncomment to use scanf/printf instead of cin/cout 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | int map[1001][1001]; 8 | int visit[1001]; 9 | 10 | int main() { 11 | int N; 12 | cin >> N; 13 | string line; 14 | // drop a newline traling n 15 | int max = -1; 16 | while (N-- > -1) { 17 | getline(cin, line); 18 | stringstream ss(line); 19 | cout << line << endl; 20 | bool chk = false; 21 | int from; 22 | int to; 23 | //cout << "**************" << endl; 24 | for (int fork; !(ss >> fork).fail();) { // read forks from a note 25 | // @todo Write your code here. 26 | //cout << fork << endl; 27 | visit[fork] = 1; 28 | if (max < fork) 29 | max = fork; 30 | if (!chk) { 31 | from = fork; 32 | chk = true; 33 | continue; 34 | } else { 35 | map[from][fork] = 1; 36 | from = fork; 37 | } 38 | } 39 | } 40 | 41 | int cnt; 42 | for (int i = 1; i <= max; i++) { 43 | cnt = 0; 44 | for (int j = 1; j <= max; j++) { 45 | if (map[i][j]) 46 | cnt++; 47 | if (cnt > 1) { 48 | cout << -1 << endl; 49 | return 0; 50 | } 51 | } 52 | } 53 | 54 | for (int i = 1; i <= max; i++) { 55 | bool chk1 = true; 56 | for (int j = 1; j <= max; j++) { 57 | if (map[i][j]) { 58 | chk1 = false; 59 | break; 60 | } 61 | } 62 | if (chk1 && visit[i] == 1) { 63 | cout << i << " "; 64 | } 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /baekjoon/2583.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-26. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | int arr[100][100]; 11 | int visit[100][100]; 12 | int Dx[4] = {0,0,1,-1}; 13 | int Dy[4] = {1,-1,0,0}; 14 | int M,N,K; 15 | int sx,sy,dx,dy; 16 | vector sol; 17 | queue> q; 18 | int cnt; 19 | pair tmp; 20 | 21 | void BFS(){ 22 | cnt = 0; 23 | while(q.size()){ 24 | int y = q.front().first; 25 | int x = q.front().second; 26 | q.pop(); 27 | for(int i = 0 ; i < 4 ; i++){ 28 | int nx = x + Dx[i]; 29 | int ny = y + Dy[i]; 30 | if(nx >=0 && nx < N && ny >=0 && ny < M && arr[ny][nx] == 0 && visit[ny][nx] == 0){ 31 | visit[ny][nx] = 1; 32 | cnt++; 33 | q.push({ny,nx}); 34 | } 35 | } 36 | } 37 | sol.push_back(cnt+1); 38 | }; 39 | 40 | int main(){ 41 | cin >> M >> N >> K; 42 | while(K--){ 43 | cin >> sx >> sy >> dx >> dy; 44 | for(int i = sy ; i < dy ; i++ ){ 45 | for(int j = sx ; j < dx ; j++){ 46 | arr[i][j] = 1; 47 | } 48 | } 49 | } 50 | for(int i = 0 ; i < M ; i++){ 51 | for(int j = 0 ; j < N ; j++){ 52 | cout< 6 | #include 7 | 8 | using namespace std; 9 | int N,M; 10 | vector> h; 11 | vector> c; 12 | int combination[13]; 13 | int sol = 99999999; 14 | int d[100][13]; 15 | void calc(){ 16 | int msol = 0; 17 | for(int i = 0 ; i < h.size(); i++){ 18 | int m = 99999999; 19 | for(int j = 0 ; j < c.size(); j++){ 20 | if(combination[j]){ 21 | if(m > d[i][j]){ 22 | m = d[i][j]; 23 | } 24 | } 25 | } 26 | msol += m; 27 | } 28 | if(sol > msol) 29 | sol = msol; 30 | } 31 | void makeCombination(int current, int size, int target){ 32 | if(size == target){ 33 | calc(); 34 | return; 35 | } 36 | for(int i = current + 1; i <= c.size() + size - target ; i++){ 37 | combination[i] = 1; 38 | makeCombination(i,size+1,M); 39 | combination[i] = 0; 40 | } 41 | } 42 | void comb(){ 43 | for(int i = 0 ; i <= c.size() - M; i++){ 44 | combination[i] = 1; 45 | makeCombination(i,1,M); 46 | combination[i] = 0; 47 | } 48 | } 49 | void dcalc(){ 50 | for(int i = 0 ; i < h.size(); i++){ 51 | for(int j= 0 ; j < c.size(); j++){ 52 | d[i][j] = abs(h[i].first - c[j].first)+ abs(h[i].second - c[j].second); 53 | } 54 | } 55 | } 56 | int main(){ 57 | cin >> N >> M; 58 | int a; 59 | for(int i = 1 ; i <= N ; i++){ 60 | for(int j = 1; j <= N ; j++){ 61 | cin >> a; 62 | //집 63 | if(a == 1){ 64 | h.emplace_back(make_pair(i,j)); 65 | } 66 | //치킨 67 | else if(a == 2){ 68 | c.emplace_back(make_pair(i,j)); 69 | } 70 | } 71 | } 72 | dcalc(); 73 | comb(); 74 | cout << sol << endl; 75 | } -------------------------------------------------------------------------------- /baekjoon/10866.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-14. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main(){ 12 | deque dq; 13 | int N; 14 | cin >> N; 15 | string str; 16 | int a; 17 | int tmp; 18 | while(N--){ 19 | cin >> str; 20 | if(str == "push_back"){ 21 | cin >> a; 22 | dq.push_back(a); 23 | } 24 | 25 | else if(str == "push_front"){ 26 | cin >> a; 27 | dq.push_front(a); 28 | } 29 | 30 | else if(str == "pop_front") { 31 | if(dq.empty()){ 32 | cout << "-1" < 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | struct xy { 10 | int y, x; 11 | }; 12 | queue q; 13 | int map[21][21]; //맵 14 | int visit[21][21]; //방문체크 배열 15 | int dx[4] = { 0,0,1,-1 }; //4방향 체크 16 | int dy[4] = { 1,-1,0,0 }; 17 | int N, M; //맵의 크기, 지불금액 18 | int sol; 19 | void bfs() { 20 | //초기값 설정 21 | int t; 22 | int d = 1; //현재 서비스 영역의 크기 23 | int n = 0; //현재 서비스를 받는 집의 수 24 | if (map[q.front().y][q.front().x] == 1) 25 | n = 1; 26 | int cost = d*d +(d-1)*(d-1); //서비스 비용 27 | if (cost <= n*M) { 28 | if (sol < n) 29 | sol = n; 30 | } 31 | int ny, nx; 32 | xy front; 33 | while (!q.empty()) { 34 | t = q.size(); 35 | //한 사이클을 체크하기 위해 현재 큐 사이즈만큼만 BFS 36 | while (t--) { 37 | front = q.front(); 38 | q.pop(); 39 | for (int i = 0; i < 4; i++) { 40 | nx = front.x + dx[i]; 41 | ny = front.y + dy[i]; 42 | //유요한 영역이면 큐에 넣음 43 | if (nx > 0 && nx < N + 1 && ny > 0 && ny < N + 1 && visit[ny][nx] == 0) { 44 | visit[ny][nx] = 1; 45 | q.push({ ny,nx }); 46 | //집이면 혜택받는 집++ 47 | if (map[ny][nx] == 1) 48 | n++; 49 | } 50 | } 51 | } 52 | d++; //서비스 영역의 크기 하나 증가 53 | cost = d*d + (d - 1)*(d - 1); //비용 계산 54 | if (cost <= n*M) { //손해를 안보면 -> 답을 갱신할지 판단 55 | if (sol < n) 56 | sol = n; 57 | } 58 | } 59 | } 60 | 61 | int main() { 62 | int T; 63 | cin >> T; 64 | int c = 1; 65 | while (T--) { 66 | //입력 67 | cin >> N >> M; 68 | for (int i = 1; i <= N; i++) { 69 | for (int j = 1; j <= N; j++) { 70 | cin >> map[i][j]; 71 | } 72 | } 73 | //각 점을 시작으로 BFS를 통해 영역을 확장시켜나감 74 | for (int i = 1; i <= N; i++) { 75 | for (int j = 1; j <= N; j++) { 76 | fill_n(&visit[0][0], 21 * 21, 0); 77 | visit[i][j] = 1; 78 | q.push({ i,j }); 79 | bfs(); 80 | } 81 | } 82 | cout << "#" << c << " " << sol << endl; 83 | sol = 0; 84 | c++; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /baekjoon/14891_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-25. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int arr[4][8]; 9 | int visit[4]; 10 | 11 | void rotate(int num, int direction) { 12 | //cout << num+1 << "th :" << direction << endl; 13 | int ldir = -2; 14 | int rdir = -2; 15 | //왼쪽 톱니바퀴가 있고, 현재[6] == 왼쪽[2]이면 반대방향 회전 16 | if (num - 1 >= 0 && visit[num - 1] == 0 && arr[num][6] != arr[num - 1][2]) { 17 | ldir = -1 * direction; 18 | } 19 | //오른쪽 톱니바퀴가 있고, 현재[2] == 오른쪽[6]이면 반대방향 회전 20 | if (num + 1 < 4 && visit[num + 1] == 0 && arr[num + 1][6] != arr[num][2]) { 21 | rdir = -1 * direction; 22 | } 23 | //본인 24 | //시계방향 25 | if (direction == 1) { 26 | int tmp = arr[num][7]; 27 | for (int i = 7; i > 0; i--) 28 | arr[num][i] = arr[num][i - 1]; 29 | arr[num][0] = tmp; 30 | } 31 | //반시계방향 32 | else { 33 | int tmp = arr[num][0]; 34 | for (int i = 1; i < 8; i++) 35 | arr[num][i - 1] = arr[num][i]; 36 | arr[num][7] = tmp; 37 | } 38 | //왼쪽 39 | if (ldir != -2) { 40 | visit[num - 1] = 1; 41 | rotate(num - 1, ldir); 42 | } 43 | //오른쪽 44 | if (rdir != -2) { 45 | visit[num + 1] = 1; 46 | rotate(num + 1, rdir); 47 | } 48 | } 49 | 50 | int main() { 51 | int K; 52 | char a; 53 | for (int i = 0; i < 4; i++) { 54 | for (int j = 0; j < 8; j++) { 55 | cin >> a; 56 | if(a == '0') 57 | arr[i][j] = 0; 58 | else 59 | arr[i][j] = 1; 60 | } 61 | } 62 | cin >> K; 63 | int num, dir; 64 | while (K--) { 65 | cin >> num >> dir; 66 | for (int i = 0; i < 4; i++) { 67 | visit[i] = 0; 68 | } 69 | visit[num-1] = 1; 70 | rotate(num-1, dir); 71 | } 72 | cout << arr[0][0] + arr[1][0]*2 + arr[2][0]*4 + arr[3][0]*8 << endl; 73 | return 0; 74 | } -------------------------------------------------------------------------------- /baekjoon/15685.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-27. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int map[101][101]; 11 | int N; 12 | int dx[4] = {1, 0, -1, 0}; 13 | int dy[4] = {0, -1, 0, 1}; 14 | int x, y, d, g; 15 | 16 | void calc() { 17 | vector> v; //y,x 18 | //1세대 19 | v.emplace_back(make_pair(y, x)); 20 | v.emplace_back(make_pair(y + dy[d], x + dx[d])); 21 | map[y][x] = 1; 22 | map[y + dy[d]][x + dx[d]] = 1; 23 | // cout <<0<<"th generation"< -1; j--) { 37 | dx1 = sx - v[j].second; 38 | dy1 = sy - v[j].first; 39 | nx = sx + dy1; 40 | ny = sy - dx1; 41 | map[ny][nx] = 1; 42 | v.emplace_back(make_pair(ny,nx)); 43 | } 44 | // cout <> N; 66 | for (int i = 0; i < N; i++) { 67 | cin >> x >> y >> d >> g; 68 | calc(); 69 | } 70 | cout << findSol() << endl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /SW_EXPERT/5658.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-12. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | vector v; 11 | set s; 12 | int T,N,K; 13 | int sol; 14 | int getDec(char a){ 15 | if(a >= 48 && a <58){ 16 | return a-48; 17 | } 18 | else{ 19 | return a-55; 20 | } 21 | } 22 | void calc(int start){ 23 | // cout << start+1<<"th calc start!!"< i-1 ; j--){ 28 | // cout << v[j] <<" -> "<< getDec(v[j])<> T; 62 | int a = 1; 63 | char c; 64 | while(T--){ 65 | cin >> N >> K; 66 | for(int i = 0 ; i < N ; i++){ 67 | cin >> c; 68 | v.push_back(c); 69 | } 70 | // for(int i = 0 ;i 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | char map[50][50]; 12 | int visit[50][50]; 13 | int dp[50][50]; 14 | int N, M; 15 | bool cycle = false; 16 | //상하좌우 17 | int dx[4] = {0, 0, -1, 1}; 18 | int dy[4] = {1, -1, 0, 0}; 19 | 20 | struct yx { 21 | int y, x,cnt; 22 | }; 23 | queue q; 24 | bool chk(int y, int x){ 25 | if(x >= 0 && x < M && y >= 0 && y < N) 26 | return true; 27 | return false; 28 | } 29 | 30 | void dfs(int y, int x){ 31 | int nx,ny; 32 | for(int i = 0 ; i < 4 ; i++){ 33 | nx = x + dx[i]*(map[y][x]-48); 34 | ny = y + dy[i]*(map[y][x]-48); 35 | if(chk(ny,nx)){ 36 | if(map[ny][nx] == 'H'){ 37 | continue; 38 | } 39 | //첫방문 40 | if(!visit[ny][nx]){ 41 | visit[ny][nx] = 1; 42 | dfs(ny,nx); 43 | visit[ny][nx] = 0; 44 | } 45 | //사이클 46 | else{ 47 | cycle = true; 48 | return; 49 | } 50 | } 51 | } 52 | } 53 | int sol(int y,int x){ 54 | int nx,ny; 55 | if(!chk(y,x) || map[y][x] == 'H') 56 | return 0; 57 | int& ret = dp[y][x]; 58 | if(ret) { 59 | cout <> N >> M; 72 | for (int i = 0; i < N; i++) 73 | for (int j = 0; j < M; j++) 74 | cin >> map[i][j]; 75 | visit[0][0] = 1; 76 | dfs(0,0); 77 | if(cycle) { 78 | cout << -1 << endl; 79 | } 80 | else{ 81 | cout << sol(0,0) << endl; 82 | } 83 | return 0; 84 | } -------------------------------------------------------------------------------- /baekjoon/14500.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-03. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int arr[507][507]; 9 | int N,M; 10 | int sol; 11 | int t[19]; 12 | void find(int y,int x){ 13 | t[1] = arr[y][x] + arr[y][x+1] + arr[y][x+2] + arr[y][x+3]; 14 | t[2] = arr[y][x] + arr[y+1][x] + arr[y+2][x] + arr[y+3][x]; 15 | // 16 | t[3] = arr[y][x] + arr[y][x+1] + arr[y+1][x] + arr[y+1][x+1]; 17 | // 18 | t[4] = arr[y][x] + arr[y+1][x] +arr[y+2][x] +arr[y+2][x+1]; 19 | t[5] = arr[y][x] + arr[y+1][x] +arr[y+2][x] +arr[y+2][x-1]; 20 | t[6] = arr[y][x] + arr[y+1][x] +arr[y+2][x] +arr[y][x+1]; 21 | t[7] = arr[y][x] + arr[y][x+1] +arr[y+1][x+1] +arr[y+2][x+1]; 22 | // 23 | t[8] = arr[y][x] + arr[y+1][x] +arr[y+1][x+1] +arr[y+1][x+2]; 24 | t[9] = arr[y][x] + arr[y][x+1] +arr[y][x+2] +arr[y+1][x]; 25 | t[10] = arr[y][x] + arr[y][x+1] +arr[y][x+2] +arr[y+1][x+2]; 26 | t[11] = arr[y][x] + arr[y][x+1] +arr[y][x+2] +arr[y-1][x+2]; 27 | //ㅗ,ㅜ 28 | t[12] = arr[y][x] + arr[y][x+1] +arr[y][x+2] +arr[y+1][x+1]; 29 | t[13] = arr[y][x] + arr[y][x+1] +arr[y][x+2] +arr[y-1][x+1]; 30 | //ㅓ,ㅏ 31 | t[14] = arr[y][x] + arr[y+1][x] +arr[y+2][x] +arr[y+1][x+1]; 32 | t[15] = arr[y][x] + arr[y+1][x] +arr[y+2][x] +arr[y+1][x-1]; 33 | // 34 | t[16] = arr[y][x] + arr[y+1][x] +arr[y+1][x+1] +arr[y+2][x+1]; 35 | t[0] = arr[y][x] + arr[y+1][x] +arr[y][x+1] +arr[y-1][x+1]; 36 | // 37 | t[17] = arr[y][x] + arr[y][x+1] + arr[y+1][x+1] + arr[y+1][x+2]; 38 | t[18] = arr[y][x] + arr[y][x+1] + arr[y-1][x+1] + arr[y-1][x+2]; 39 | sort(t,t+19); 40 | if(sol < t[18]) 41 | sol = t[18]; 42 | } 43 | 44 | int main(){ 45 | cin >> N >> M; 46 | 47 | for(int i = 2; i <= N+1; i++) 48 | for(int j = 2; j <= M+1 ; j++) 49 | cin >> arr[i][j]; 50 | 51 | for(int i = 2; i <= N+1; i++) 52 | for(int j = 2; j <= M+1 ; j++) 53 | find(i,j); 54 | 55 | cout << sol << endl; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /kakao/dart_game.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-03. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | string dartResult = "1S2D*3T"; 12 | int score[3]; 13 | string bonus[3]; 14 | string option[3]; 15 | int idx = 0; 16 | for (int i = 0; i < dartResult.length(); i++) { 17 | if (dartResult[i] == 'S' || dartResult[i] == 'D' || dartResult[i] == 'T') { 18 | //cout <<"bonus in! : "<< i << endl; 19 | bonus[idx] = dartResult[i]; 20 | if (i + 1 < dartResult.length() && (dartResult[i + 1] == '#' || dartResult[i + 1] == '*')) { 21 | option[idx] = dartResult[i + 1]; 22 | //cout<< "option in!! : "<< i < -1 && (int)dartResult[i - 1] >= 48 && (int)dartResult[i - 1] < 58) { 25 | //cout << "score in!! : "<< i << " " << dartResult[i - 1] < -1 && (int)dartResult[i - 2] == 49) 28 | score[idx] = 10; 29 | else 30 | score[idx] = 0; 31 | } else 32 | score[idx] = (int) dartResult[i - 1] - 48; 33 | } 34 | idx++; 35 | } 36 | 37 | } 38 | 39 | // for(int i = 0 ; i < 3 ; i++){ 40 | // cout << score[i] << ' '<< bonus[i] << ' ' << option[i] << endl; 41 | // } 42 | 43 | for(int i = 0 ; i < 3; i++){ 44 | if(bonus[i] == "S") 45 | ; 46 | else if(bonus[i] == "D") 47 | score[i] = (score[i]*score[i]); 48 | else 49 | score[i] = (score[i]*score[i]*score[i]); 50 | if(option[i] == "*"){ 51 | score[i] *= 2; 52 | if(i) 53 | score[i-1] *= 2; 54 | } 55 | if(option[i] == "#"){ 56 | score[i] *= -1; 57 | } 58 | } 59 | 60 | cout << score[0] + score[1] + score[2] << endl; 61 | return 0; 62 | } -------------------------------------------------------------------------------- /kakao-2018-1st-codingTest/1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-15. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | vector record = {"Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan"}; 12 | vector solution; 13 | map m; 14 | vector> s; 15 | 16 | int main(){ 17 | string tmpstr; 18 | string command; 19 | string id; 20 | string nickname; 21 | 22 | for(int i = 0 ; i < record.size() ; i++){ 23 | tmpstr = record[i]; 24 | command = tmpstr.substr(0,tmpstr.find(" ")); 25 | tmpstr = tmpstr.substr(tmpstr.find(" ")+1); 26 | id = tmpstr.substr(0,tmpstr.find(" ")); 27 | if(command != "Change"){ 28 | s.push_back({command,id}); 29 | if(command == "Leave"){ 30 | cout << command << " " << id << endl; 31 | continue; 32 | } 33 | } 34 | nickname = tmpstr.substr(tmpstr.find(" ")+1); 35 | cout << command << " " << id << " "<< nickname << endl; 36 | 37 | //없으면 넣고, 있으면 변경하고 38 | auto iter = m.find(id); 39 | if(iter == m.end()) 40 | m.insert({id,nickname}); 41 | else 42 | iter->second = nickname; 43 | } 44 | cout << "**********" << endl; 45 | for(int i = 0 ; i < s.size(); i++){ 46 | cout << s[i].first <<"," << s[i].second << endl; 47 | } 48 | 49 | cout << "**********" << endl; 50 | for(auto it = m.begin() ; it != m.end() ; it++){ 51 | cout << it->first << "," << it->second << endl; 52 | } 53 | 54 | for(int i = 0 ; i < s.size() ; i++){ 55 | if(s[i].first == "Enter"){ 56 | solution.push_back(m.find(s[i].second)->second+"님이 들어왔습니다."); 57 | } 58 | else{ 59 | solution.push_back(m.find(s[i].second)->second+"님이 나갔습니다."); 60 | } 61 | } 62 | for(int i = 0 ; i < s.size() ; i++) { 63 | cout << solution[i] << endl; 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /baekjoon/7569.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-20. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int dx[6] = {0,1,0,-1,0,0}; 10 | int dy[6] = {1,0,-1,0,0,0}; 11 | int dz[6] = {0,0,0,0,1,-1}; 12 | int arr[101][101][101]; 13 | int M,N,H; 14 | 15 | struct xy{ 16 | int x; 17 | int y; 18 | int z; 19 | }; 20 | 21 | queue q; 22 | 23 | void BFS(int cnt){ 24 | int t = q.size(); 25 | xy tmp; 26 | xy tmp1; 27 | while(t--){ 28 | tmp = q.front(); 29 | q.pop(); 30 | for(int i = 0; i <= 5; i++){ 31 | int x = tmp.x + dx[i]; //가로 32 | int y = tmp.y + dy[i]; //세로 33 | int z = tmp.z + dz[i]; //높이 34 | if(x >= 1 && x <= M && y >= 1 && y <= N && z >= 1 && z <= H){ 35 | if(arr[z][y][x] == 0){ 36 | arr[z][y][x] = 1; 37 | tmp1.x = x; 38 | tmp1.y = y; 39 | tmp1.z = z; 40 | q.push(tmp1); 41 | } 42 | } 43 | } 44 | } 45 | 46 | if(q.empty()){ 47 | for(int i = 1; i <= H ; i++) { 48 | for (int j = 1; j <= N; j++) { 49 | for (int k = 1; k <= M; k++) { 50 | if (arr[i][j][k] == 0) { 51 | cout << "-1" << endl; 52 | return; 53 | } 54 | } 55 | } 56 | } 57 | cout << cnt << endl; 58 | return; 59 | } 60 | BFS(cnt+1); 61 | }; 62 | 63 | 64 | int main(){ 65 | xy tmp; 66 | cin >> M >> N >> H; 67 | for(int i = 1 ; i <= H ; i++){ 68 | for(int j = 1 ; j <= N ; j++){ 69 | for(int k =1 ; k <= M ; k++) { 70 | cin >> arr[i][j][k]; 71 | if (arr[i][j][k] == 1) { 72 | tmp.x = k; //가로 73 | tmp.y = j; //세로 74 | tmp.z = i; //높이 75 | q.push(tmp); 76 | } 77 | } 78 | } 79 | } 80 | BFS(0); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /baekjoon/1753.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-26. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | #define INF 999999999 11 | 12 | vector> graph[20001]; // to, weight 13 | int sol[20001]; 14 | int V,E; 15 | struct comp { 16 | bool operator()(pair a, pair b) { 17 | return a.second > b.second; 18 | } 19 | }; 20 | 21 | void FindD(int i){ 22 | priority_queue,vector>,comp> q; 23 | q.push({i,0}); 24 | cout < sol[v] + nw) { 38 | sol[nv] = sol[v] + nw; 39 | q.push({nv,sol[nv]}); 40 | cout <> V >> E >> s; 50 | int u,v,w; 51 | 52 | 53 | //그래프 생성 54 | for(int i = 1 ; i <= E ; i++) { 55 | cin >> u >> v >> w; 56 | int chk = true; 57 | for(auto it = graph[u].begin(); it != graph[u].end(); it++){ 58 | if((*it).first == v){ 59 | chk = false; 60 | if((*it).second > w) { 61 | (*it).second = w; 62 | } 63 | } 64 | } 65 | if(chk) 66 | graph[u].emplace_back(make_pair(v,w)); 67 | } 68 | 69 | 70 | for(int i = 1; i <= V ; i++) 71 | sol[i] = INF; 72 | sol[s] = 0; 73 | //다익스트라 74 | FindD(s); 75 | 76 | for(int i = 1 ; i <= V ; i++) { 77 | if (sol[i] == INF) 78 | cout << "INF" << '\n'; 79 | else 80 | cout << sol[i] << '\n'; 81 | } 82 | return 0; 83 | } -------------------------------------------------------------------------------- /baekjoon/2206.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-07. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | int N,M; 8 | int map[1001][1001]; 9 | int visit[1001][1001][2]; 10 | struct xy{ 11 | int x,y,c,d; 12 | }; 13 | queue q; 14 | 15 | int dx[4] = {1,-1,0,0}; 16 | int dy[4] = {0,0,1,-1}; 17 | int sol = 999999999; 18 | void bfs(){ 19 | visit[1][1][0] = 1; 20 | visit[1][1][1] = 1; 21 | q.push({1,1,0,0}); 22 | int nx,ny; 23 | while(!q.empty()){ 24 | xy tmp = q.front(); 25 | //cout < tmp.d+1) 32 | sol = tmp.d+1; 33 | } 34 | 35 | if(nx > 0 && nx <= M && ny > 0 && ny <= N && ((visit[ny][nx][0] == 0 && tmp.c == 0)||(visit[ny][nx][1] == 0 && tmp.c == 1))){ 36 | // /벽이 있다면 37 | if(map[ny][nx] == 1){ 38 | //그전에 벽을 부신적이 없다면 39 | if(tmp.c == 0){ 40 | visit[ny][nx][0] = 1; 41 | q.push({nx,ny,1,tmp.d+1}); 42 | } 43 | } 44 | //벽이 없다면 45 | else{ 46 | if(tmp.c == 0) { 47 | visit[ny][nx][0] = 1; 48 | } else 49 | visit[ny][nx][1] = 1; 50 | q.push({nx,ny,tmp.c,tmp.d+1}); 51 | } 52 | } 53 | } 54 | } 55 | } 56 | int main() { 57 | cin >> N >> M; 58 | char a; 59 | for(int i = 1 ; i <= N ; i++) { 60 | for (int j = 1; j <= M; j++) { 61 | cin >> a; 62 | if(a == '1') 63 | map[i][j] = 1; 64 | else 65 | map[i][j] = 0; 66 | } 67 | } 68 | if(N==1 && M==1){ 69 | cout << 1 << endl; 70 | return 0; 71 | } 72 | bfs(); 73 | if(sol == 999999999) 74 | cout << -1 << endl; 75 | else 76 | cout << sol+1 << endl; 77 | return 0; 78 | } -------------------------------------------------------------------------------- /SW_EXPERT/4013_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-17. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int magnet[5][8]; 10 | int visit[5]; 11 | int direction[5]; 12 | int T, K,sol; 13 | 14 | void setDirection(int num, int d) { 15 | visit[num] = 1; 16 | direction[num] = d; 17 | //왼쪽 18 | if (num != 1) { 19 | //방문 가능할 때! 20 | if (!visit[num - 1]) { 21 | //자성이 다를때 반대방향으로 22 | if (magnet[num][6] != magnet[num - 1][2]) { 23 | setDirection(num-1,d*(-1)); 24 | } 25 | } 26 | } 27 | //오른쪽 28 | if (num != 4) { 29 | //방문 가능할 때! 30 | if (!visit[num + 1]) { 31 | //자성이 다를때 반대방향으로 32 | if (magnet[num][2] != magnet[num + 1][6]) { 33 | setDirection(num+1,d*(-1)); 34 | } 35 | } 36 | } 37 | } 38 | void rotate(){ 39 | int tmp; 40 | for(int i = 1; i < 5 ; i++){ 41 | if(direction[i] == 1){ 42 | tmp = magnet[i][7]; 43 | for(int j = 6 ; j > -1 ; j--){ 44 | magnet[i][j+1] = magnet[i][j]; 45 | } 46 | magnet[i][0] = tmp; 47 | } 48 | else if(direction[i] == -1){ 49 | tmp = magnet[i][0]; 50 | for(int j = 0 ; j < 7 ; j++){ 51 | magnet[i][j] = magnet[i][j+1]; 52 | } 53 | magnet[i][7] = tmp; 54 | } 55 | } 56 | } 57 | void init(){ 58 | for(int i = 1 ; i < 5 ; i++) { 59 | visit[i] = 0; 60 | direction[i] = 0; 61 | } 62 | } 63 | void calc(){ 64 | int a = 1; 65 | for(int i = 1; i < 5 ; i++){ 66 | sol += magnet[i][0]*a; 67 | a *= 2; 68 | } 69 | } 70 | int main() { 71 | cin >> T; 72 | int a = 1; 73 | int magnetNum, d; 74 | while (T--) { 75 | cin >> K; 76 | for (int i = 1; i < 5; i++) { 77 | for (int j = 0; j < 8; j++) 78 | cin >> magnet[i][j]; 79 | } 80 | while (K--) { 81 | cin >> magnetNum >> d; 82 | setDirection(magnetNum, d); 83 | rotate(); 84 | init(); 85 | } 86 | calc(); 87 | cout << "#"< 6 | #include 7 | using namespace std; 8 | char map[51][51]; 9 | int waterTime[51][51]; 10 | int visit[51][51]; 11 | int dx[4] = {1,-1,0,0}; 12 | int dy[4] = {0,0,1,-1}; 13 | int R,C; 14 | int sol = -1; 15 | struct xy{ 16 | int y,x,t; 17 | }; 18 | queue q; 19 | void calcTime(){ 20 | int nx,ny; 21 | while (!q.empty()) { 22 | xy tmp = q.front(); 23 | q.pop(); 24 | for (int i = 0; i < 4; i++) { 25 | nx = tmp.x + dx[i]; 26 | ny = tmp.y + dy[i]; 27 | if (nx > 0 && nx <= C && ny > 0 && ny <= R && map[ny][nx] != 'X' && map[ny][nx] != 'D' && visit[ny][nx] == 0) { 28 | visit[ny][nx] = 1; 29 | waterTime[ny][nx] = tmp.t + 1; 30 | q.push({ny, nx, tmp.t + 1}); 31 | } 32 | } 33 | } 34 | } 35 | 36 | void bfs(int y, int x){ 37 | int ny,nx; 38 | visit[y][x] = 1; 39 | q.push({y,x,0}); 40 | while(!q.empty()){ 41 | xy tmp = q.front(); 42 | q.pop(); 43 | for(int i = 0 ; i < 4 ; i++){ 44 | nx = tmp.x + dx[i]; 45 | ny = tmp.y + dy[i]; 46 | if(map[ny][nx] == 'D'){ 47 | sol = tmp.t + 1; 48 | return; 49 | } 50 | if(nx >0 && nx <= C && ny >0 && ny <= R && map[ny][nx] != 'X' && map[ny][nx] != '*' && visit[ny][nx] == 0 && waterTime[ny][nx] > tmp.t+1){ 51 | visit[ny][nx] = 1; 52 | q.push({ny,nx,tmp.t+1}); 53 | } 54 | } 55 | } 56 | } 57 | 58 | int main(){ 59 | fill_n(&waterTime[0][0],51*51,99999999); 60 | cin >> R >> C; 61 | int wx = -1; 62 | int wy = -1; 63 | int x,y; 64 | for(int i = 1 ; i <= R ; i++) { 65 | for (int j = 1; j <= C; j++){ 66 | cin >> map[i][j]; 67 | if(map[i][j] == '*'){ 68 | q.push({i,j,0}); 69 | } 70 | if(map[i][j] == 'S'){ 71 | y = i; 72 | x = j; 73 | } 74 | } 75 | } 76 | 77 | calcTime(); 78 | fill_n(&visit[0][0],51*51,0); 79 | bfs(y,x); 80 | if(sol == -1) 81 | cout << "KAKTUS"< 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | string m = "ABC"; 10 | vector musicinfos = {"12:00,12:14,HELLO,C#DEFGAB", "13:00,13:05,WORLD,ABCDEF"}; 11 | 12 | int main(){ 13 | string answer = "`(None)`"; 14 | int ptime = -1; 15 | string info; 16 | int start_hour; 17 | int start_minute; 18 | int end_hour; 19 | int end_minute; 20 | int interval; 21 | string name; 22 | string music; 23 | 24 | string tmp = ""; 25 | for(int i = 0 ; i < m.size() ; i++){ 26 | if(m[i] >= 'A' && m[i] <='G'&& i+1 < m.size() && m[i+1] == '#'){ 27 | tmp += (m[i]+32); 28 | i++; 29 | } 30 | else 31 | tmp += m[i]; 32 | } 33 | m = tmp; 34 | cout << m << endl; 35 | for(int i = 0 ; i < musicinfos.size(); i++){ 36 | info = musicinfos[i]; 37 | start_hour = stoi(info.substr(0,2)); 38 | start_minute = stoi(info.substr(3,2)); 39 | end_hour = stoi(info.substr(6,2)); 40 | end_minute = stoi(info.substr(9,2)); 41 | interval = 60*(end_hour - start_hour) + end_minute - start_minute; 42 | info = info.substr(12); 43 | name = info.substr(0,info.find(',')); 44 | music = info.substr(info.find(',')+1); 45 | string musictmp = ""; 46 | for(int i = 0 ; i < music.size() ; i++){ 47 | if(music[i] >= 'A' && music[i] <='G'&& i+1 < music.size() && music[i+1] == '#'){ 48 | musictmp += (music[i]+32); 49 | i++; 50 | } 51 | else 52 | musictmp += music[i]; 53 | } 54 | music = musictmp; 55 | 56 | string total = ""; 57 | for(int i = 0 ; i < interval/music.size(); i++){ 58 | total += music; 59 | } 60 | int n = 0; 61 | for(int i = 0 ; i < interval%music.size() + n ; i++){ 62 | total += music[i]; 63 | } 64 | 65 | cout << interval << '\t' << name << '\t' << music <<'\t'< ptime){ 68 | ptime = interval; 69 | answer = name; 70 | } 71 | } 72 | } 73 | cout << answer << endl; 74 | return 0; 75 | } -------------------------------------------------------------------------------- /baekjoon/14503.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-05. 3 | // 4 | #include 5 | 6 | using namespace std; 7 | 8 | int map[51][51]; 9 | int visit[51][51]; 10 | int direction; //0 : 북, 1 : 동, 2 : 남, 3 : 서 11 | int dx[4] = {0,1,0,-1}; //북동남서 12 | int dy[4] = {-1,0,1,0}; //북동남서 13 | int N,M; 14 | int sol; 15 | int changeDirection(int a){ 16 | if(a == 0) 17 | return 3; 18 | else 19 | return a - 1; 20 | } 21 | int goback(int a){ 22 | if(a == 0) 23 | return 2; 24 | if(a == 1) 25 | return 3; 26 | if(a == 2) 27 | return 0; 28 | if(a == 3) 29 | return 1; 30 | } 31 | int main(){ 32 | int posy,posx; 33 | cin >> N >> M; 34 | cin >> posy >> posx >> direction ; 35 | for(int i = 1 ; i <= N ; i++) 36 | for(int j = 1 ; j <= M ; j++) 37 | cin >> map[i][j]; 38 | posx++; 39 | posy++; 40 | //초기 위치 청소 41 | sol++; 42 | visit[posy][posx] = 1; 43 | int nd,ny,nx; 44 | while(1){ 45 | //4방향을 검사했을 때 다 청소할수없는지 판단하는 변수 46 | bool chk = false; 47 | //4방향검사 48 | for(int i = 0 ; i < 4 ;i++) { 49 | //현재 방향의 왼쪽 = 현재 방향 - 1 (북 -> 서만 예외처리) 50 | nd = changeDirection(direction); 51 | ny = posy + dy[nd]; 52 | nx = posx + dx[nd]; 53 | //새로운 방향에 청소할게 있는지 검사 54 | //있다면 55 | //이동 후 청소하고 다시 4방향검사 56 | if(visit[ny][nx] == 0 && map[ny][nx] == 0){ 57 | posy = ny; 58 | posx = nx; 59 | direction = nd; 60 | sol++; 61 | visit[posy][posx] = 1; 62 | chk = true; 63 | break; 64 | } 65 | //없다면 66 | //방향을 회전하고 다음 왼쪽 방향 검사 67 | else{ 68 | direction = nd; 69 | } 70 | } 71 | //4방향을 다 청소할수없다면or 벽이라면 72 | if(!chk){ 73 | //방향 유지하고 후진을 하는데 74 | 75 | ny = posy + dy[goback(direction)]; 76 | nx = posx + dx[goback(direction)]; 77 | //벽이라 할수없다면 작동을 멈춤 78 | if(map[ny][nx] == 1){ 79 | break; 80 | } 81 | else{ 82 | posy = ny; 83 | posx = nx; 84 | } 85 | 86 | } 87 | } 88 | 89 | cout << sol << endl; 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /SW_EXPERT/2117_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-14. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | int T, N, M; 10 | int sol = 1; 11 | int home; 12 | int map[20][20]; 13 | int visit[20][20]; 14 | int dx[4] = {0, 0, -1, 1}; 15 | int dy[4] = {1, -1, 0, 0}; 16 | queue> q; 17 | 18 | void bfs(int t) { 19 | int nx, ny; 20 | pair tmp; 21 | int size = q.size(); 22 | while (size--) { 23 | tmp = q.front(); 24 | q.pop(); 25 | for (int i = 0; i < 4; i++) { 26 | nx = tmp.second + dx[i]; 27 | ny = tmp.first + dy[i]; 28 | if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visit[ny][nx]) { 29 | visit[ny][nx] = 1; 30 | q.push({ny, nx}); 31 | if (map[ny][nx] == 1) 32 | home++; 33 | } 34 | } 35 | } 36 | t++; 37 | //손해안봤으면 38 | if (t * t + (t - 1) * (t - 1) <= home * M) { 39 | if (sol < home) 40 | sol = home; 41 | } 42 | // cout <<"****************************"<> T; 80 | int a = 1; 81 | while (T--) { 82 | cin >> N >> M; 83 | for (int i = 0; i < N; i++) 84 | for (int j = 0; j < N; j++) 85 | cin >> map[i][j]; 86 | simulation(); 87 | cout << "#" << a++ << " " << sol << endl; 88 | sol = 1; 89 | home = 0; 90 | } 91 | return 0; 92 | } -------------------------------------------------------------------------------- /baekjoon/14502_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-24. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | struct xy { 11 | int y, x; 12 | }; 13 | vector vv; 14 | vector wv; 15 | int wvsize; 16 | int vvsize; 17 | queue q; 18 | int N, M; //세로, 가로 19 | int nmap[8][8]; 20 | int dx[4] = {0, 0, -1, 1}; 21 | int dy[4] = {-1, 1, 0, 0}; 22 | int sol; 23 | 24 | void makeQueue() { 25 | for (int i = 0; i < vvsize; i++) 26 | q.push(vv[i]); 27 | } 28 | 29 | void bfs() { 30 | xy tmp; 31 | int nx, ny; 32 | while (!q.empty()) { 33 | tmp = q.front(); 34 | q.pop(); 35 | for (int i = 0; i < 4; i++) { 36 | nx = tmp.x + dx[i]; 37 | ny = tmp.y + dy[i]; 38 | if (nmap[ny][nx] == 0 && nx >= 0 && nx < M && ny >= 0 && ny < N) { 39 | nmap[ny][nx] = 3; 40 | q.push({ny, nx}); 41 | } 42 | } 43 | } 44 | int mmax = 0; 45 | for (int i = 0; i < N; i++) { 46 | for (int j = 0; j < M; j++) { 47 | if (nmap[i][j] == 0) 48 | mmax++; 49 | else if (nmap[i][j] == 3) 50 | nmap[i][j] = 0; 51 | } 52 | } 53 | 54 | if (sol < mmax) 55 | sol = mmax; 56 | } 57 | 58 | int main() { 59 | vv.reserve(64); 60 | wv.reserve(64); 61 | cin >> N >> M; 62 | for (int i = 0; i < N; i++) { 63 | for (int j = 0; j < M; j++) { 64 | cin >> nmap[i][j]; 65 | if (nmap[i][j] == 2) { 66 | vv.push_back({i, j}); 67 | } else if (nmap[i][j] == 0) { 68 | wv.push_back({i, j}); 69 | } 70 | } 71 | } 72 | wvsize = wv.size(); 73 | vvsize = vv.size(); 74 | for (int i = 0; i 5 | using namespace std; 6 | 7 | int map[10][10]; //지도 : 0 빈칸 1 벽 2 바이러스 8 | int N,M; //세로,가로 9 | int arr[64][2]; //빈칸의 위치를 저장 10 | int cnt = 0; //빈칸의 갯수 11 | int sol = 0; //답 12 | 13 | void recursion(int i,int j){ 14 | if(i > 1 && map[i-1][j] == 0){ 15 | map[i-1][j] = 3; 16 | recursion(i-1,j); 17 | } 18 | if(i < N && map[i+1][j] == 0){ 19 | map[i+1][j] = 3; 20 | recursion(i+1,j); 21 | } 22 | if(j>1 && map[i][j-1] == 0){ 23 | map[i][j-1] = 3; 24 | recursion(i,j-1); 25 | } 26 | if(j> N >> M; 64 | 65 | for(int i = 1; i <= N ; i++ ){ 66 | for( int j = 1; j <= M ; j++) { 67 | cin >> map[i][j]; 68 | if (map[i][j] == 0) { 69 | arr[cnt][0] = i; 70 | arr[cnt][1] = j; 71 | cnt++; 72 | } 73 | } 74 | } 75 | 76 | for(int i = 0; i < cnt - 2 ; i++ ){ 77 | for(int j = i+1 ; j < cnt - 1 ; j++){ 78 | for(int k = j+1 ; k < cnt ; k++){ 79 | map[arr[i][0]][arr[i][1]] = 1; 80 | map[arr[j][0]][arr[j][1]] = 1; 81 | map[arr[k][0]][arr[k][1]] = 1; 82 | countVirus(); 83 | map[arr[i][0]][arr[i][1]] = 0; 84 | map[arr[j][0]][arr[j][1]] = 0; 85 | map[arr[k][0]][arr[k][1]] = 0; 86 | } 87 | } 88 | } 89 | cout << sol << endl; 90 | return 0; 91 | } -------------------------------------------------------------------------------- /baekjoon/14891.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-02-05. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | int arr[5][9]; 9 | int interm[5]; // -1 : 반시계, 1 : 시계, 0 : 홀드 10 | int visit[5]; 11 | int score; 12 | 13 | void direction(int num, int dir){ 14 | //cout<< "in the direction func"< 1 && visit[num-1] == 0){ 18 | if(arr[num-1][3] == arr[num][7]){ 19 | interm[num-1] = 0; 20 | } 21 | else{ 22 | interm[num-1] = (-1)*dir; 23 | direction(num-1,interm[num-1]); 24 | } 25 | 26 | } 27 | //오른쪽 28 | if(num < 4 && visit[num+1] == 0){ 29 | if(arr[num+1][7] == arr[num][3]){ 30 | interm[num+1] = 0; 31 | } 32 | else{ 33 | interm[num+1] = (-1)*dir; 34 | direction(num+1,interm[num+1]); 35 | } 36 | } 37 | }; 38 | 39 | void rotate(){ 40 | //cout<< "in the rotate func"< 1; j--){ 46 | arr[i][j] = arr[i][j-1]; 47 | } 48 | arr[i][1] = temp; 49 | } 50 | if(interm[i] == -1){ 51 | temp = arr[i][1]; 52 | for(int j = 1; j < 8; j++){ 53 | arr[i][j] = arr[i][j+1]; 54 | } 55 | arr[i][8] = temp; 56 | } 57 | } 58 | } 59 | 60 | void calc(){ 61 | //cout<< "in the calc func"<> s; 69 | if(s =='1') 70 | arr[i][j] = 1; 71 | else 72 | arr[i][j] = 0; 73 | } 74 | } 75 | int k; 76 | cin >> k; 77 | int num; 78 | int dir; 79 | while(k--){ 80 | cin >> num >> dir; 81 | interm[num] = dir; 82 | direction(num,dir); 83 | /*for(int i = 1 ; i < 5 ; i++) 84 | cout< 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | vector files = {"img0000312.png", "img0000210.png", "img0000102.png", "img0000001.png", "IMG0000001.GIF", 11 | "img0000002.JPG"}; 12 | //vector files = {"img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG"}; 13 | //vector files = {"F-5 Freedom Fighter", "B-50 Superfortress", "A-10 Thunderbolt II", "F-14 Tomcat"}; 14 | struct hnt{ 15 | string head; 16 | string number; 17 | }; 18 | string toUpper(string a) { 19 | for (int i = 0; i < a.size(); i++) { 20 | if (a[i] >= 'a' && a[i] <= 'z') 21 | a[i] = a[i] - 32; 22 | } 23 | return a; 24 | } 25 | 26 | bool cmp(pair a, pair b) { 27 | if(a.first.head < b.first.head) 28 | return true; 29 | else if(a.first.head > b.first.head) 30 | return false; 31 | else{ 32 | if(a.first.number < b.first.number) 33 | return true; 34 | else if(a.first.number >= b.first.number) 35 | return false; 36 | } 37 | 38 | } 39 | 40 | int main() { 41 | string name = ""; 42 | int start; 43 | int end; 44 | vector> nfiles; 45 | for (int i = 0; i < files.size(); i++) { 46 | name = files[i]; 47 | for (int i = 0; i < name.size(); i++) { 48 | if (name[i] >= '0' && name[i] <= '9') { 49 | start = i; 50 | end = i; 51 | break; 52 | } 53 | } 54 | for (int i = start + 1; i < name.size(); i++) { 55 | if (name[i] >= '0' && name[i] <= '9') { 56 | end++; 57 | if (end - start == 4) 58 | break; 59 | } else 60 | break; 61 | } 62 | string number = name.substr(start, end - start + 1); 63 | int size = number.size(); 64 | for (int i = 0; i < 5 - size; i++) { 65 | number = '0' + number; 66 | } 67 | nfiles.push_back({{toUpper(name.substr(0, start)), number}, name}); 68 | } 69 | 70 | stable_sort(nfiles.begin(), nfiles.end(), cmp); 71 | 72 | vector answer; 73 | for (int i = 0; i < nfiles.size(); i++) { 74 | answer.push_back(nfiles[i].second); 75 | } 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /baekjoon/14499_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-21. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int N,M,sx,sy,K; 10 | int dice[4][3]; //동 : [1][2] , 서 : [1][0] , 남 : [2][1] , 북 : [0][1] , 위 : [1][1] , 아래 [3][1] 11 | int map[20][20];//(y,x) 12 | queue q; 13 | //동(x++),서(x--),북(y--),남(y++) 14 | int dx[5] = {0,1,-1,0,0}; 15 | int dy[5] = {0,0,0,-1,1}; 16 | void east(){ 17 | int tmp = dice[1][1]; 18 | dice[1][1] = dice[1][0]; 19 | dice[1][0] = dice[3][1]; 20 | dice[3][1] = dice[1][2]; 21 | dice[1][2] = tmp; 22 | } 23 | 24 | void west(){ 25 | int tmp = dice[1][1]; 26 | dice[1][1] = dice[1][2]; 27 | dice[1][2] = dice[3][1]; 28 | dice[3][1] = dice[1][0]; 29 | dice[1][0] = tmp; 30 | } 31 | 32 | void north(){ 33 | int tmp = dice[0][1]; 34 | dice[0][1] = dice[1][1]; 35 | dice[1][1] = dice[2][1]; 36 | dice[2][1] = dice[3][1]; 37 | dice[3][1] = tmp; 38 | } 39 | 40 | void south(){ 41 | int tmp = dice[0][1]; 42 | dice[0][1] = dice[3][1]; 43 | dice[3][1] = dice[2][1]; 44 | dice[2][1] = dice[1][1]; 45 | dice[1][1] = tmp; 46 | } 47 | void move(){ 48 | int command; 49 | int ny,nx; 50 | while(!q.empty()){ 51 | command = q.front(); 52 | q.pop(); 53 | ny = sy+dy[command]; 54 | nx = sx+dx[command]; 55 | if(ny >= 0 && ny < N && nx >= 0 && nx < M){ 56 | switch(command){ 57 | case 1: 58 | east(); 59 | break; 60 | case 2: 61 | west(); 62 | break; 63 | case 3: 64 | north(); 65 | break; 66 | case 4: 67 | south(); 68 | break; 69 | } 70 | sx = nx; 71 | sy = ny; 72 | if(map[ny][nx]){ 73 | dice[3][1] = map[ny][nx]; 74 | map[ny][nx] = 0; 75 | } 76 | else 77 | map[ny][nx] = dice[3][1]; 78 | cout << dice[1][1] << '\n'; 79 | } 80 | //cout << ny <<","<> N >> M >> sy >> sx >> K; 85 | for(int i = 0 ; i < N ; i++) 86 | for(int j = 0 ; j < M ; j++) 87 | cin >> map[i][j]; 88 | int c; 89 | for(int i = 0 ; i < K ; i++){ 90 | cin >> c; 91 | q.push(c); 92 | } 93 | move(); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /baekjoon/1063.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-13. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int map[8][8]; 11 | int N; 12 | string command[50]; 13 | 14 | //R(x++),L(x--),B(y--),T(y++),RT,LT,RB,LB 15 | int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1}; 16 | int dy[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 17 | 18 | bool chk(int x, int y) { 19 | if (x >= 0 && x < 8 && y >= 0 && y < 8) 20 | return true; 21 | return false; 22 | } 23 | 24 | int main() { 25 | //A = 65 26 | //1 = 49 27 | string king; 28 | string stone; 29 | cin >> king >> stone >> N; 30 | for (int i = 0; i < N; i++) { 31 | cin >> command[i]; 32 | } 33 | //킹,스톤 표시 34 | int kx = king[0] - 65; 35 | int ky = king[1] - 49; 36 | int sx = stone[0] - 65; 37 | int sy = stone[1] - 49; 38 | map[king[1] - 49][king[0] - 65] = 1; 39 | map[stone[1] - 49][stone[0] - 65] = 2; 40 | int nx, ny; 41 | int d = 0; 42 | 43 | for (int i = 0; i < N; i++) { 44 | if (command[i] == "R") d = 0; 45 | else if (command[i] == "L") d = 1; 46 | else if (command[i] == "B") d = 2; 47 | else if (command[i] == "T") d = 3; 48 | else if (command[i] == "RT") d = 4; 49 | else if (command[i] == "LT") d = 5; 50 | else if (command[i] == "RB") d = 6; 51 | else d = 7; 52 | 53 | nx = kx + dx[d]; 54 | ny = ky + dy[d]; 55 | 56 | if (chk(nx, ny)) { 57 | //킹이 움직인 위치에 돌이 있으면 58 | if(map[ny][nx] == 2){ 59 | int nsx,nsy; 60 | nsx = sx + dx[d]; 61 | nsy = sy + dy[d]; 62 | //돌도 유효한 위치로 움직이게 되면 63 | if(chk(nsx,nsy)){ 64 | map[ky][kx] = 0; 65 | map[ny][nx] = 1; 66 | map[nsy][nsx] = 2; 67 | kx = nx; 68 | ky = ny; 69 | sx = nsx; 70 | sy = nsy; 71 | } 72 | } 73 | else{ 74 | map[ky][kx] = 0; 75 | map[ny][nx] = 1; 76 | kx = nx; 77 | ky = ny; 78 | } 79 | } 80 | // cout <<"**************************"< 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | int input; 10 | int output; 11 | 12 | struct s{ 13 | int v; 14 | string result = ""; 15 | int cnt = 0; 16 | }; 17 | queue q; 18 | s tmp; 19 | int visit[10001]; 20 | void bfs(){ 21 | int t,v1,v2,v3,v4; 22 | int n,time; 23 | string sol = ""; 24 | s front; 25 | front.v = input; 26 | front.cnt = 0; 27 | visit[input] = 1; 28 | q.push(front); 29 | while(!q.empty()){ 30 | front = q.front(); 31 | time = front.cnt; 32 | n = front.v; 33 | sol = front.result; 34 | if(front.v == output){ 35 | cout << front.result << "\n"; 36 | return; 37 | } 38 | q.pop(); 39 | t = n; 40 | v1 = t/1000; 41 | t %= 1000; 42 | v2 = t/100; 43 | t %= 100; 44 | v3 = t/10; 45 | t %= 10; 46 | v4 = t; 47 | //D 48 | int aNum = (2*n)%10000; 49 | if(!visit[aNum]) { 50 | tmp.v = aNum; 51 | tmp.result = sol + "D"; 52 | tmp.cnt = time + 1; 53 | visit[tmp.v] = 1; 54 | q.push(tmp); 55 | } 56 | //S 57 | aNum = n - 1; 58 | if(aNum == -1) 59 | tmp.v = 9999; 60 | else 61 | tmp.v = aNum; 62 | if(!visit[aNum]) { 63 | tmp.cnt = time + 1; 64 | tmp.result = sol + "S"; 65 | visit[tmp.v] = 1; 66 | q.push(tmp); 67 | } 68 | //R 69 | if(!visit[1000*v4 + 100*v1 + 10*v2 + v3]) { 70 | tmp.v = 1000 * v4 + 100 * v1 + 10 * v2 + v3; 71 | tmp.result = sol + "R"; 72 | tmp.cnt = time + 1; 73 | visit[tmp.v] = 1; 74 | q.push(tmp); 75 | } 76 | //L 77 | if(!visit[1000*v2 + 100*v3 + 10*v4 + v1]) { 78 | tmp.v = 1000 * v2 + 100 * v3 + 10 * v4 + v1; 79 | tmp.result = sol + "L"; 80 | tmp.cnt = time + 1; 81 | visit[tmp.v] = 1; 82 | q.push(tmp); 83 | } 84 | } 85 | } 86 | 87 | int main(){ 88 | ios_base::sync_with_stdio(false); 89 | cin.tie(NULL); 90 | int T; 91 | cin >> T; 92 | while(T--){ 93 | cin >> input >> output; 94 | bfs(); 95 | while(!q.empty()) 96 | q.pop(); 97 | fill_n(visit,10001,0); 98 | } 99 | return 0; 100 | } -------------------------------------------------------------------------------- /baekjoon/1062.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-13. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | int N, K; 11 | int visit[50][26]; 12 | int chk[26]; 13 | vector combination; 14 | int c[26]; 15 | int sol = 0; 16 | vector v; 17 | 18 | void calc() { 19 | combination.clear(); 20 | combination = {0,2,8,13,19}; 21 | for(int i = 0 ; i < K - 5 ; i++) 22 | combination.push_back(c[i]); 23 | sort(combination.begin(),combination.end()); 24 | // cout << "*****************************"<> N >> K; 71 | //최소개수도 못가르치면 0개 72 | if (K < 5) { 73 | cout << 0 << endl; 74 | return 0; 75 | } 76 | 77 | for (int i = 0; i < N; i++) { 78 | cin >> tmp; 79 | for (int j = 0; j < tmp.size(); j++) { 80 | visit[i][tmp[j] - 97] = 1; 81 | chk[tmp[j] - 97] = 1; 82 | } 83 | } 84 | //cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"< 6 | #include 7 | using namespace std; 8 | 9 | int map[101][101]; 10 | int N,K,L; 11 | //시계 반대 방향 12 | int dx[4] = {1,0,-1,0}; 13 | int dy[4] = {0,1,0,-1}; 14 | struct sd{ 15 | int sec = 0; 16 | char dir = 'D'; 17 | }; 18 | struct tailD{ 19 | int x,y; 20 | int d; 21 | }; 22 | queue q; 23 | queue tdq; 24 | int main(){ 25 | cin >> N >> K; 26 | int x,y; 27 | for(int i = 0 ; i < K ; i++){ 28 | cin >> y >> x; 29 | map[y][x] = 1; 30 | } 31 | cin >> L; 32 | sd temp; 33 | for(int i = 0 ; i < L ; i++) { 34 | cin >> temp.sec >> temp.dir; 35 | q.push(temp); 36 | } 37 | map[1][1] = 2; 38 | int time = 0; 39 | int direction = 0; //오른쪽 40 | int directionTail = 0; 41 | int headx = 1; 42 | int heady = 1; 43 | int tailx = 1; 44 | int taily = 1; 45 | while(1){ 46 | sd tmp = q.front(); 47 | //시간이 되면 방향을 변경 48 | //시간이 안되었으면 뱡향 유지 49 | if(tmp.sec == time){ 50 | q.pop(); 51 | if(tmp.dir == 'L'){ 52 | if(direction == 0) 53 | direction = 3; 54 | else 55 | direction--; 56 | } 57 | else{ 58 | if(direction == 3) 59 | direction = 0; 60 | else 61 | direction++; 62 | } 63 | tailD td; 64 | td.y = heady; 65 | td.x = headx; 66 | td.d = direction; 67 | tdq.push(td); 68 | } 69 | tailD tmp_td = tdq.front(); 70 | if(tmp_td.x == tailx &&tmp_td.y == taily){ 71 | directionTail = tmp_td.d; 72 | tdq.pop(); 73 | } 74 | //머리를 늘려 다음칸에 위치 75 | headx += dx[direction]; 76 | heady += dy[direction]; 77 | //정상범위에 있고, 자기자신을 만나지않았을때 78 | if(headx > 0 && headx <= N && heady > 0 && heady <= N && map[heady][headx] != 2){ 79 | //사과가 있을 때 -> 머리만 이동하고 꼬리 그대로 80 | if(map[heady][headx] == 1){ 81 | map[heady][headx] = 2; 82 | } 83 | //사과가 없을 때 84 | else{ 85 | map[heady][headx] = 2; 86 | map[taily][tailx] = 0; 87 | tailx += dx[directionTail]; 88 | taily += dy[directionTail]; 89 | } 90 | } 91 | //게임 끝 92 | else{ 93 | cout << time+1 << endl; 94 | return 0; 95 | } 96 | time++; 97 | } 98 | return 0; 99 | } -------------------------------------------------------------------------------- /SW_EXPERT/5648_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-17. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | struct atom { 10 | int y, x, d, k, crash; 11 | }; 12 | //vector atoms; 13 | atom atoms[1000]; 14 | int map[4001][4001]; 15 | bool visit[4001][4001]; 16 | int T, N, sol; 17 | int dx[4] = {0, 0, -1, 1}; 18 | int dy[4] = {1, -1, 0, 0}; 19 | 20 | void simulation() { 21 | int ny, nx; 22 | int cnt = 0; 23 | while (cnt != N) { 24 | for (int i = 0; i < N; i++) { 25 | if (!atoms[i].crash) { 26 | nx = atoms[i].x + dx[atoms[i].d]; 27 | ny = atoms[i].y + dy[atoms[i].d]; 28 | 29 | map[atoms[i].y][atoms[i].x]--; 30 | if (map[atoms[i].y][atoms[i].x] <= 1) 31 | visit[atoms[i].y][atoms[i].x] = false; 32 | 33 | //out of bound 34 | if (nx < 0 || ny < 0 || nx > 4000 || ny > 4000) { 35 | atoms[i].crash = 1; 36 | cnt++; 37 | continue; 38 | } 39 | //in bound 40 | else { 41 | atoms[i].x = nx; 42 | atoms[i].y = ny; 43 | map[ny][nx]++; 44 | if (map[ny][nx] > 1) 45 | visit[ny][nx] = true; 46 | } 47 | } 48 | } 49 | for (int i = 0; i < N; i++) { 50 | if (!atoms[i].crash) { 51 | //충돌지역이면 52 | if (visit[atoms[i].y][atoms[i].x]) { 53 | atoms[i].crash = 1; 54 | map[atoms[i].y][atoms[i].x]--; 55 | //마지막 원소이면 56 | if (!map[atoms[i].y][atoms[i].x]) { 57 | visit[atoms[i].y][atoms[i].x] = false; 58 | } 59 | sol += atoms[i].k; 60 | cnt++; 61 | } 62 | } 63 | } 64 | //cout << cnt << endl; 65 | } 66 | } 67 | 68 | void init() { 69 | sol = 0; 70 | } 71 | 72 | int main() { 73 | ios_base::sync_with_stdio(false); 74 | cin.tie(NULL); 75 | cin >> T; 76 | int a = 1; 77 | int x, y, d, k; 78 | while (T--) { 79 | cin >> N; 80 | for (int i = 0; i < N; i++) { 81 | cin >> x >> y >> d >> k; 82 | atoms[i] = {2 * (y + 1000), 2 * (x + 1000), d, k, 0}; 83 | map[2 * (y + 1000)][2 * (x + 1000)] = 1; 84 | } 85 | simulation(); 86 | cout << "#" << a++ << " " << sol << '\n'; 87 | init(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /kakao/traffic.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-03. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | struct t { 12 | long start; 13 | long end; 14 | }; 15 | vector lines = {"2016-09-15 20:59:57.421 0.351s", "2016-09-15 20:59:58.233 1.181s", 16 | "2016-09-15 20:59:58.299 0.8s", "2016-09-15 20:59:58.688 1.041s", 17 | "2016-09-15 20:59:59.591 1.412s", "2016-09-15 21:00:00.464 1.466s", 18 | "2016-09-15 21:00:00.741 1.581s", "2016-09-15 21:00:00.748 2.31s", 19 | "2016-09-15 21:00:00.966 0.381s", "2016-09-15 21:00:02.066 2.62s"}; 20 | 21 | //vector lines = {"2016-09-15 01:00:04.002 2.0s", "2016-09-15 01:00:07.000 2s"}; 22 | 23 | 24 | vector list; 25 | 26 | void makeList() { 27 | for (int i = 0; i < lines.size(); i++) { 28 | string str = lines[i]; 29 | long hour = stol(str.substr(11)); 30 | long min = stol(str.substr(14)); 31 | long sec = stol(str.substr(17, 2)); 32 | long msec = stol(str.substr(20)); 33 | long time = (long) (stod(str.substr(24)) * 1000.0); 34 | long end = hour * 60 * 60 * 1000 + min * 60 * 1000 + sec * 1000 + msec; 35 | long start = end - time + 1; 36 | // cout << str << endl << hour << ":" << min << ":" <= jstart && istart <= jend) || (istart <= jstart && istart + 999 >= jend) || 58 | (istart + 999 >= jstart && istart + 999 <= jend)) { 59 | mlsol++; 60 | } 61 | 62 | if ((iend >= jstart && iend <= jend) || (iend <= jstart && iend + 999 >= jend) || 63 | (iend + 999 >= jstart && iend + 999 <= jend)) { 64 | mrsol++; 65 | } 66 | } 67 | if (mlsol > mrsol) 68 | msol = mlsol; 69 | else 70 | msol = mrsol; 71 | if (msol > answer) 72 | answer = msol; 73 | } 74 | cout << answer << endl; 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /baekjoon/5430.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-03-14. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int T; 12 | cin >> T; 13 | 14 | string c; 15 | int n; 16 | char a; 17 | int input; 18 | while (T--) { 19 | deque dq; 20 | cin >> c; 21 | cin >> n; 22 | if (n == 0) { 23 | cin >> a; 24 | cin >> a; 25 | } 26 | else if (n == 1) { 27 | cin >> a; 28 | cin >> input; 29 | cin >> a; 30 | dq.push_back(input); 31 | } 32 | else { 33 | char ba; 34 | cin >> ba; 35 | cin >> input; 36 | dq.push_back(input); 37 | for (int i = 1; i < n; i++) { 38 | cin >> a; 39 | cin >> input; 40 | dq.push_back(input); 41 | } 42 | char ab; 43 | cin >> ab; 44 | } 45 | bool front = true; 46 | bool error = false; 47 | for (int i = 0; i < c.length(); i++) { 48 | if (c[i] == 'R'){ 49 | front = !front; 50 | } 51 | else { 52 | if (dq.empty()) { 53 | cout << "error" << endl; 54 | error = true; 55 | break; 56 | } else { 57 | if (front) 58 | dq.pop_front(); 59 | else 60 | dq.pop_back(); 61 | } 62 | } 63 | } 64 | if(error) { 65 | continue; 66 | } 67 | if(dq.size() == 0){ 68 | cout<<"[]"< 2 | #include 3 | using namespace std; 4 | 5 | int dx[4] = {0,0,1,-1}; 6 | int dy[4] = {1,-1,0,0}; 7 | 8 | struct strct{ 9 | int cnt; 10 | int rx,ry,bx,by; 11 | }; 12 | 13 | queue q; 14 | 15 | int visit[11][11][11][11]; 16 | char map[12][12]; 17 | 18 | int main(){ 19 | int N,M; 20 | cin >> N >> M; 21 | int rx,ry,bx,by; 22 | for(int i = 0 ; i < N ; i++){ 23 | for(int j = 0 ; j < M ; j++){ 24 | cin >> map[i][j]; 25 | if(map[i][j] == 'R') { 26 | rx = i; 27 | ry = j; 28 | } 29 | 30 | if(map[i][j] == 'B') { 31 | bx = i; 32 | by = j; 33 | } 34 | } 35 | } 36 | 37 | visit[rx][ry][bx][by] = 1; 38 | q.push({0,rx,ry,bx,by}); 39 | //BFS 40 | while(q.size()){ 41 | strct temp = q.front(); 42 | q.pop(); 43 | for(int i = 0 ; i < 4 ; i++){ 44 | int nrx = temp.rx; 45 | int nry = temp.ry; 46 | int nbx = temp.bx; 47 | int nby = temp.by; 48 | int cr = 0; 49 | int cb = 0; 50 | //R 움직임 51 | while(map[nrx+dx[i]][nry+dy[i]] != '#' && map[nrx][nry] != 'O'){ 52 | nrx += dx[i]; 53 | nry += dy[i]; 54 | cr++; 55 | } 56 | //B 움직임 57 | while(map[nbx+dx[i]][nby+dy[i]] != '#' && map[nbx][nby] != 'O'){ 58 | nbx += dx[i]; 59 | nby += dy[i]; 60 | cb++; 61 | } 62 | //R,B가 같은 위치에 있을 때 63 | if(nrx == nbx && nry == nby){ 64 | //실패 65 | if(map[nrx][nry] == 'O') 66 | continue; 67 | //우선순위로 판단 68 | if(cb > cr){ 69 | nbx -= dx[i]; 70 | nby -= dy[i]; 71 | } 72 | else{ 73 | nrx -= dx[i]; 74 | nry -= dy[i]; 75 | } 76 | } 77 | //실패 78 | if(map[nbx][nby] == 'O') { 79 | continue; 80 | } 81 | //성공 답 출력 82 | if(map[nrx][nry] == 'O'){ 83 | cout < 2 | #include 3 | #include 4 | using namespace std; 5 | int map[101][101][2]; 6 | int visit[101][101]; 7 | int N; 8 | int cnt; //섬 라벨링에 필요 9 | struct xy{ 10 | int y,x; 11 | }; 12 | int dx[4] = {1,-1,0,0}; 13 | int dy[4] = {0,0,1,-1}; 14 | queue q; 15 | vector v[5001]; 16 | int sol = 99999999; 17 | void label(){ 18 | xy front; 19 | int ny,nx; 20 | bool chk; 21 | while(!q.empty()){ 22 | front = q.front(); 23 | q.pop(); 24 | chk = false; 25 | for(int i = 0 ; i < 4; i++){ 26 | ny = front.y + dy[i]; 27 | nx = front.x + dx[i]; 28 | //ny,nx가 유효한 좌표면 29 | if(nx > 0 && nx < N+1 && ny > 0 && ny < N+1){ 30 | if(!chk){ 31 | //바다와 인접한 육지이면 32 | if(map[ny][nx][0] == 0){ 33 | chk = true; 34 | v[cnt].push_back({front.y,front.x}); 35 | } 36 | } 37 | //같은 섬의 육지를 찾으면 38 | if(map[ny][nx][0] == 1 && visit[ny][nx] == 0) { 39 | map[ny][nx][0] = cnt; 40 | visit[ny][nx] = 1; 41 | q.push({ny, nx}); 42 | } 43 | } 44 | 45 | } 46 | } 47 | } 48 | int main(){ 49 | cin >> N; 50 | //map 입력 51 | for(int i = 1; i <= N ; i++) 52 | for(int j = 1; j <= N ; j++) 53 | cin >> map[i][j][0]; 54 | //섬 라벨링 55 | for(int i = 1; i <= N ; i++){ 56 | for(int j = 1; j <= N ; j++){ 57 | //방문하지않았고, 바다가 아닌 육지이면 BFS 58 | if(visit[i][j] == 0 && map[i][j][0] == 1){ 59 | cnt++; 60 | visit[i][j] = 1; 61 | map[i][j][0] = cnt; 62 | q.push({i,j}); 63 | label(); 64 | } 65 | } 66 | } 67 | 68 | //최단 다리 계산 69 | int ix,iy; 70 | int jx,jy; 71 | int difx,dify; 72 | for(int i = 1; i < cnt ; i++){ 73 | for(int j = i+1; j < cnt+1 ; j++){ 74 | for(auto ixy : v[i]){ 75 | ix = ixy.x; 76 | iy = ixy.y; 77 | for(auto jxy : v[j]){ 78 | jx = jxy.x; 79 | jy = jxy.y; 80 | difx = ix - jx; 81 | dify = iy - jy; 82 | if(difx < 0) 83 | difx = -(difx); 84 | if(dify < 0) 85 | dify = -(dify); 86 | if(sol > difx + dify - 1) 87 | sol = difx + dify - 1; 88 | } 89 | } 90 | } 91 | } 92 | 93 | cout << sol << endl; 94 | 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /baekjoon/14499.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-03. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | int map[21][21]; 8 | int order[1001]; // 1 : 동, 2 : 서, 3 : 북, 4 : 남 9 | int dice[5][4]; 10 | int dx[5] = {0,1,-1,0,0}; 11 | int dy[5] = {0,0,0,-1,1}; 12 | 13 | int N,M; //세로,가로 14 | int dicex,dicey; //주사위 위치(y부터 받자) 15 | int K; //명령 개수 16 | 17 | void east(){ 18 | //주사위 모양 변경 19 | int tmp = dice[4][2]; 20 | dice[4][2] = dice[2][3]; 21 | for(int i = 3 ; i > 1 ; i--) 22 | dice[2][i] = dice[2][i-1]; 23 | dice[2][1] = tmp; 24 | } 25 | 26 | void west(){ 27 | //주사위 모양 변경 28 | int tmp = dice[4][2]; 29 | dice[4][2] = dice[2][1]; 30 | for(int i = 1 ; i < 3 ; i++) 31 | dice[2][i] = dice[2][i+1]; 32 | dice[2][3] = tmp; 33 | } 34 | 35 | void north(){ 36 | //주사위 모양 변경 37 | int tmp = dice[1][2]; 38 | for(int i = 1 ; i <= 3 ; i++) 39 | dice[i][2] = dice[i+1][2]; 40 | dice[4][2] = tmp; 41 | } 42 | 43 | void south(){ 44 | //주사위 모양 변경 45 | int tmp = dice[4][2]; 46 | for(int i = 4 ; i > 1 ; i--) 47 | dice[i][2] = dice[i-1][2]; 48 | dice[1][2] = tmp; 49 | } 50 | int main(){ 51 | cin >> N >> M >> dicey >> dicex >> K; 52 | for(int i = 1 ; i <= N ; i++) 53 | for(int j = 1 ; j <= M ; j++) 54 | cin >> map[i][j]; 55 | for(int i = 1 ; i <= K ; i++) 56 | cin >> order[i]; 57 | 58 | dicex++; 59 | dicey++; 60 | 61 | //주사위 위치 62 | int ndx; 63 | int ndy; 64 | for(int i = 1 ; i <= K ; i++){ 65 | ndx = dicex + dx[order[i]]; 66 | ndy = dicey + dy[order[i]]; 67 | //맵 벗어나면 명령 수행X 68 | if(ndx <= 0 || ndx > M || ndy <= 0 || ndy > N) 69 | continue; 70 | 71 | dicex += dx[order[i]]; 72 | dicey += dy[order[i]]; 73 | switch(order[i]){ 74 | //동 75 | case 1 : 76 | east(); 77 | break; 78 | //서 79 | case 2 : 80 | west(); 81 | break; 82 | //북 83 | case 3 : 84 | north(); 85 | break; 86 | //남 87 | default: 88 | south(); 89 | break; 90 | } 91 | //주사위 <-> 맵 복사 92 | //칸이 0이면 주사위 바닥 숫자를 칸으로 93 | if(map[dicey][dicex] == 0 ) { 94 | map[dicey][dicex] = dice[4][2]; 95 | } 96 | //칸이 0이 아니면 주사위에 칸을 복사하고 칸을 0으로 97 | else{ 98 | dice[4][2] = map[dicey][dicex]; 99 | map[dicey][dicex] = 0; 100 | } 101 | cout << dice[2][2] << endl; 102 | } 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /SW_EXPERT/4013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int T, K; 5 | int arr[5][9]; 6 | int directionArr[5]; 7 | int visit[5]; 8 | int sol = 0; 9 | int num = 0; 10 | int d = 1; 11 | 12 | void leftshift(int num) { 13 | int temp = arr[num][1]; 14 | for (int i = 1; i < 8; i++) 15 | arr[num][i] = arr[num][i + 1]; 16 | arr[num][8] = temp; 17 | } 18 | void rightshift(int num) { 19 | int temp = arr[num][8]; 20 | for (int i = 8; i > 1; i--) 21 | arr[num][i] = arr[num][i - 1]; 22 | arr[num][1] = temp; 23 | } 24 | void rotate() { 25 | //정해진 방향에 따라 shift 26 | for (int i = 1; i < 5; i++) { 27 | if (directionArr[i] == 1) 28 | rightshift(i); 29 | if (directionArr[i] == -1) 30 | leftshift(i); 31 | } 32 | } 33 | 34 | void setDirection(int num, int direction) { 35 | int left = num - 1; 36 | int right = num + 1; 37 | //왼쪽, 오른쪽 방향 결정 38 | if (left >= 1 && visit[left] == 0) { 39 | //같은 극이면 안돌고 40 | if (arr[num][7] == arr[left][3]) { 41 | visit[left] = 1; 42 | directionArr[left] = 0; 43 | setDirection(left, 0); 44 | } 45 | //다른 극이면 반대로 돌고 46 | else { 47 | visit[left] = 1; 48 | directionArr[left] = -1 * direction; 49 | setDirection(left, -1 * direction); 50 | } 51 | } 52 | 53 | if (right < 5 && visit[right] == 0) { 54 | //같은 극이면 안돌고 55 | if (arr[num][3] == arr[right][7]) { 56 | visit[right] = 1; 57 | directionArr[right] = 0; 58 | setDirection(right, 0); 59 | } 60 | //다른 극이면 반대로 돌고 61 | else { 62 | visit[right] = 1; 63 | directionArr[right] = -1 * direction; 64 | setDirection(right, -1 * direction); 65 | } 66 | } 67 | 68 | } 69 | 70 | void score() { 71 | //점수 계산 72 | int d = 1; 73 | for (int i = 1; i < 5; i++) { 74 | sol += (d*arr[i][1]); 75 | d *= 2; 76 | } 77 | } 78 | 79 | int main() { 80 | cin >> T; 81 | int C = 1; 82 | while(T--){ 83 | cin >> K; 84 | for (int i = 1; i < 5; i++) 85 | for (int j = 1; j < 9; j++) 86 | cin >> arr[i][j]; 87 | 88 | while (K--) { 89 | cin >> num; 90 | cin >> d; 91 | directionArr[num] = d; 92 | visit[num] = 1; 93 | setDirection(num, d); 94 | rotate(); 95 | fill_n(visit, 5, 0); 96 | } 97 | score(); 98 | cout <<"#"<< C <<" "<< sol << endl; 99 | sol = 0; 100 | C++; 101 | } 102 | return 0; 103 | } -------------------------------------------------------------------------------- /baekjoon/1963.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-06. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | int arr[1061][1061]; 11 | int visit[1061]; 12 | vector v; 13 | int sol; 14 | struct xx{ 15 | int v,d; 16 | }; 17 | queueq; 18 | 19 | void makeGraph(){ 20 | bool chk; 21 | for(int i = 1000 ; i < 10000 ; i++){ 22 | chk = true; 23 | for(int k = 2 ; k < i ; k++){ 24 | if(i % k == 0){ 25 | chk = false; 26 | break; 27 | } 28 | } 29 | if(chk) { 30 | v.push_back(i); 31 | } 32 | } 33 | int a; 34 | int b,c; 35 | for(int i = 0 ; i < 1061 ; i++){ 36 | for(int j = i+1 ; j < 1061 ; j++){ 37 | a = 0; 38 | b = v[i]; 39 | c = v[j]; 40 | if(b / 1000 == c / 1000) 41 | a++; 42 | b %=1000; 43 | c %=1000; 44 | if(b / 100 == c / 100) 45 | a++; 46 | b %=100; 47 | c %=100; 48 | if(b / 10 == c / 10) 49 | a++; 50 | b %=10; 51 | c %=10; 52 | if(b == c) 53 | a++; 54 | if(a == 3){ 55 | arr[i][j] = 1; 56 | arr[j][i] = 1; 57 | } 58 | } 59 | } 60 | } 61 | 62 | void bfs(int target){ 63 | while(!q.empty()){ 64 | xx tmp = q.front(); 65 | q.pop(); 66 | for(int i = 0 ; i < 1061 ; i++){ 67 | if(arr[tmp.v][i] == 1 && visit[i] == 0){ 68 | if(i ==target){ 69 | sol = tmp.d + 1; 70 | return; 71 | } 72 | visit[i] = 1; 73 | q.push({i,tmp.d+1}); 74 | } 75 | } 76 | } 77 | sol = -1; 78 | return; 79 | } 80 | int main(){ 81 | makeGraph(); 82 | int T; 83 | cin >> T; 84 | int n1,n2; 85 | while(T--){ 86 | int v1,v2; 87 | cin >> n1 >> n2; 88 | if(n1 == n2 ){ 89 | cout << 0 << endl; 90 | continue; 91 | } 92 | for(int i = 0 ; i < 1061 ; i++){ 93 | if(v[i] == n1) 94 | v1 = i; 95 | if(v[i] == n2) 96 | v2 = i; 97 | } 98 | visit[v1] = 1; 99 | q.push({v1,0}); 100 | bfs(v2); 101 | if(sol == -1){ 102 | cout <<"Impossible"< 6 | #include 7 | 8 | using namespace std; 9 | 10 | int T, N, M, L; 11 | int R, C; //세로,가로 12 | int sol = 1; 13 | int map[50][50]; 14 | int visit[50][50]; 15 | int p[8][4] = {{}, 16 | {1, 1, 1, 1}, 17 | {1, 1, 0, 0}, 18 | {0, 0, 1, 1}, 19 | {1, 0, 0, 1}, 20 | {0, 1, 0, 1}, 21 | {0, 1, 1, 0}, 22 | {1, 0, 1, 0}}; 23 | //상,하,좌,우 (y--,y++,x--,x++) 24 | int dx[4] = {0, 0, -1, 1}; 25 | int dy[4] = {-1, 1, 0, 0}; 26 | queue> q; 27 | 28 | void simulation() { 29 | int t = 1; 30 | int size, type, nx, ny; 31 | int from; 32 | pair tmp; 33 | while (t < L) { 34 | size = q.size(); 35 | while (size--) { 36 | tmp = q.front(); 37 | q.pop(); 38 | type = map[tmp.first][tmp.second]; 39 | for (int i = 0; i < 4; i++) { 40 | //움직일수있는 방향이면 41 | if (p[type][i]) { 42 | ny = tmp.first + dy[i]; 43 | nx = tmp.second + dx[i]; 44 | //유효한 점이면 45 | //범위 안벗어나고, 구조물있고 46 | if (ny >= 0 && ny < N && nx >= 0 && nx < M && map[ny][nx] && !visit[ny][nx]) { 47 | if (i == 0) from = 1; 48 | else if (i == 1) from = 0; 49 | else if (i == 2) from = 3; 50 | else from = 2; 51 | //이동할 방향의 구조물이 갈수있는 구조물이면! 52 | if (p[map[ny][nx]][from]) { 53 | visit[ny][nx] = 1; 54 | q.push({ny, nx}); 55 | sol++; 56 | } 57 | } 58 | } 59 | } 60 | } 61 | t++; 62 | // cout<> nq; 78 | swap(q,nq); 79 | } 80 | int main() { 81 | cin >> T; 82 | int a = 1; 83 | while (T--) { 84 | cin >> N >> M >> R>>C>>L; 85 | for (int i = 0; i < N; i++) 86 | for (int j = 0; j < M; j++) 87 | cin >> map[i][j]; 88 | 89 | 90 | q.push({R, C}); 91 | visit[R][C] = 1; 92 | simulation(); 93 | cout <<"#"< 2 | #include 3 | #include 4 | using namespace std; 5 | int N, M,sol; //세로 가로 6 | int map[8][8]; 7 | int visit[8][8]; 8 | int dx[4] = { 0,0,-1,1 }; 9 | int dy[4] = { 1,-1,0,0 }; 10 | 11 | vector> w; 12 | vector> v; 13 | 14 | void printMap() { 15 | cout << "*****************************" << endl; 16 | for (int i = 0; i < N; i++) { 17 | for (int j = 0; j < M; j++) { 18 | cout << map[i][j] << " "; 19 | } 20 | cout << endl; 21 | } 22 | 23 | } 24 | void bfs() { 25 | queue> q; 26 | pair tmp; 27 | int nx, ny; 28 | for (int i = 0; i < v.size(); i++) { 29 | q.push(v[i]); 30 | visit[v[i].first][v[i].second] = 1; 31 | } 32 | //printMap(); 33 | while (!q.empty()) { 34 | tmp = q.front(); 35 | q.pop(); 36 | for (int i = 0; i < 4; i++) { 37 | nx = tmp.second + dx[i]; 38 | ny = tmp.first + dy[i]; 39 | if (nx >= 0 && nx < M && ny >= 0 && ny < N && !visit[ny][nx] && map[ny][nx] == 0) { 40 | map[ny][nx] = 3; 41 | visit[ny][nx] = 1; 42 | q.push({ ny,nx }); 43 | } 44 | } 45 | } 46 | //printMap(); 47 | } 48 | 49 | void countVirus() { 50 | int msol = 0; 51 | for (int i = 0; i < N; i++) { 52 | for (int j = 0; j < M; j++) { 53 | if (map[i][j] == 0) 54 | msol++; 55 | else if (map[i][j] == 3) 56 | map[i][j] = 0; 57 | } 58 | } 59 | if (sol < msol) 60 | sol = msol; 61 | } 62 | 63 | void init() { 64 | for (int i = 0; i < N; i++) 65 | for (int j = 0; j < M; j++) 66 | visit[i][j] = 0; 67 | } 68 | int main() { 69 | ios_base::sync_with_stdio(false); 70 | cin.tie(NULL); 71 | cin >> N >> M; 72 | for (int i = 0; i < N; i++) { 73 | for (int j = 0; j < M; j++) { 74 | cin >> map[i][j]; 75 | if (map[i][j] == 0) 76 | w.push_back({ i,j }); 77 | else if (map[i][j] == 2) 78 | v.push_back({ i,j }); 79 | } 80 | } 81 | for (int i = 0; i < w.size() - 2; i++) { 82 | for (int j = i + 1; j < w.size() - 1; j++) { 83 | for (int k = j + 1; k < w.size(); k++) { 84 | map[w[i].first][w[i].second] = 1; 85 | map[w[j].first][w[j].second] = 1; 86 | map[w[k].first][w[k].second] = 1; 87 | bfs(); 88 | countVirus(); 89 | init(); 90 | map[w[i].first][w[i].second] = 0; 91 | map[w[j].first][w[j].second] = 0; 92 | map[w[k].first][w[k].second] = 0; 93 | } 94 | } 95 | } 96 | cout << sol << endl; 97 | return 0; 98 | 99 | } -------------------------------------------------------------------------------- /baekjoon/3190_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-09-21. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int N; //보드의 크기 12 | int K; //사과의 개수 13 | int L; //방향 변환 횟수 14 | int map[101][101]; // 1 : 사과, 2 : 뱀 15 | char dmap[101][101];//꼬리 방향 변경 지점 체크 16 | int t = 0; //시간 17 | //하(y--)우(x++)상(y++)좌(x--) 18 | int dx[4] = {0, 1, 0, -1}; 19 | int dy[4] = {-1, 0, 1, 0}; 20 | int curhdir = 1; 21 | int curtdir = 1; 22 | queue> change; 23 | 24 | bool chkPoint(int x, int y) { 25 | if (x > 0 && x <= N && y > 0 && y <= N && map[y][x] != 2) 26 | return true; 27 | return false; 28 | } 29 | 30 | 31 | void move() { 32 | int hx, hy, tx, ty; 33 | hx = hy = tx = ty = 1; 34 | int ntx, nty, nhx, nhy; 35 | map[1][1] = 2; 36 | while (1) { 37 | //머리 방향 변경 38 | if (change.front().first == t) { 39 | if (change.front().second == 'D') { 40 | if (curhdir != 3) curhdir++; 41 | else curhdir = 0; 42 | } else { 43 | if (curhdir != 0) curhdir--; 44 | else curhdir = 3; 45 | } 46 | dmap[hy][hx] = change.front().second; 47 | change.pop(); 48 | } 49 | //꼬리 방향 변경 50 | if(dmap[ty][tx] == 'L'){ 51 | if (curtdir != 0) curtdir--; 52 | else curtdir = 3; 53 | dmap[ty][tx] = 'X'; 54 | } 55 | if(dmap[ty][tx] == 'D'){ 56 | if (curtdir != 3) curtdir++; 57 | else curtdir = 0; 58 | dmap[ty][tx] = 'X'; 59 | } 60 | //바꾸고 이동시작 61 | nhx = hx + dx[curhdir]; 62 | nhy = hy + dy[curhdir]; 63 | ntx = tx + dx[curtdir]; 64 | nty = ty + dy[curtdir]; 65 | 66 | //유효한 범위면 이동 67 | if (chkPoint(nhx, nhy)) { 68 | //사과가 있으면 head만 증가 69 | if(map[nhy][nhx] == 1){ 70 | map[nhy][nhx] = 2; 71 | hx = nhx; 72 | hy = nhy; 73 | } 74 | //없으면 tail도 증가 75 | else{ 76 | map[nhy][nhx] = 2; 77 | map[ty][tx] = 0; 78 | hx = nhx; 79 | hy = nhy; 80 | tx = ntx; 81 | ty = nty; 82 | } 83 | } else { 84 | t++; 85 | break; 86 | } 87 | t++; 88 | } 89 | } 90 | 91 | 92 | int main() { 93 | cin >> N >> K; 94 | int y, x; 95 | for (int i = 0; i < K; i++) { 96 | cin >> y >> x; 97 | map[y][x] = 1; 98 | } 99 | char a; 100 | cin >> L; 101 | for (int i = 0; i < L; i++) { 102 | cin >> y >> a; 103 | change.push(make_pair(y, a)); 104 | } 105 | 106 | move(); 107 | cout << t << endl; 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /baekjoon/10026.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-06. 3 | // 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct xy{ 9 | int x,y; 10 | }; 11 | 12 | int arr[101][101]; //0 : R, 1 : G, 2 : B 13 | int visit[101][101]; 14 | int N; 15 | queue q; 16 | int dx[4] = {0,0,1,-1}; 17 | int dy[4] = {1,-1,0,0}; 18 | void bfs(int color){ 19 | int nx,ny; 20 | while(!q.empty()){ 21 | xy tmp = q.front(); 22 | q.pop(); 23 | for(int i = 0 ; i < 4 ; i++){ 24 | nx = tmp.x + dx[i]; 25 | ny = tmp.y + dy[i]; 26 | if(nx>0 && nx <= N && ny>0 && ny <= N && visit[ny][nx] == 0 && arr[ny][nx] == color){ 27 | visit[ny][nx] = 1; 28 | q.push({nx,ny}); 29 | } 30 | } 31 | } 32 | } 33 | 34 | void bfs1(int color){ 35 | int nx,ny; 36 | while(!q.empty()){ 37 | xy tmp = q.front(); 38 | q.pop(); 39 | for(int i = 0 ; i < 4 ; i++){ 40 | nx = tmp.x + dx[i]; 41 | ny = tmp.y + dy[i]; 42 | if(color != 2) { 43 | if (nx > 0 && nx <= N && ny > 0 && ny <= N && visit[ny][nx] == 0 && arr[ny][nx] != 2) { 44 | visit[ny][nx] = 1; 45 | q.push({nx, ny}); 46 | } 47 | } 48 | else{ 49 | if (nx > 0 && nx <= N && ny > 0 && ny <= N && visit[ny][nx] == 0 && arr[ny][nx] == color) { 50 | visit[ny][nx] = 1; 51 | q.push({nx, ny}); 52 | } 53 | } 54 | } 55 | } 56 | } 57 | 58 | 59 | 60 | int main(){ 61 | cin >> N; 62 | char a; 63 | for(int i = 1; i <= N ; i++){ 64 | for(int j = 1; j <= N ; j++){ 65 | cin >> a; 66 | if(a == 'R'){ 67 | arr[i][j] = 0; 68 | } 69 | else if(a == 'G'){ 70 | arr[i][j] = 1; 71 | } 72 | else{ 73 | arr[i][j] = 2; 74 | } 75 | } 76 | } 77 | 78 | int sol = 0; 79 | for(int i = 1 ; i <= N ; i++){ 80 | for(int j = 1; j <= N ; j++){ 81 | if(visit[i][j] == 0){ 82 | sol++; 83 | visit[i][j] = 1; 84 | q.push({j,i}); 85 | bfs(arr[i][j]); 86 | } 87 | } 88 | } 89 | cout << sol<<" "; 90 | 91 | while(!q.empty()) 92 | q.pop(); 93 | fill_n(&visit[0][0],101*101,0); 94 | sol = 0; 95 | for(int i = 1 ; i <= N ; i++){ 96 | for(int j = 1; j <= N ; j++){ 97 | if(visit[i][j] == 0){ 98 | sol++; 99 | visit[i][j] = 1; 100 | q.push({j,i}); 101 | bfs1(arr[i][j]); 102 | } 103 | } 104 | } 105 | cout << sol << endl; 106 | return 0; 107 | } -------------------------------------------------------------------------------- /SW_EXPERT/2112.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-04-12. 3 | // 4 | #include 5 | using namespace std; 6 | 7 | int arr[14][21]; 8 | int D, W, K; 9 | int med[14]; 10 | int medAB[14]; 11 | int sol; 12 | bool stop = true; 13 | struct st { 14 | int tarr[14][21]; 15 | int num; 16 | }; 17 | bool check() { 18 | int tmp; 19 | int cnt; 20 | for (int i = 1; i <= W; i++) { 21 | tmp = arr[1][i]; 22 | cnt = 1; 23 | for (int j = 2; j <= D; j++) { 24 | //같으면 cnt++ 25 | if (arr[j][i] == tmp) { 26 | cnt++; 27 | if (cnt == K) 28 | break; 29 | } 30 | else { 31 | tmp = arr[j][i]; 32 | cnt = 1; 33 | } 34 | } 35 | //통과하지못하면 false 반환 36 | if (cnt != K) 37 | return false; 38 | } 39 | //통과하면 true 반환 40 | return true; 41 | } 42 | 43 | void chk(st f) { 44 | int tmp; 45 | int cnt; 46 | bool ch; 47 | for (int i = 1; i <= W; i++) { 48 | tmp = f.tarr[1][i]; 49 | cnt = 1; 50 | ch = false; 51 | for (int j = 2; j <= D; j++) { 52 | //위층이랑 같으면 53 | if (tmp == f.tarr[j][i]) { 54 | cnt++; 55 | } 56 | //위층이랑 다르면 57 | else { 58 | tmp = f.tarr[j][i]; 59 | cnt = 1; 60 | } 61 | 62 | if (cnt == K) { 63 | ch = true; 64 | break; 65 | } 66 | 67 | } 68 | if (ch == false) { 69 | return; 70 | } 71 | } 72 | //통과했으면 73 | if (ch) { 74 | sol = f.num; 75 | stop = false; 76 | return; 77 | } 78 | 79 | } 80 | st makest(int d) { 81 | st tmp; 82 | for (int i = 1; i <= D; i++) { 83 | for (int j = 1; j <= W; j++) { 84 | if (medAB[i] == -1) 85 | tmp.tarr[i][j] = arr[i][j]; 86 | else 87 | tmp.tarr[i][j] = medAB[i]; 88 | } 89 | } 90 | 91 | tmp.num = d; 92 | return tmp; 93 | } 94 | 95 | void makeMedAB(int d, int num) { 96 | if (d == num) { 97 | chk(makest(num)); 98 | return; 99 | } 100 | medAB[med[d + 1]] = 0; 101 | makeMedAB(d + 1, num); 102 | medAB[med[d + 1]] = 1; 103 | makeMedAB(d + 1, num); 104 | } 105 | void dfs(int max, int cd, int td) { 106 | if (cd == td) { 107 | fill_n(medAB, 14, -1); 108 | makeMedAB(0,td); 109 | //check하고 110 | //반환 111 | return; 112 | } 113 | for (int i = max + 1; i <= D - (td - cd) + 1; i++) { 114 | med[cd + 1] = i; 115 | dfs(i, cd + 1, td); 116 | } 117 | } 118 | void makeCombination(int num) { 119 | for (int i = 1; i <= D - num + 1; i++) { 120 | med[1] = i; 121 | dfs(i, 1,num); 122 | } 123 | } 124 | 125 | int main() { 126 | int T; //test case 수 127 | cin >> T; 128 | int c = 1; 129 | while (T--) { 130 | cin >> D >> W >> K; 131 | for (int i = 1; i <= D; i++) { 132 | for (int j = 1; j <= W; j++) { 133 | cin >> arr[i][j]; 134 | } 135 | } 136 | //바로 검사 137 | if (check()) { 138 | cout << "#" << c << " " << 0 << endl; 139 | c++; 140 | stop = true; 141 | continue; 142 | } 143 | for (int i = 1; i <= D && stop; i++) { 144 | makeCombination(i); 145 | } 146 | cout << "#" << c << " " << sol << endl; 147 | stop = true; 148 | c++; 149 | } 150 | 151 | return 0; 152 | } 153 | -------------------------------------------------------------------------------- /SW_EXPERT/1949.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-14. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | int T, N, K,sol; // 3 <= N높이 <= 8, 1 <=K 깎 <= 5, 1<=높이<=20 9 | int map[8][8]; 10 | int visit[8][8]; 11 | int valCount[21]; 12 | //상하좌우 13 | int dx[4] = {0, 0, -1, 1}; 14 | int dy[4] = {1, -1, 0, 0}; 15 | 16 | int findMaxVal() { 17 | for (int i = 20; i > -1; i--) { 18 | if (valCount[i]) 19 | return i; 20 | } 21 | } 22 | 23 | bool chk(int y, int x) { 24 | if (!visit[y][x] && y >= 0 && y < N && x >= 0 && x < N) 25 | return true; 26 | return false; 27 | } 28 | 29 | void dfs(int y, int x, int cnt) { 30 | int ny, nx; 31 | for (int i = 0; i < 4; i++) { 32 | ny = y + dy[i]; 33 | nx = x + dx[i]; 34 | //유효한 점이면 들어가 35 | if (chk(ny, nx) && map[ny][nx] < map[y][x]) { 36 | //내리막길이어야! 37 | visit[ny][nx] = 1; 38 | dfs(ny, nx, cnt + 1); 39 | visit[ny][nx] = 0; 40 | } 41 | else{ 42 | if(sol < cnt) 43 | sol = cnt; 44 | } 45 | } 46 | } 47 | 48 | void simulation() { 49 | int tmp, maxVal; 50 | //일단 깎을 곳을 정하고 51 | for (int i = 0; i < N; i++) { 52 | for (int j = 0; j < N; j++) { 53 | //얼마나 깎을 지 정하고 54 | //0부터~K까지 55 | tmp = map[i][j]; 56 | maxVal = findMaxVal(); 57 | valCount[tmp]--; 58 | for (int k = 0; k <= K; k++) { 59 | if (tmp - k < 0) 60 | break; 61 | //cout <"<> T; 94 | int a = 1; 95 | int maxV = -1; 96 | while (T--) { 97 | cin >> N >> K; 98 | for (int i = 0; i < N; i++) { 99 | for (int j = 0; j < N; j++) { 100 | cin >> map[i][j]; 101 | valCount[map[i][j]]++; 102 | } 103 | } 104 | simulation(); 105 | cout << "#"< 5 | #include 6 | using namespace std; 7 | 8 | int map[301][301]; 9 | int cv[301][301]; 10 | int visit[301][301]; 11 | int dx[4] = {1,-1,0,0}; 12 | int dy[4] = {0,0,1,-1}; 13 | int N,M; 14 | struct xy{ 15 | int x,y; 16 | }; 17 | queue q; 18 | void countV(){ 19 | int nx,ny; 20 | int cnt; 21 | for(int i = 1 ; i <= N ; i++){ 22 | for(int j = 1; j <= M ; j++) { 23 | cnt = 0; 24 | if (map[i][j] != 0) { 25 | for (int k = 0; k < 4; k++) { 26 | ny = i + dy[k]; 27 | nx = j + dx[k]; 28 | if (nx > 0 && nx <= M && ny > 0 && ny <= N && map[ny][nx] == 0) 29 | cnt++; 30 | } 31 | cv[i][j] = cnt; 32 | } 33 | } 34 | } 35 | 36 | /*cout << "ice breaking speed"< 0 && nx <= M && ny > 0 && ny <= N && map[ny][nx] != 0 && visit[ny][nx] == 0){ 54 | visit[ny][nx] = 1; 55 | q.push({nx,ny}); 56 | } 57 | } 58 | } 59 | } 60 | 61 | int countCluster() { 62 | fill_n(&visit[0][0],301*301,0); 63 | int cnt = 0; 64 | for (int i = 1; i <= N; i++) { 65 | for (int j = 1; j <= M; j++) { 66 | if(map[i][j] != 0 &&visit[i][j] == 0){ 67 | visit[i][j] = 1; 68 | q.push({j,i}); 69 | cnt++; 70 | bfs(); 71 | } 72 | } 73 | } 74 | //cout << "current cluster num :"<> N >> M; 81 | for(int i = 1 ; i <= N ; i++){ 82 | for(int j = 1; j <= M ; j++) 83 | cin >> map[i][j]; 84 | } 85 | 86 | countV(); 87 | int sol = 0; 88 | while(1){ 89 | int cntC = countCluster(); 90 | //빙산이 2개이상으로 분리되면 시간 출력 후 종료 91 | if(cntC >= 2){ 92 | cout << sol << endl; 93 | return 0; 94 | } 95 | //빙산이 다 없어졌으면 0 출력 후 종료 96 | if(cntC == 0 ){ 97 | cout << 0 << endl; 98 | return 0; 99 | } 100 | //시간 경과 >> 빙산 붕괴 101 | for(int i = 1 ; i <= N ; i++){ 102 | for(int j = 1; j <= M ; j++){ 103 | map[i][j] -= cv[i][j]; 104 | if(map[i][j] <= 0) { 105 | map[i][j] = 0; 106 | } 107 | } 108 | } 109 | sol++; 110 | //다시 빙산 붕괴 속도 계산 111 | countV(); 112 | } 113 | } -------------------------------------------------------------------------------- /SW_EXPERT/5648.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N; 7 | int cnt; 8 | int sol; 9 | int visit[4001][4001]; 10 | int cvisit[4001][4001]; 11 | 12 | struct atom { 13 | int x, y, k, d; //x,y좌표,에너지,방향 14 | bool crash; 15 | 16 | atom(int _x, int _y, int _k, int _d, bool _crash) { 17 | x = _x; 18 | y = _y; 19 | k = _k; 20 | d = _d; 21 | crash = _crash; 22 | } 23 | }; 24 | 25 | int dx[4] = {0, 0, -1, 1}; 26 | int dy[4] = {1, -1, 0, 0}; 27 | 28 | vector atoms; 29 | 30 | bool chkPoint(int x, int y) { 31 | if (x >= 0 && x <= 4000 && y >= 0 && y <= 4000) 32 | return true; 33 | return false; 34 | } 35 | 36 | void move() { 37 | int nx, ny; 38 | //cout << "**********************" << endl; 39 | for (int i = 0; i < N; i++) { 40 | if (!atoms[i].crash) { 41 | nx = atoms[i].x + dx[atoms[i].d]; 42 | ny = atoms[i].y + dy[atoms[i].d]; 43 | //cout << nx << "," << ny << endl; 44 | if (chkPoint(nx, ny)) { 45 | visit[atoms[i].y][atoms[i].x]--; 46 | visit[ny][nx]++; 47 | 48 | if (visit[ny][nx] > 1) { 49 | cvisit[ny][nx] = 1; 50 | //cout << "crach at " << ny << "," << nx <<":"<> T; 96 | while (T--) { 97 | cin >> N; 98 | for (int i = 0; i < N; i++) { 99 | cin >> x >> y >> d >> k; 100 | atoms.emplace_back(atom(2 * (x + 1000), 2 * (y + 1000), k, d, false)); 101 | visit[2 * (y + 1000)][2 * (x + 1000)]++; 102 | } 103 | // for (int i = 0; i < N; i++) { 104 | // cout << atoms[i].x << atoms[i].y << endl; 105 | // } 106 | simulation(); 107 | cout << "#" << a << " " << sol << endl; 108 | init(); 109 | a++; 110 | } 111 | 112 | return 0; 113 | } -------------------------------------------------------------------------------- /SW_EXPERT/2112_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by vkdne on 2018-10-14. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int T, D, W, K; 10 | int com[13][2]; 11 | int film[13][20]; 12 | int nfilm[13][20]; 13 | bool pass = false; 14 | int sol = 0; 15 | 16 | bool chk() { 17 | bool c; 18 | for (int i = 0; i < W; i++) { 19 | int tmp = nfilm[0][i]; 20 | int cnt = 1; 21 | c = false; 22 | for (int j = 1; j < D; j++) { 23 | if (nfilm[j][i] == tmp) { 24 | cnt++; 25 | } else { 26 | cnt = 1; 27 | tmp = nfilm[j][i]; 28 | } 29 | if (cnt == K) { 30 | c = true; 31 | break; 32 | } 33 | } 34 | if (!c) 35 | return false; 36 | } 37 | return true; 38 | } 39 | 40 | void calc(int target) { 41 | for (int i = 0; i < D; i++) 42 | for (int j = 0; j < W; j++) 43 | nfilm[i][j] = film[i][j]; 44 | 45 | for (int i = 0; i < target; i++) { 46 | for (int j = 0; j < W; j++) 47 | nfilm[com[i][0]][j] = com[i][1]; 48 | } 49 | // cout<<"*********************************"<"<> T; 107 | int a = 1; 108 | while (T--) { 109 | cin >> D >> W >> K; 110 | for (int i = 0; i < D; i++) { 111 | for (int j = 0; j < W; j++) { 112 | cin >> film[i][j]; 113 | } 114 | } 115 | simulation(); 116 | cout << "#" << a++ << " " << sol << endl; 117 | init(); 118 | } 119 | 120 | return 0; 121 | } --------------------------------------------------------------------------------