백준188 백준-2992번(크면서 작은 수)-python 백준-2992번(백트레킹,dfs)-python dfs를 이용하여 코드를 작성해보았다. import sys n=(str(sys.stdin.readline().strip())) visited=[0]*len(n) s=[] y=[] def dfs(): if len(n)==len(s): y.append(int(''.join(s))) return for i in range(len(n)): if visited[i]==0: s.append(n[i]) visited[i]=1 dfs() s.pop() visited[i]=0 dfs() y.sort() for x in y: if x > int(n): print(x) break; if x==y[-1]: print(0) break; 아래는 permutation(내장함수)을 사용.. 2023. 11. 23. 백준-10866번(덱)-python 백준-10866번(덱)-python dequeue문제를 풀어보았다. push_front함수는 insert(0,x[1])로도 표현이 가능했지만,일반 queue와 달리 appendleft라는 함수로도 가능했다. 하지만 vscode에서는 파랭이가 떠서 기분이 좋지않다.에디터를 바꿀까 고민하고있다. 또한 push_back연산은 덱의 마지막부분에 요소를 추가해주는 함수이다. append(x[1])로 표현가능하다. pop_front연산은 덱의 제일 앞에 있는 요소를 빼내는 함수이다.popleft()를 사용한다. pop_back은 덱의 마지막 요소를 제거하는 함수이다.pop()을 사용하였다. size는 덱의 요소의 수를 반환해야하므로 len()함수를 사용한다. empty는 덱이 비었으면 1,요소가 들어있으면 0을 반.. 2023. 11. 23. 백준-24445번(알고리즘 수업 - 너비 우선 탐색 2)-python 백준-24445번(알고리즘 수업 - 너비 우선 탐색 2)-python 24444번 문제에서 for x in adl: x.sort() 이 부분을 for x in adl: x.sort(reverse=True) 이렇게 변경해주면 되었다. from collections import deque import sys input = sys.stdin.readline V,E,R=map(int,input().split()) visited=[0]*(V+1) adl=[[] for _ in range(V+1)] cnt=1 for _ in range(E): m,n=map(int,input().split()) adl[m].append(n) adl[n].append(m) for x in adl: x.sort(reverse=True).. 2023. 11. 23. 백준-24444번(알고리즘 수업 - 너비 우선 탐색 1)-python 백준-24444번(알고리즘 수업 - 너비 우선 탐색 1)-python 2차원 배열을 for문이 아닌 다른 방식으로 생성해볼까 하여 adl=[[] for _ in range(V+1)] 이부분을 아래부분으로 adl=[[]]*(V+1) 바꾸어 문제를 풀었는데 모든 배열이 복사되어 나왔다. 그래서 문제가 무엇인지 검색해보았더니,2차원배열을 *를 이용하여 생성하는것은 얕은 복사가 진행되어 배열 내의 요소들이 같은 객체를 가리키게 된다고 하였다. 다시 말해서,이 방식으로 2차원 배열을 선언하고 요소를 변경하게 되면 다른 요소들의 값도 같이 바뀌는 것이었다. 참고: https://velog.io/@sjy5386/Python-2%EC%B0%A8%EC%9B%90-%EB%B0%B0%EC%97%B4-%EC%84%A0%EC%.. 2023. 11. 23. 백준-24480번(알고리즘 수업 - 깊이 우선 탐색 2)-python 백준-24480번(알고리즘 수업 - 깊이 우선 탐색 2)-python 정점번호가 24479번에서 오름차순 방문이었던것이 내림차순으로 바뀌어 sort함수에서 옵션을(reverse=True)로 변경하였다 import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) V,M,R=map(int,input().split()) adj=[[] for i in range(V+1)] visited=[0]*(V+1) v=[0]*(V+1) cnt=1 for _ in range(M): m,n=map(int,input().split()) adj[m].append(n) adj[n].append(m) def dfs(x): global cnt visited[x]=cnt adj.. 2023. 11. 23. 백준-24479번(알고리즘 수업 - 깊이 우선 탐색 1)-python 백준-24479번(알고리즘 수업 - 깊이 우선 탐색 1)-python import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) V,M,R=map(int,input().split()) adj=[[] for i in range(V+1)] visited=[0]*(V+1) v=[0]*(V+1) cnt=1 for _ in range(M): m,n=map(int,input().split()) adj[m].append(n) adj[n].append(m) def dfs(x): global cnt visited[x]=cnt adj[x].sort() for i in adj[x]: if not visited[i]: cnt+=1 dfs(i) dfs(R) for x.. 2023. 11. 23. 이전 1 ··· 25 26 27 28 29 30 31 32 다음