Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1-kokeunho #1

Merged
merged 2 commits into from
Oct 1, 2024
Merged

1-kokeunho #1

merged 2 commits into from
Oct 1, 2024

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ - ν‚Ή] (https://www.acmicpc.net/problem/1063)

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ λ°˜μ •λ„ κ±Έλ¦° 것 κ°™μŠ΅λ‹ˆλ‹€

✨ μˆ˜λ„ μ½”λ“œ

  1. ν‚Ή, 돌, 이동 횟수λ₯Ό μž…λ ₯ λ°›κ³  움직일 λ°©ν–₯을 이동 횟수만큼 λ°°μ—΄λ‘œ λ°›λŠ”λ‹€
  2. ν‚Ήκ³Ό 돌의 μœ„μΉ˜λ‘œ 받은 λ¬Έμžμ—΄μ„ μ •μˆ˜λ‘œ μ •λ¦¬ν•œλ‹€
  3. μ›€μ§μž„ λ°©ν–₯을 μ •μ˜?ν•œλ‹€
  4. 이동 횟수만큼 λ°˜λ³΅λ¬Έμ„ λŒλ©΄μ„œ 킹이 움직인 ν›„ μœ„μΉ˜λ₯Ό κ³„μ‚°ν•˜κ³  λ‹€μŒ κ²½μš°μ— 따라 ν‚Ήκ³Ό 돌의 μœ„μΉ˜λ₯Ό λ³€κ²½ν•΄μ€€λ‹€.
  • ν‚Ήκ³Ό 돌이 겹쳐질 경우
    ν‚Ήκ³Ό 돌 λͺ¨λ‘ μœ„μΉ˜λ₯Ό λ³€κ²½
  • 그렇지 μ•Šμ€ 경우
    ν‚Ήλ§Œ μœ„μΉ˜λ₯Ό λ³€κ²½
  1. μ •μˆ˜μΈ ν‚Ήκ³Ό 돌의 xμ’Œν‘œλ₯Ό μœ λ‹ˆμ½”λ“œλ‘œ λ³€κ²½ν•˜μ—¬ 좜λ ₯ν•œλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

사싀 νŒŒμ΄μ¬μ„ μ‹œμž‘ν•œμ§€ μ–Όλ§ˆ μ•ˆλΌμ„œ κ·Έλ‚˜λ§ˆ λ§Œλ§Œν•΄λ³΄μ΄λŠ” 문제둜 ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.
파이썬 문법 λͺ‡ 개λ₯Ό λ°°μ› μŠ΅λ‹ˆλ‹€.

  1. N개의 λ¬Έμžμ—΄μ„ λ°›μ•„ 배열에 λ„£κΈ°
A = [input() for _ in range(N)]
  1. μœ λ‹ˆμ½”λ“œ -> μ •μˆ˜ ex) A->65
A = int(ord('A'))
  1. μ •μˆ˜ -> μœ λ‹ˆμ½”λ“œ ex) 65->B
A = 65
B = chr(A)

그리고

move_way = ['R', 'L', 'T', 'B', 'RT', 'RB', 'LT', 'LB']
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]

μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ λ°©ν–₯에 따라 x, y μ’Œν‘œ 이동을 맀핑? ν•˜λŠ” 방법도 λ°°μ› μŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ’Œν‘œν‰λ©΄μ—μ„œ 이동할 λ°©ν–₯을 λ‘κ°œμ˜ λ°°μ—΄λ‘œ λ‚˜νƒ€λ‚΄λŠ” 방식 μ˜€λžœλ§Œμ— λ³΄λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.
쀑간쀑간 λ‚΄ μ„€μ •ν•œ offsetκ·œμΉ™(xκ°€ col, yκ°€ row λ“±)을 κΉŒλ¨Ήμ–΄μ„œ ν—·κ°ˆλ Έλ˜ 적이 μžˆλŠ” 문제 μœ ν˜•μ΄λΌμ„œ ν•œ 번 ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.
μ΄ν•΄ν•˜κΈ° μ‰½κ²Œ μˆ˜λ„μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ—¬μ„œ μˆ˜λ„μ½”λ“œλ₯Ό λ³΄λ©΄μ„œ c++둜 λΉ λ₯΄κ²Œ ν’€ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

그리고 μœ λ‹ˆμ½”λ“œμ™€ μ •μˆ˜ 관계가 맀번 ν—·κ°ˆλ ΈλŠ”λ° 이번 κΈ°νšŒμ— ν™•μ‹€νžˆ μ•Œκ²Œ 된 것 κ°™μŠ΅λ‹ˆλ‹€!

