๊ตฌํ์ด๋?
๋จธ๋ฆฟ์์ ์๋ ์๊ณ ๋ฆฌ์ฆ์ ์์ค์ฝ๋๋ก ๋ฐ๊พธ๋ ๊ณผ์
์์ ํ์ : ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฃผ์ ์์ด ๋ค ๊ณ์ฐํ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ
์๋ฎฌ๋ ์ด์ : ๋ฌธ์ ์์ ์ ์ํ ์๊ณ ๋ฆฌ์ฆ์ ํ ๋จ๊ณ์ฉ ์ฐจ๋ก๋๋ก ์ง์ ์ํํด์ผํ๋ ๋ฌธ์ ์ ํ
์์ ํ์, ์๋ฎฌ๋ ์ด์ ์ ํ์ ๋ชจ๋ '๊ตฌํ' ์ ํ์ผ๋ก ๋ฌถ์ด์ ๋ค๋ฃฌ๋ค.
[์์ 1] ์ํ์ข์ฐ
#include <iostream>
using namespace std;
//์ผ ์ค ์ ์๋
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char moveTypes[4] = {'L', 'R', 'U', 'D'};
int main() {
int n, x = 1, y = 1;
string plans;
cin >> n;
cin.ignore(); //๋ฒํผ ์ง์ฐ๊ธฐ getline ์ ์ ํด์ผํจ
getline(cin, plans); //์ํฐ๋ฅผ ๋ฒํผ์ ๋จ๊น
for (char plan: plans) {
if (plan == ' ') {
continue;
} else {
int nx = 0, ny = 0;
for (int j = 0; j < 4; j++) {
if (plan == moveTypes[j]) {
nx = x + dx[j];
ny = y + dy[j];
break;
}
}
if (nx < 1 || nx > n || ny < 1 || ny > n) {
continue;
} else {
x = nx;
y = ny;
}
}
}
cout << x << " " << y;
return 0;
}
[์์ 2] ์๊ฐ
18312๋ฒ: ์๊ฐ
์ ์ N๊ณผ K๊ฐ ์ ๋ ฅ๋์์ ๋ 00์ 00๋ถ 00์ด๋ถํฐ N์ 59๋ถ 59์ด๊น์ง์ ๋ชจ๋ ์๊ฐ ์ค์์ K๊ฐ ํ๋๋ผ๋ ํฌํจ๋๋ ๋ชจ๋ ์๊ฐ์ ์ธ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์๊ฐ์ ์ ๋๋ ๋์งํธ ์๊ณ๋ฅผ ๊ธฐ์ค์ผ๋ก,
www.acmicpc.net
#include <iostream>
using namespace std;
int main() {
int n, k, cnt = 0;
cin >> n >> k;
for (int i = 0; i <= n; i++) { //์
for (int j = 0; j < 60; j++) { //๋ถ
for (int z = 0; z < 60; z++) { //์ด
if (i % 10 == k || i / 10 == k ||
j % 10 == k || j / 10 == k ||
z % 10 == k || z / 10 == k) {
cnt++;
}
}
}
}
cout << cnt;
return 0;
}
[์ค์ 1] ์์ค์ ๋์ดํธ
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int dx[4] = {-1, 1, 0, 0}; //์ ์๋ ์ผ ์ค
int dy[4] = {0, 0, -1, 1};
vector<char> ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
bool isScope(int nx, int ny) {
if (nx < 0 || nx > 7 || ny < 0 || ny > 7)
return false;
return true;
}
int main() {
string s;
int x, y, cnt = 0;
cin >> s;
x = s[1] - '0' - 1;
for (int i = 0; i < ch.size(); i++) {
if (s[0] == ch[i]) {
y = i;
}
}
for (int i = 0; i < 4; i++) { //4๋ฐฉํฅ
int nx = x, ny = y;
for (int j = 0; j < 2; j++) { //๋๋ฒ๊ฐ
nx += dx[i];
ny += dy[i];
}
if (!isScope(nx, ny))
continue;
if (i > 1) { //์ํ์ผ ๋
for (int z = 0; z < 2; z++) {
nx += dx[z];
ny += dy[z];
if (isScope(nx, ny))
cnt++;
}
} else { //์์ง์ผ ๋
for (int z = 2; z < 4; z++) {
nx += dx[z];
ny += dy[z];
if (isScope(nx, ny))
cnt++;
}
}
}
cout << cnt;
return 0;
}
์ง์ง ํ๋์ฝ๋ฉํ๋๋ฐ ... ๋ฐฉํฅ๋ง ๋ฏธ๋ฆฌ์ ํด์ฃผ๋ฉด ๊ฐ๋จํ ํ๋ฆฌ๋ ๋ฌธ์ ์๋ค ใ
#include <iostream>
#include <vector>
using namespace std;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
vector<char> ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
int main() {
string s;
int x, y, cnt = 0;
cin >> s;
x = s[1] - '0' - 1;
for (int i = 0; i < ch.size(); i++) {
if (s[0] == ch[i]) {
y = i;
}
}
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx > 7 || ny < 0 || ny > 7)
continue;
cnt++;
}
cout << cnt;
return 0;
}
[์ค์ 2] ๊ฒ์ ๊ฐ๋ฐ
#include <iostream>
using namespace std;
int n, m, x, y, direction, cnt = 1, turn_time = 0;
bool visit[50][50];
int map[50][50];
int dx[] = {-1, 0, 1, 0}; //๋ถ ์ ๋จ ๋
int dy[] = {0, -1, 0, 1};
int main() {
cin >> n >> m;
cin >> x >> y >> direction;
visit[x][y] = true;
// ์ ์ฒด ๋งต ์ ๋ณด๋ฅผ ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
while (true) {
direction = (direction + 1) % 4;
int nx = x + dx[direction];
int ny = y + dy[direction];
if (map[nx][ny] == 1 || visit[nx][ny]) { // ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์๊ฑฐ๋ ๋ฐ๋ค์ธ ๊ฒฝ์ฐ
turn_time += 1;
} else { // ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์ด๋
visit[nx][ny] = true;
x = nx;
y = ny;
cnt += 1;
turn_time = 0;
continue;
}
// ๋ค ๋ฐฉํฅ ๋ชจ๋ ๊ฐ ์ ์๋ ๊ฒฝ์ฐ
if (turn_time == 4) {
nx = x - dx[direction];
ny = y - dy[direction];
// ๋ค๋ก ๊ฐ ์ ์๋ค๋ฉด ์ด๋ํ๊ธฐ
if (map[nx][ny] == 0) {
x = nx;
y = ny;
} else // ๋ค๊ฐ ๋ฐ๋ค๋ก ๋งํ์๋ ๊ฒฝ์ฐ
break;
turn_time = 0;
}
}
cout << cnt;
return 0;
}
'โ๏ธAlgorithm > ๐ ์ด์ทจ์ฝ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด์ทจ์ฝ] 10. ๊ทธ๋ํ ์ด๋ก - C++ (0) | 2022.10.22 |
---|---|
[์ด์ทจ์ฝ] 9. ์ต๋จ๊ฒฝ๋ก - C++ (0) | 2022.10.22 |
[์ด์ทจ์ฝ] 8. DP - C++ (0) | 2022.10.21 |
[์ด์ทจ์ฝ] 5. DFS/BFS - C++ (0) | 2022.10.20 |
[์ด์ทจ์ฝ] 1. ๊ทธ๋ฆฌ๋(ํ์๋ฒ) - C++ (0) | 2022.10.04 |