이코테_게임 개발

·2022년 6월 1일

이코테_알고리즘

목록 보기
17/23

코드

: 뒤로 가는거는 안함.

#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;



int main()
{
	int n, m;
	cin >> n >> m;

	vector<vector<int>>v(n, vector<int>(m, 0));

	//0 : 위 / 1 : 오른 / 2 : 아래 / 3 : 왼

	// (0,0) 부터 시작함.
	int x, y, dir;
	cin >> x >> y >> dir;

	int cur_x = x;
	int cur_y = y;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> v[i][j];
		}
	}
	// 기존
	// 좌표 : (y, x) 
	// 0 북 , 1 동, 2 남 , 3 서 
	// ( -1 , 0) , (0 , 1) , (1 , 0) , (0 , -1)
	//int trans_x[]{ 0,1,0,-1 };
	//int trans_y[]{ -1,0,1,0 };

	// 0 북 -> 1 서 -> 2 남 -> 3 동 순으로 변경하자.
	int trans_x[]{ 0,-1,0,1 };
	int trans_y[]{ -1,0,1,0 };
	string word[]{ "위_북","왼_서","밑_남","오_동" };
	// 동 쪽 3번에서는 다시 북으로 복귀해야 함.
	int cnt{ 0 };
	//현재 있는 칸은? 
	while (1)
	{
		//현재 위치로부터 왼쪽으로 돌아야 함. 
		// 북(-1 , 0) -> 서(0 , -1) 로 / 서(0, -1) -> 남(1 , 0) 으로 
		// 남(1 , 0) -> 동(0 , 1) / 동(0 , 1) -> 북(-1 , 0)
		bool isEnd = false;

		for (int i = 0; i < 4; i++)
		{
			//현재 위치에서 왼쪽으로 돌리다가 가장 먼저 0을 발견하면 
			// 그 지점으로 이동함. 
			// 그 지점은 체크 표시해야 함.

			// 1단계 : 왼쪽으로 회전하기.
			++dir;
			// 조건 : 동쪽일 경우 북으로 변경함.
			if (dir == 4)
			{
				dir = 0;
			}

			int n_x = cur_x + trans_x[dir];
			int n_y = cur_y + trans_y[dir];

			if (n_x >= 0 && n_x <= n && n_y >= 0 && n_y <= m)
			{
				//if (n_y == cur_y && n_x == cur_x)
				//	continue;

				// 갈수 있는 지형일 경우.
				if (v[n_y][n_x] == 0)
				{
					// 자기 자신일 경우 pass 
					// 이동 함.
					v[cur_y][cur_x] = 1;
					cur_x = n_x;
					cur_y = n_y;
					
					++cnt;
					
					isEnd = true;
					cout << word[dir] << endl;
					// 방향 이동을 출력하자.
					//if()

					break;
				}
			}
		}

		if (isEnd == false)
		{
			cout << cnt + 1;
			return 0;

		}
	}
	cout << cnt + 1;
}
profile
🔥🔥🔥

0개의 댓글