int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, -1, 1, 1, 1, -1, -1};

void getPosition(string& pos, int& x, int & y){
   x = pos[0] - 'A' + 1;
   y = pos[1] - '0';
}

bool moveStone(int dir){
   int sx_ = sx + dx[dir];
   int sy_ = sy + dy[dir];
   if (sx_ < 1 || sx_ > 8 || sy_ < 1 || sy_ > 8) return false;
   
   sx = sx_;
   sy = sy_;

   return true;
}

void moveKing(int dir){
   int kx_ = kx + dx[dir];
   int ky_ = ky + dy[dir];
   if (kx_ < 1 || kx_ > 8 || ky_ < 1 || ky_ > 8) return;
   
   if (kx_ == sx && ky_ == sy)
       if (!moveStone(dir)) return;
   
   kx = kx_;
   ky = ky_;
}

@wnsmir
Copy link
Collaborator

wnsmir commented Oct 1, 2024

λ¬Έμ œν‘ΈλŠ”λ°λŠ” μ „ν˜€ 지μž₯μ—†μ§€λ§Œ λ³€μˆ˜λͺ…이 SX SYλ₯Ό StoneX StoneY King 과같이 쑰금 더 직관적이면 μ’‹μ„κ²ƒκ°™μŠ΅λ‹ˆλ‹€. λ‹€λ₯Έμ μ€ μ½”λ“œ κ°„κ²°ν•˜κ³  κΉ”λ”ν•œκ²ƒκ°™μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!! 저도 제 λ‚˜λ¦„λŒ€λ‘œ ν’€μ–΄λ³΄μ•˜μ–΄μš” :)

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

// μ™Όμͺ½ μ•„λž˜λ₯Ό {0, 0} 으둜 μ„€μ •
unordered_map<string, pair<int, int>> dir = {
	{"R",{1, 0} },
	{"L",{-1, 0} },
	{"B",{0, -1} },
	{"T",{0, 1} },
	{"RT",{1, 1} },
	{"LT",{-1, 1} },
	{"RB",{1, -1} },
	{"LB",{-1, -1} }
};

int king[2];
int stone[2];
int n;
vector<string> moves;

void readInput()
{
	string str;
	cin >> str;
	king[0] = str[0] - 'A';
	king[1] = str[1] - '1';

	cin >> str;
	stone[0] = str[0] - 'A';
	stone[1] = str[1] - '1';

	cin >> n;
	moves.resize(n);
	for (int i = 0; i < n; i++)
		cin >> moves[i];
}

bool moveStone(string index) {
	int stoneX = stone[0] + dir[index].first;
	int stoneY = stone[1] + dir[index].second;
	if(stoneX < 0 || stoneX > 7 || stoneY < 0 || stoneY > 7) return false;
	stone[0] = stoneX;
	stone[1] = stoneY;
	return true;
}

void moveKing(string index)
{
	int kingX = king[0] + dir[index].first;
	int kingY = king[1] + dir[index].second;
	if (kingX < 0 || kingX > 7 || kingY < 0 || kingY > 7) return;
	if (kingX == stone[0] && kingY == stone[1])
		if (!moveStone(index)) return;

	king[0] = kingX; king[1] = kingY;
}

int main()
{
	readInput();
	for (int i = 0; i < n; i++)
		moveKing(moves[i]);

	cout << (char)('A' + king[0]) << (char)('1' + king[1]) << '\n';
	cout << (char)('A' + stone[0]) << (char)('1' + stone[1]);

	return 0;
}

ν’€μ–΄λ³΄λŠ”λ° μ’€ 였랜 μ‹œκ°„μ„ μΌλ„€μš”...

μˆ˜λ„μ½”λ“œ 덕에 둜직 κ΅¬ν˜„μ€ μ‰½κ²Œ ν–ˆμ§€λ§Œ μž…λ ₯받은 String값을 μ–΄λ–»κ²Œ direction으둜 λ§€ν•‘μ‹œν‚¬μ§€ κ³ λ―Όν•˜λŠ”λ° μ‹œκ°„μ„ μ’€ μΌμŠ΅λ‹ˆλ‹€. μ €λŠ” cpp의 unordered_map을 μ‚¬μš©ν•΄μ„œ ν•œλ²ˆμ— λ¬Άμ–΄λ²„λ ΈμŠ΅λ‹ˆλ‹€..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants