diff --git a/190521/k_number.js b/190521/k_number.js deleted file mode 100644 index d49891a..0000000 --- a/190521/k_number.js +++ /dev/null @@ -1,6 +0,0 @@ -const solution = (arr, commands) => { - let answer = commands.map( el => arr.slice(el[0]-1,el[1]).sort(sortFuc)[el[2]-1]) - return answer; -} - -const sortFuc = (a,b) => {return a-b}; \ No newline at end of file diff --git a/190523/wangmin/pg42746.cpp b/190523/wangmin/pg42746.cpp new file mode 100644 index 0000000..2fbd7a5 --- /dev/null +++ b/190523/wangmin/pg42746.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +using namespace std; + +bool func(string a, string b){ + return a+b > b+a ? true : false; +} + +string solution(vector numbers) { + string answer = ""; + vector str; + for(int i = 0 ; i < numbers.size(); i++){ + str.push_back(to_string(numbers[i])); + } + + sort(str.begin(), str.end(), func); + for(int i = 0; i < str.size(); i++){ + answer += str[i]; + } + if(answer[0] == '0'){ + return "0"; + } + + return answer; +} \ No newline at end of file diff --git a/190524/circus/hIndex.js b/190524/circus/hIndex.js new file mode 100644 index 0000000..d507e9f --- /dev/null +++ b/190524/circus/hIndex.js @@ -0,0 +1,34 @@ +// 첫번째 +function solution(citations) { + var answer = 0; + while (true) { + if (citations.filter(x => { return x > answer }).length > answer) { + answer += 1; + continue; + } + break; + } + return Math.max.apply(null, citations) === 0 ? 0 : answer; +} + +// 두번째 + +function solution(citations) { + citations.sort((a, b) => b - a).unshift(0); + for (var i = citations.length - 1; i >= 0; i--) { + if (citations[i] >= i) return i; + } +} + +// 세번째 + +function solution(citations) { + citations = citations.sort((a, b) => a - b); + while (true) { + if (citations.findIndex(x => x < citations.length) !== -1) { + citations.shift(); + } else { + return citations.length; + } + } +} \ No newline at end of file diff --git a/190524/honux/h-index.js b/190524/honux/h-index.js new file mode 100644 index 0000000..e3a5572 --- /dev/null +++ b/190524/honux/h-index.js @@ -0,0 +1,6 @@ +function solution(citations) { + citations.sort((a,b) => (b - a)); + let i = 0; + while (i + 1 <= citations[i]) i++; + return i; +} diff --git a/190524/ruby/solution.js b/190524/ruby/solution.js new file mode 100644 index 0000000..fd42ca1 --- /dev/null +++ b/190524/ruby/solution.js @@ -0,0 +1,13 @@ +const solution = (c) => { + c.sort((a,b)=>b-a); + for(let i=0; i c[i]){ + return i + }else if(i+1 === c[i]){ + return c[i] + } + } + return c.length +} + +console.log(solution([1,1,3,4,8])) \ No newline at end of file diff --git a/190524/wangmin/pg42747.cpp b/190524/wangmin/pg42747.cpp new file mode 100644 index 0000000..8d1425a --- /dev/null +++ b/190524/wangmin/pg42747.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +using namespace std; + +int solution(vector citations) { + int h; + sort(citations.begin(),citations.end()); + for(int i = 0; i< citations.size(); i++){ + h = citations.size()-i; + + if(citations[i] >= h){ + return h; + } + + } + return 0; +} + diff --git a/190527/H/exam.py b/190527/H/exam.py new file mode 100644 index 0000000..9fd5582 --- /dev/null +++ b/190527/H/exam.py @@ -0,0 +1,17 @@ +def solution(answers): + person1 = [1, 2, 3, 4, 5] + person2 = [2, 1, 2, 3, 2, 4, 2, 5] + person3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] + count = [0, 0, 0] + result = [] + for idx, answer in enumerate(answers): + if answer == person1[idx % len(person1)]: + count[0] += 1 + if answer == person2[idx % len(person2)]: + count[1] += 1 + if answer == person3[idx % len(person3)]: + count[2] += 1 + for idx, c in enumerate(count): + if c == max(count): + result.append(idx+1) + return result diff --git a/190527/circus/exam.js b/190527/circus/exam.js new file mode 100644 index 0000000..a5cac56 --- /dev/null +++ b/190527/circus/exam.js @@ -0,0 +1,19 @@ +function solution(answers) { + var num1 = [1, 2, 3, 4, 5]; + var num2 = [2, 1, 2, 3, 2, 4, 2, 5]; + var num3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]; + var results = [0, 0, 0]; + var winer = []; + + answers.forEach((ans, i) => { + if (ans === num1[i % num1.length]) results[0] += 1; + if (ans === num2[i % num2.length]) results[1] += 1; + if (ans === num3[i % num3.length]) results[2] += 1; + }) + + var max = Math.max(...results); + results.forEach((result, i) => { + if (result === max) winer.push(i + 1); + }) + return winer; +} \ No newline at end of file diff --git a/190527/hyodol/mock_test.cpp b/190527/hyodol/mock_test.cpp new file mode 100644 index 0000000..4d6c8d5 --- /dev/null +++ b/190527/hyodol/mock_test.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +vector solution(vector answers) { + vector > patterns(3); + patterns[0] = {1, 2, 3, 4, 5}; + patterns[1] = {2, 1, 2, 3, 2, 4, 2, 5}; + patterns[2] = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; + + int maxCnt = 0; + vector personCnt(3); + for (int i = 0; i < 3; i++) { + int cnt = 0; + int patternsSize = (int)patterns[i].size(); + for (int j = 0; j < answers.size(); j++) { + if (answers[j] == patterns[i][j % patternsSize]) cnt++; + } + personCnt[i] = cnt; + maxCnt = max(maxCnt, cnt); + } + + vector result; + for (int i = 0; i < 3; i++) { + if (maxCnt == personCnt[i]) result.push_back(i+1); + } + return result; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + vector result1 = solution({1, 2, 3, 4, 5}); + for (int num : result1) cout << num << " "; + cout << '\n'; + + vector result2 = solution({1, 3, 2, 4, 2}); + for (int num : result2) cout << num << " "; + cout << '\n'; + + return 0; +} diff --git a/190527/ingleby/mouigosa.py b/190527/ingleby/mouigosa.py new file mode 100644 index 0000000..bb4a765 --- /dev/null +++ b/190527/ingleby/mouigosa.py @@ -0,0 +1,24 @@ +def solution(answers): + patternA = [1, 2, 3, 4, 5] + patternB = [2, 1, 2, 3, 2, 4, 2, 5] + patternC = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] + i = 0 + a = 0 + b = 0 + c = 0 + for s in answers : + if patternA[i%5] == s: + a=a+1 + if patternB[i%8] == s: + b=b+1 + if patternC[i%10] == s: + c=c+1 + i = i+1 + answer2 = [a,b,c] + maxt = max(answer2) + answer = [] + i = 1 + for idx in range(len(answer2)): + if answer2[idx] == maxt: + answer.append(idx + 1) + return answer diff --git a/190527/mockExam.js b/190527/mockExam.js new file mode 100644 index 0000000..9f3ea56 --- /dev/null +++ b/190527/mockExam.js @@ -0,0 +1,21 @@ +function solution(answers) { + let answer = []; + let supo1 = [1,2,3,4,5]; + let supo2 = [2,1,2,3,2,4,2,5]; + let supo3 = [3,3,1,1,2,2,4,4,5,5]; + let ans1 = howManyCorrect(answers , supo1); + let ans2 = howManyCorrect(answers , supo2); + let ans3 = howManyCorrect(answers , supo3); + let max = Math.max(ans1,ans2,ans3); + if( max === ans1) answer.push(1); + if( max === ans2) answer.push(2); + if( max === ans3) answer.push(3); + + return answer; +} + +function howManyCorrect(ans , supo) { + return ans.filter(function(v, i){ + return v === supo[i % supo.length]; + }).length +} diff --git a/190527/mukeunzi/MockTest.java b/190527/mukeunzi/MockTest.java new file mode 100644 index 0000000..e49f252 --- /dev/null +++ b/190527/mukeunzi/MockTest.java @@ -0,0 +1,46 @@ +import java.util.*; + +class Solution { + public int[] solution(int[] answers) { + int way1[] = new int[]{1, 2, 3, 4, 5}; + int way2[] = new int[]{2, 1, 2, 3, 2, 4, 2, 5}; + int way3[] = new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; + int cnt[] = new int[3]; + int max = 0; + int result[]; + List answer = new ArrayList<>(); + + for(int i=0; iy and x>z: + return [1] + if y>x and y>z: + return [2] + if z>x and z>y: + return [3] + if x==y and y > z: + return [1, 2] + if y==z and z > x: + return [2, 3] + if z==x and x > y: + return [1, 3] + + return answer diff --git a/190527/ruby/solution.js b/190527/ruby/solution.js new file mode 100644 index 0000000..6353650 --- /dev/null +++ b/190527/ruby/solution.js @@ -0,0 +1,25 @@ +const solution = (answers) => { + let answer = []; + const a = [1,2,3,4,5]; + const b = [2,1,2,3,2,4,2,5]; + const c = [3,3,1,1,2,2,4,4,5,5]; + let num = [0,0,0]; + + answers.forEach((el, i)=>{ + if(el === a[i%5]){ + num[0] = num[0] + 1; + } + if(el === b[i%8]){ + num[1] = num[1] + 1; + } + if(el === c[i%10]){ + num[2] = num[2] + 1; + } + }) + num.forEach( (el, i) => { + if(el === Math.max(...num)){ + answer.push(i+1) + } + }) + return answer +} \ No newline at end of file diff --git a/190527/wangmin/pg42840.cpp b/190527/wangmin/pg42840.cpp new file mode 100644 index 0000000..941df3c --- /dev/null +++ b/190527/wangmin/pg42840.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; + +vector solution(vector answers) { + vector one = {1,2,3,4,5}; + vector two = {2,1,2,3,2,4,2,5}; + vector three = {3,3,1,1,2,2,4,4,5,5}; + + int oneCount = 0; + int twoCount = 0; + int threeCount = 0; + + for(int i = 0; i < answers.size(); i++){ + if(answers[i] == one[i%one.size()]){ + oneCount++; + } + } + + int max = oneCount; + + for(int i = 0; i < answers.size(); i++){ + if(answers[i] == two[i%two.size()]){ + twoCount++; + } + } + + max = (twoCount > max) ? twoCount : max; + + for(int i = 0; i < answers.size(); i++){ + if(answers[i] == three[i%three.size()]){ + threeCount++; + } + } + + max = (threeCount > max) ? threeCount : max; + + + + vector answer; + if(max == oneCount) answer.push_back(1); + if(max == twoCount) answer.push_back(2); + if(max == threeCount) answer.push_back(3); + + + return answer; +} \ No newline at end of file diff --git a/190530/honux/1913-snail.cpp b/190530/honux/1913-snail.cpp new file mode 100644 index 0000000..9983edf --- /dev/null +++ b/190530/honux/1913-snail.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +using namespace std; +int dr[] = { -1, 0, 1, 0 }; +int dc[] = { 0, 1, 0, -1 }; + + +int main() { + int n; + scanf("%d", &n); + vector > m(n); + map> nmap; + for (int i = 0; i < n; i++) { + m[i].resize(n); + } + + int num = 1; + int row = n / 2; + int col = row; + double w = 1; + m[row][col] = num; + int vec = 0; + + bool loop = true; + while (loop) { + for (int i = 0; i < (int) w; i++) { + num++; + if (num > n * n) { + loop = false; + break; + } + row += dr[vec]; + col += dc[vec]; + m[row][col] = num; + nmap[num] = make_pair(row + 1, col + 1); + } + vec = (vec + 1) % 4; + w += 0.5; + } + + for (auto v : m) { + for (auto n : v) { + printf("%d ", n); + } + printf("\n"); + } + + int x; + scanf("%d", &x); + printf("%d %d\n", nmap[x].first, nmap[x].second); + return 0; +} diff --git a/190530/hyodol/snail.cpp b/190530/hyodol/snail.cpp new file mode 100644 index 0000000..ba72ec3 --- /dev/null +++ b/190530/hyodol/snail.cpp @@ -0,0 +1,54 @@ +// BOJ 1913 - 달팽이 +#include +#include +using namespace std; + +const int dy[] = {-1, 0, 1, 0}; +const int dx[] = {0, 1, 0, -1}; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + int n; + cin >> n; + vector > arr(n, vector(n)); + + int value; + cin >> value; + + int num = 1; + int dir = 0; + int y = n / 2; + int x = n / 2; + while (!(y == 0 && x == 0)) { + arr[y][x] = num++; + int ny = y + dy[dir % 4]; + int nx = x + dx[dir % 4]; + if (ny < 0 || ny >= n || nx < 0 || nx >= n) dir++; + if (arr[ny][nx] == 0) { + y = ny; + x = nx; + dir++; + } else { + dir--; + num--; + } + } + arr[y][x] = num; + + int findY = 0, findX = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (value == arr[i][j]) { + findY = i+1; + findX = j+1; + } + cout << arr[i][j] << " "; + } + cout << '\n'; + } + + cout << findY << " " << findX << '\n'; + return 0; +} diff --git a/190530/jake/1913.cpp b/190530/jake/1913.cpp new file mode 100644 index 0000000..bd8beca --- /dev/null +++ b/190530/jake/1913.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +int n, t; +int arr[999][999]{0,}; + +int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}; + +bool valid_location(int y, int x) +{ + return y < n && y >= 0 && x < n && x >= 0 && arr[y][x] == 0; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n >> t; + + int num = n * n, d = 0, y = -1, x = 0, ay, ax; + + while(num > 0){ + int dy = dir[d % 4][0], dx = dir[d % 4][1]; + y += dy, x += dx; + while(valid_location(y, x)){ + if(num == t) ay = y, ax = x; + arr[y][x] = num--, y += dy, x += dx; + } + y -= dy, x -= dx; + ++d; + } + + for(int i=0; i +#include + +using namespace std; + +int main () { + int n,a; + cin >> n >> a; + + vector> vec = vector>(n,(vector(n,0))); + + vector start ={n/2,n/2}; + vector> right = {{-1,0},{0,1},{1,0},{0,-1}}; + vector head = {-1,0}; + + vector aindex = {0,0}; + + // vec[start[0]][start[1]] = 1; + int rightindex = 0; + for(int i = 1; i <= n*n; i ++){ + // vec[start[0],start[1]] + vec[start[0]][start[1]] = i; + + if(i == a){ + aindex = {start[0],start[1]}; + } + + if(vec[start[0]+right[rightindex%4][0]][start[1]+right[rightindex%4][1]] == 0){ + head = right[rightindex%4]; + start[0] = start[0]+head[0]; + start[1] = start[1]+head[1]; + rightindex++; + }else{ + start[0] = start[0]+head[0]; + start[1] = start[1]+head[1]; + } + } + + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + cout << vec[i][j]<<" "; + } + cout << "\n"; + } + + cout << aindex[0]+1 << " " << aindex[1]+1< +#include +#include +#include +#include +#include +using namespace std; + +vector getPrime() { + const int MAX = 9999999; + vector isPrime(MAX+1, true); + isPrime[0] = isPrime[1] = false; + for (int i = 2; i < sqrt(MAX); i++) { + if (!isPrime[i]) continue; + for (int j = i*i; j <= MAX; j+=i) { + isPrime[j] = false; + } + } + return isPrime; +} + +int solution(string numbers) { + string tempNumbers = numbers; + sort(tempNumbers.begin(), tempNumbers.end()); + vector isPrime = getPrime(); + set checkPrime; + do { + for (int d = 1; d <= numbers.length(); d++) { + int num = stoi(tempNumbers.substr(0, d)); + if (isPrime[num] && checkPrime.count(num) == 0) { + checkPrime.insert(num); + } + } + } while (next_permutation(tempNumbers.begin(), tempNumbers.end())); + + return (int)checkPrime.size(); +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + cout << solution("17") << '\n'; + cout << solution("011") << '\n'; + cout << solution("012") << '\n'; + + return 0; +} diff --git a/190611/jake/find-primes.cpp b/190611/jake/find-primes.cpp new file mode 100644 index 0000000..792de56 --- /dev/null +++ b/190611/jake/find-primes.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; +unordered_set used; +vector picked; + +bool check_prime(const int n) +{ + if(n == 2) return true; + if(n == 1 || n % 2 == 0) return false; + + int sqrt_root = 1; + while(sqrt_root * sqrt_root < n * n) + ++sqrt_root; + + for(int i=2; i& digits, int number, int len) +{ + if(len == digits.size()) return 0; + + int ret = 0; + for(int i=0; i digits; + for(char c : numbers) digits.push_back(c - '0'); + picked = vector(digits.size(), false); + + return check_all_numbers(digits, 0, 0); +} diff --git a/190611/problem.md b/190611/problem.md new file mode 100644 index 0000000..07c8f5c --- /dev/null +++ b/190611/problem.md @@ -0,0 +1,6 @@ +# 소수 찾기 + +- https://programmers.co.kr/learn/courses/30/lessons/42839 +- 분류: 완전탐색 +- 난이도: 2 + diff --git a/190611/sony/Lv2_findPrimeNum.py b/190611/sony/Lv2_findPrimeNum.py new file mode 100644 index 0000000..76c92b2 --- /dev/null +++ b/190611/sony/Lv2_findPrimeNum.py @@ -0,0 +1,33 @@ +import itertools + +def primNumber(n): + if n < 2: return False + if n == 2: return True + if n % 2 == 0: return False + + m = int(n**0.5)+1 + + for i in range(3,m,2): + if n % i == 0: + return False + return True + +def findPrimeNum(numbers): + count = 0 + a = set() + for k in range(len(numbers)): + n = list(itertools.permutations(numbers, k+1)) + + for i in n: + s = ''.join(i) + a.add(int(s)) + + for j in a: + if primNumber(j): + count += 1 + return count + +# nums = '17' +nums = '011' + +findPrimeNum(nums) \ No newline at end of file diff --git a/190613/honux/input.txt b/190613/honux/input.txt new file mode 100644 index 0000000..5ca0303 --- /dev/null +++ b/190613/honux/input.txt @@ -0,0 +1,11 @@ +10 +5 +2 +3 +1 +4 +2 +3 +5 +1 +7 \ No newline at end of file diff --git a/190613/honux/sort.js b/190613/honux/sort.js new file mode 100644 index 0000000..f9d4c54 --- /dev/null +++ b/190613/honux/sort.js @@ -0,0 +1,20 @@ +var lines = require('fs').readFileSync('/dev/stdin').toString().split('\n'); +var arr = []; +for (var i = 0; i <= 10000; i++) { + arr[i] = 0; +} + +var n = Number(lines[0]); +for (var i = 1; i <= n; i++) { + var x = Number(lines[i]); + arr[x]++; +} + +for (var i = 0; i <= 10000; i++) { + if (arr[i] > 0) { + for (var j = 0; j < arr[i]; j++) { + console.log(i); + } + } +} + diff --git a/190613/hyodol/10989.cpp b/190613/hyodol/10989.cpp new file mode 100644 index 0000000..7fac9ee --- /dev/null +++ b/190613/hyodol/10989.cpp @@ -0,0 +1,29 @@ +// BOJ 10989 - 수 정렬하기 3 +// Counting Sort +#include +#include +#include +using namespace std; +const int MAX = 10000; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + int n; + cin >> n; + + vector count(MAX+1); + for (int i = 0; i < n; i++) { + int num; + cin >> num; + count[num]++; + } + + for (int num = 0; num <= MAX; num++) { + if (count[num] == 0) continue; + while (count[num]--) cout << num << '\n'; + } + + return 0; +} diff --git a/190613/jake/10989.cpp b/190613/jake/10989.cpp new file mode 100644 index 0000000..40149f0 --- /dev/null +++ b/190613/jake/10989.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +int N; +int store[10001] {0,}; +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cin >> N; + int t; + while(N--){ + cin >> t; + store[t]++; + } + for(int i = 1; i < 10001; ++i){ + while(store[i]--){ + cout << i << "\n"; + } + } + return 0; +} + diff --git a/190613/problem.md b/190613/problem.md new file mode 100644 index 0000000..cb8892d --- /dev/null +++ b/190613/problem.md @@ -0,0 +1,5 @@ +# BOJ 정렬하기 3 + +- https://www.acmicpc.net/problem/10989 +- 난이도: 2점 + diff --git a/190613/sony/boj_10989.py b/190613/sony/boj_10989.py new file mode 100644 index 0000000..057b00e --- /dev/null +++ b/190613/sony/boj_10989.py @@ -0,0 +1,17 @@ +import sys + +n = int(sys.stdin.readline()) + + +def countingSort(n): + count_list = [0] * (10001) # 주어지는 수의 크기 범위만큼 리스트 길이를 만든다. + for _ in range(n): + a = int(sys.stdin.readline()) + count_list[a] += 1 + + for j in range(len(count_list)): + if count_list[j] != 0: + for _ in range(count_list[j]): + print(j) + +countingSort(n) diff --git a/190613/wangmin/bj10989.cpp b/190613/wangmin/bj10989.cpp new file mode 100644 index 0000000..c555b9a --- /dev/null +++ b/190613/wangmin/bj10989.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int main(){ + cin.tie(NULL); + cout.tie(NULL); + ios_base :: sync_with_stdio(false); + + int N; + cin >> N; + int a; + vector count(10001, 0); + + + for(int i = 0; i < N; i++){ + cin >> a; + count[a]++; + } + + for(int i = 0; i <= 10001; i++){ + for(int j = count[i]; j > 0; j--){ + cout << i << "\n"; + } + } + + + return 0; +} + + diff --git a/190618/problem.md b/190618/problem.md new file mode 100644 index 0000000..886ee22 --- /dev/null +++ b/190618/problem.md @@ -0,0 +1,6 @@ +# programmers 체육복 + +- https://programmers.co.kr/learn/courses/30/lessons/42862 +- 레벨 1 +- 분류: greedy + diff --git a/190621/hyodol/1525.cpp b/190621/hyodol/1525.cpp new file mode 100644 index 0000000..b83d30b --- /dev/null +++ b/190621/hyodol/1525.cpp @@ -0,0 +1,63 @@ +// BOJ 1525 - 퍼즐 +#include +#include +#include +#include +using namespace std; +const int dy[] = {-1, 1, 0, 0}; +const int dx[] = {0, 0, -1, 1}; + +int bfs(int start) { + unordered_map um; + queue q; + + um[start] = 0; + q.push(start); + + while (!q.empty()) { + int current = q.front(); + q.pop(); + + if (current == 123456789) + return um[current]; + + string strCurrent = to_string(current); + int index = (int)strCurrent.find('9'); + int cy = index / 3, cx = index % 3; + + for (int d = 0; d < 4; d++) { + int ny = cy + dy[d]; + int nx = cx + dx[d]; + if (ny < 0 || ny >= 3 || nx < 0 || nx >= 3) + continue; + + string strNext = strCurrent; + swap(strNext[cy * 3 + cx], strNext[ny * 3 + nx]); + int next = stoi(strNext); + if (um.count(next) == 0) { + um[next] = um[current] + 1; + q.push(next); + } + } + } + + return -1; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + int start = 0; + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + int num; + cin >> num; + if (num == 0) num = 9; + start = start * 10 + num; + } + } + + cout << bfs(start) << '\n'; + return 0; +} diff --git a/190621/jake/1525.cpp b/190621/jake/1525.cpp new file mode 100644 index 0000000..71474cc --- /dev/null +++ b/190621/jake/1525.cpp @@ -0,0 +1,84 @@ +#include > +using namespace std; + +unordered_set visited; +string target = "123456780"; +int delta[4][2] {0, -1, -1, 0, 0, 1, 1, 0}; + +void state_to_arr(char arr[3][3], string& state){ + for(int i=0; i<3; ++i){ + for(int j=0; j<3; ++j){ + arr[i][j] = state[i*3+j]; + } + } +} + +string arr_to_state(char arr[3][3]) { + string ret = ""; + for(int i=0; i<3; ++i){ + for(int j=0; j<3; ++j){ + ret += arr[i][j]; + } + } + return ret; +} + +bool is_valid(int y, int x){ + return y >= 0 && y < 3 && x >= 0 && x < 3; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + string start = ""; + char t; + for(int i=0; i<9; ++i){ + cin >> t; + start += t; + } + + queue> q; + q.push(make_pair(start, 0)); + int answer = -1; + + while(q.size()){ + auto cur = q.front(); + q.pop(); + auto& state = cur.first; + + if(visited.find(state) != visited.end()) { + continue; + } + if(state == target){ + answer = cur.second; + break; + } + visited.insert(cur.first); + + int idx; + for(int i=0; i<9; ++i){ + if(state[i] == '0'){ + idx = i; + break; + } + } + + int y = idx / 3, x = idx % 3; + char arr[3][3]; + state_to_arr(arr, state); + for(int i=0; i<4; ++i){ + int next_y = y + delta[i][0], next_x = x + delta[i][1]; + if(is_valid(next_y, next_x)){ + swap(arr[next_y][next_x], arr[y][x]); + q.push(make_pair(arr_to_state(arr), cur.second + 1)); + swap(arr[next_y][next_x], arr[y][x]); + } + } + } + + cout << answer; + + return 0; +} diff --git a/190621/problem.md b/190621/problem.md new file mode 100644 index 0000000..ccd5f1b --- /dev/null +++ b/190621/problem.md @@ -0,0 +1,5 @@ +# BOJ 1525 퍼즐 + +- https://www.acmicpc.net/problem/1525 +- 분류: BFS + diff --git a/190621/sony/boj_1525.py b/190621/sony/boj_1525.py new file mode 100644 index 0000000..f4f7cb0 --- /dev/null +++ b/190621/sony/boj_1525.py @@ -0,0 +1,41 @@ +import sys +from collections import deque + +q = deque() +dist = dict() + +a = [list(map(int, sys.stdin.readline().split())) for _ in range(3)] +m = 0 + +for i in range(3): + for j in range(3): + n = a[i][j] + if n == 0: + n = 9 + m = m*10 + n + +def bfs(): + q.append(m) + dist[m] = 0 + + while q: + d = q.popleft() + if d == 123456789: + return dist[d] + s = str(d) + k = s.find('9') + y,x = k//3, k%3 + + for dy, dx in (-1,0), (1,0), (0,-1), (0,1): + ny, nx = y+dy, x+dx + if nx < 0 or nx >= 3 or ny < 0 or ny >= 3: continue + nk = ny*3 + nx + ns = list(s) + ns[k], ns[nk] = ns[nk], ns[k] + nd = int(''.join(ns)) + if dist.get(nd) is None: + dist[nd] = dist[d]+1 + q.append(nd) + return -1 + +print(bfs()) \ No newline at end of file diff --git a/190621/wangmin/bj1525.cpp b/190621/wangmin/bj1525.cpp new file mode 100644 index 0000000..27cc03d --- /dev/null +++ b/190621/wangmin/bj1525.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +using namespace std; + +int find9(int number){ + int result = 0; + int c; + int i = 0; + while(!result){ + c = number%10; + if(c == 9){ + result = 9-i; + return result; + } + number = number/10; + i++; + } + return result; +} + +vector makeVec(int number){ + vector result(0); + int c; + int n = 100000000; + for(int i = 0; i < 9 ; i++){ + c = number/n%10; + result.push_back(c); + n = n/10; + } + + return result; +} + +int makeNumber(vector vec){ + int result = 0; + int n = 100000000; + for(int i = 0; i < 9; i++){ + result += vec[i]*n; + n = n/10; + } + return result; +} + +vector swapVec(vector vec, int a, int b){ + int c = vec[a-1]; + vec[a-1] = vec[b-1]; + vec[b-1] = c; + + return vec; +} + +int find9(vector vec){ + for(int i = 0 ; i < 9; i++){ + if(vec[i] == 9) { + return i+1; + } + } +} + +int main(){ + int start; + int number = 0; + int n = 100000000; + int c; + + vector vec(0); + map m; + queue>> q ; + vector> map(10,vector(0)); + map[1] = {2,4}; + map[2] = {1,3,5}; + map[3] = {2,6}; + map[4] = {1,5,7}; + map[5] = {2,4,6,8}; + map[6] = {3,5,9}; + map[7] = {4,8}; + map[8] = {5,7,9}; + map[9] = {6,8}; + + + for(int i = 0 ; i < 9; i++){ + cin >> c; + if(c== 0) { + c = 9; + } + vec.push_back(c); + number += c*n; + n = n/10; + } + + m[number] = true; + + + q.push({vec,{0}}); + vector curVec; + int curNum; + int count; + int result = -1; + int cur9; + int next9; + vector nextVec; + int nextNum; + while(!q.empty()){ + curVec = q.front()[0]; + count = q.front()[1][0]; + curNum = makeNumber(curVec); + cur9 = find9(curNum); + q.pop(); + + if(curNum == 123456789){ + result = count; + break; + } + + for(int i = 0; i < map[cur9].size(); i++){ + nextVec = swapVec(curVec,cur9,map[cur9][i]); + nextNum = makeNumber(nextVec); + + if(m[nextNum] != true){ + m[nextNum] = true; + q.push({nextVec,{count+1}}); + } + } + } + + cout << result < +using namespace std; + +bool solved = false; +int board[9][9], answer[9][9]; +bool row[9][10]{0,}, col[9][10]{0,}, block[9][10]{0,}; + +void get_next(int& y, int& x) { + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + if(!board[i][j]){ + y = i, x = j; + return; + } + } + } +} + +bool check_possible(int y, int x, int v){ + return !col[y][v] && !row[x][v] && !block[(y/3)*3+(x/3)][v]; +} + +void set_boolean(int y, int x, int v, bool set_or_unset) { + col[y][v] = row[x][v] = block[(y/3)*3+(x/3)][v] = set_or_unset; +} + +void solve() { + if(solved) return; + int y = -1, x = -1; + get_next(y, x); + + if(y == -1){ + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + answer[i][j] = board[i][j]; + } + } + solved = true; + return; + } + + for(int v=1; v<10; ++v) { + if(check_possible(y, x, v)){ + set_boolean(y, x, v, true); + board[y][x] = v; + solve(); + board[y][x] = 0; + set_boolean(y, x, v, false); + } + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + cin >> board[i][j]; + set_boolean(i, j, board[i][j], true); + } + } + + solve(); + + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + cout << answer[i][j] << " "; + } + cout << "\n"; + } + + return 0; +} diff --git a/190624/problem.md b/190624/problem.md new file mode 100644 index 0000000..bbee43d --- /dev/null +++ b/190624/problem.md @@ -0,0 +1,6 @@ +# BOJ 2580 스도쿠 + +- https://www.acmicpc.net/problem/2580 +- 분류: DFS + +