[구현]새로운 게임2 boj 17837

Modyhoon·2021년 3월 22일
0
post-thumbnail
  • 17837

    https://www.acmicpc.net/problem/17837

    • algorithm 문제에서 처음으로 구조체를 만들어서 써보았다.

    • 이동하거나, 방향을 바꾸는 등의 작업들은 배열을 만들어서 대응시키면 좋다.

      • 스스로 떠올리지 못했던 방법

        int turn_d[5] = {0, 2, 1, 4, 3};
        
        d = turn_d[d];

        방향을 바꿀 때 이렇게 기존 index를 넣었을 때 바꾸고자하는 index가 튀어나오도록 배열을 정해주면 쉽게 바꿀 수 있다.

        if (d == 1)
        	d = 2
        else if (d == 2)
        	d = 1
        else if (d == 3)
        	d = 4
        else if (d == 4)
        	d = 3
        
        이런 수고를 덜어줄 수 있다는 뜻이다.
    • 특히 이번에 배운 점중에 에 들어있는 reverse, find함수를 사용하면 편하다는 점이다.

      • find함수를 이용해 vector안에 있는 특정 원소의 위치를 가지고 있는 pointer를 받아올 수 있다.
        • 이 pointer를 이용해, 특정 index가 있는곳 부터 어느 지점까지 reverse를 할 수 있고,
        • 또한 erase를 통해 범위 내 원소들을 싹다 지울 수 있다.
        • 사실 python을 사용하면 이런건 엄청 쉬울듯
    • 또한, find로 찾아낸 pointer의 type은 auto로 지정하여 간편하게 자료형을 지정해 줄 수 있다.

      auto it = find(cur.begin(), cur.end(), i);
      
      이 코드는 아래와 상동이다.
      
      vector<int>::iterator it = find(cur.begin(), cur.end(), i);
    • 어떤 원소를 다룰 때, "&" 를 통해 reference로 호출하면 코드의 가독성을 늘릴 수 있을 뿐만 아니라, 컴파일을 빠르게 할 수 있다는 장점이 있다.

      vector<int>& cur = st[h[i].y][h[i].x];
      
      이런식으로 &로 해두면 사본이 아닌 원본을 참조하기 때문에
      cur를 조정하면 st안에있는 내용이 바뀌게 된다.
    • 또한, 어떤 값 이상인지 판별하기 위해서 함수의 return값을 그 값으로 주어 간편하게 판단할 수 있도록 한다.

    • 즉, move를 한 번 했을 때 해당 좌표에 있는 원소의 개수를 반환해주면 바뀐 좌표의 원소의 개수를 가지고 원하는 조건과 비교할 수 있다.

      int move(int i)
      {
      	...
      
      return (next.size());
      }
      
      if (move(i) >= 4)
      	...

      코드

      #include <iostream>
      #include <vector>
      #include <algorithm>
      
      using namespace std;
      
      struct horse
      {
      	int x, y, d;
      }		h[11];
      int N, K;
      int dx[5] = {0, 1, -1, 0, 0};
      int dy[5] = {0, 0, 0, -1, 1};
      int maps[14][14];
      int turn_d[5] = {0, 2, 1, 4, 3};
      vector<int> st[14][14];
      
      int move(int i)
      {
      	int ny = h[i].y + dy[h[i].d];
      	int nx = h[i].x + dx[h[i].d];
      	if (ny > N || nx > N || nx <= 0 || ny <= 0 || maps[ny][nx] == 2)
      	{
      		h[i].d = turn_d[h[i].d];
      		ny = h[i].y + dy[h[i].d];
      		nx = h[i].x + dx[h[i].d];
      		if (ny > N || nx > N || nx <= 0 || ny <= 0 || maps[ny][nx] == 2)
      			return (0);
      	}
      	vector<int>& cur = st[h[i].y][h[i].x];
      	vector<int>& next = st[ny][nx];
      
      	auto it = find(cur.begin(), cur.end(), i);
      
      	if (maps[ny][nx] == 1)
      		reverse(it, cur.end());
      	for (auto m = it; m != cur.end(); m++)
      	{
      		h[*m].x = nx;
      		h[*m].y = ny;
      		next.push_back(*m);
      	}
      	cur.erase(it, cur.end());
      	return (next.size());
      }
      
      int main(void)
      {
      	cin >> N >> K;
      	for (int i = 1; i <= N; i++)
      		for (int j = 1; j <= N; j++)
      			cin >> maps[i][j];
      	for (int i = 1; i <= K; i++)
      	{
      		cin >> h[i].y >> h[i].x >> h[i].d;
      		st[h[i].y][h[i].x].push_back(i);
      	}
      	for (int count = 1; count <= 1000; count++)
      	{
      		for (int i = 1; i <= K; i++)
      		{
      			if (move(i) >= 4)
      			{
      				cout << count << endl;
      				return (0);
      			}
      		}
      	}
      	cout << "-1" << endl;
      	return (0);
      }
profile
어제보다 나은 오늘

0개의 댓글