forked from StepByStep-Algorithm/2021Fall
-
Notifications
You must be signed in to change notification settings - Fork 0
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 StepByStep-Algorithm#35 from LimSangW/main
LimSangW
- Loading branch information
Showing
3 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
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,35 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
typedef long long int ll; | ||
vector<ll> Vec; | ||
|
||
void make(ll n, int k) | ||
{ | ||
Vec.push_back(n); | ||
|
||
for(int i = k - 1; i >= 0; i--) | ||
make(n * 10 + i, i); | ||
} | ||
|
||
int main() | ||
{ | ||
// 입력 속도 업 | ||
ios::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
|
||
for(int i = 0; i < 10; i++) | ||
make(i, i); | ||
|
||
sort(Vec.begin(), Vec.end()); | ||
|
||
int N; | ||
cin >> N; | ||
|
||
ll result = Vec.size() > N ? Vec[N] : -1; | ||
|
||
cout << result << endl; | ||
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,123 @@ | ||
#include <stdio.h> | ||
|
||
int N, M, H; | ||
|
||
int MAP[30 + 5][10 + 5]; | ||
|
||
void input() | ||
{ | ||
scanf("%d %d %d", &N, &M, &H); | ||
|
||
for (int r = 0; r <= H + 1; r++) | ||
for (int c = 0; c <= N + 1; c++) | ||
MAP[r][c] = 3; /* 벽 설치 */ | ||
|
||
for (int r = 1; r <= H; r++) | ||
for (int c = 1; c <= N; c++) | ||
MAP[r][c] = 0; | ||
|
||
for (int i = 0; i < M; i++) | ||
{ | ||
int r, c; | ||
|
||
scanf("%d %d", &r, &c); | ||
|
||
MAP[r][c] = 1; | ||
MAP[r][c + 1] = 2; | ||
} | ||
|
||
return; | ||
} | ||
|
||
void output() | ||
{ | ||
for (int r = 0; r <= H + 1; r++) | ||
{ | ||
for (int c = 0; c <= N + 1; c++) | ||
printf("%d ", MAP[r][c]); | ||
putchar('\n'); | ||
} | ||
} | ||
|
||
int check() | ||
{ | ||
for (int ladder = 1; ladder <= N; ladder++) | ||
{ | ||
int sr, sc; | ||
|
||
sr = 1; | ||
sc = ladder; | ||
|
||
while (1) | ||
{ | ||
if (sr == H + 1) break; | ||
|
||
if (MAP[sr][sc] == 0) sr++; | ||
else | ||
{ | ||
if (MAP[sr][sc] == 1) sc++, sr++; | ||
else sc--, sr++; | ||
} | ||
} | ||
|
||
if (ladder != sc) return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int PASS = 0; | ||
void DFS(int L, int sr, int max) | ||
{ | ||
if (L == max) | ||
{ | ||
// output(); | ||
if (check()) PASS = 1; | ||
return; | ||
} | ||
|
||
for (int r = sr; r <= H; r++) | ||
{ | ||
for (int c = 1; c <= N; c++) | ||
{ | ||
if (MAP[r][c] == 1 || MAP[r][c] == 2 | ||
|| MAP[r][c + 1] == 1 || MAP[r][c + 1] == 3) continue; | ||
|
||
MAP[r][c] = 1; | ||
MAP[r][c + 1] = 2; | ||
|
||
DFS(L + 1, r, max); | ||
|
||
MAP[r][c] = 0; | ||
MAP[r][c + 1] = 0; | ||
} | ||
} | ||
} | ||
|
||
int main(void) | ||
{ | ||
int sr, sc; | ||
|
||
input(); | ||
|
||
if (check()) /* 사다리 설치가 필요 없는 경우 */ | ||
{ | ||
printf("0\n"); | ||
return 0; | ||
} | ||
|
||
for (int setup = 1; setup <= 3; setup++) | ||
{ | ||
DFS(0, 1, setup); | ||
|
||
if (PASS) | ||
{ | ||
printf("%d\n", setup); | ||
return 0; | ||
} | ||
} | ||
|
||
printf("-1\n"); | ||
|
||
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,81 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int arr[9][9]; | ||
vector<pair<int, int>> Vec; | ||
|
||
bool check(int y, int x, int i) | ||
{ | ||
for (int k = 0; k < 9; k++) | ||
{ | ||
if (arr[y][k] == i || arr[k][x] == i) | ||
return false; | ||
} | ||
|
||
for (int k = (y / 3) * 3; k < (y / 3) * 3 + 3; k++) | ||
{ | ||
for (int j = (x / 3) * 3; j < (x / 3) * 3 + 3; j++) | ||
{ | ||
if (arr[k][j] == i) | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool dfs(int n) | ||
{ | ||
if (n == Vec.size()) | ||
return true; | ||
|
||
int y = Vec[n].first; | ||
int x = Vec[n].second; | ||
|
||
for (int i = 1; i <= 9; i++) | ||
{ | ||
if (check(y, x, i)) | ||
{ | ||
arr[y][x] = i; | ||
|
||
if (dfs(n + 1)) | ||
return true; | ||
|
||
arr[y][x] = 0; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
int main() | ||
{ | ||
// 입력 속도 업 | ||
ios::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
|
||
for (auto & i : arr) for (int & j : i) cin >> j; | ||
|
||
for (int i = 0; i < 9; i++) | ||
{ | ||
for (int j = 0; j < 9; j++) | ||
{ | ||
if (arr[i][j] == 0) | ||
Vec.push_back({i, j}); | ||
} | ||
} | ||
|
||
dfs(0); | ||
|
||
cout << endl; | ||
|
||
for (auto & i : arr) | ||
{ | ||
for (int & j : i) cout << j << ' '; | ||
cout << endl; | ||
} | ||
|
||
return 0; | ||
} |