Skip to content

Commit

Permalink
Merge pull request #63 from HYU-PS-STUDY/wonkyDD
Browse files Browse the repository at this point in the history
[week-10] 1890, 9466
  • Loading branch information
clean2001 authored Mar 30, 2024
2 parents a1c15d6 + 9419785 commit 9c4269e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Empty file removed week10/wonkyDD/.gitkeep
Empty file.
43 changes: 43 additions & 0 deletions week10/wonkyDD/1890.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <cstring>
using namespace std;

#define MAX 100
using ll = long long;

int map[MAX][MAX];
ll dp[MAX][MAX];
int N;

int main() {
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

cin >> N;
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
cin >> map[i][j];
}
}
memset(dp, 0, sizeof(dp));

/** NOTE : 시작점의 dp값을 1로 설정한다 */
dp[0][0] = 1;
for (int y=0; y<N; ++y) {
for (int x=0; x<N; ++x) {
/**
* NOTE
* - dp값이 갱신이 되지 않았다면 도달하지 못하는 경로이므로 패스한다
* - 종착역이면 패스한다
*/
if (dp[y][x] == 0 || (y == N-1 && x == N-1)) continue;

int d = map[y][x];
/** NOTE : 미래의 dp값을 누적 갱신 */
if (y+d < N) dp[y+d][x] += dp[y][x];
if (x+d < N) dp[y][x+d] += dp[y][x];
}
}

cout << dp[N-1][N-1];
return 0;
}
50 changes: 50 additions & 0 deletions week10/wonkyDD/9466.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <cstring>
#define MAX 100001
using namespace std;
int choice[MAX];
bool visited[MAX];
bool cycle[MAX];
int n;
int cnt;

void dfs(int cur) {
visited[cur] = true;
int next = choice[cur];
if (visited[next]) {
// NOTE : 방문이 됐음에도 cycle로 마킹되지 않았어야만 사이클로 카운팅한다
if (!cycle[next]) {
for (int i=next; i!=cur; i=choice[i]) ++cnt;
++cnt;
}
}
else dfs(next);

/**
* NOTE
* - 선택한 학생에 대한 dfs를 끝내고 나서야 cycle 마킹
* - 위에있는 cnt를 세는 부분에서 flag로 사용된다
*/
cycle[cur] = true;
}

int main() {
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

int T; cin >> T;
while (T--) {
cin >> n;
cnt = 0;

for (int i=1; i<=n; ++i) {
cin >> choice[i];
visited[i] = cycle[i] = false;
}

for (int i=1; i<=n; ++i) {
if (!visited[i]) dfs(i);
}
cout << n-cnt << '\n';
}
return 0;
}

0 comments on commit 9c4269e

Please sign in to comment.