-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from HYU-PS-STUDY/wonkyDD
[week-10] 1890, 9466
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |