본문 바로가기
문제/백준

c++ 백준_17070_"파이프 옮기기1"

by 나 진짜 못차마 2023. 6. 13.
728x90

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

 

이동 할 수 있는 부분은 정해져있고

공통적으로 오른쪽 아래 방향으로 내려가기 때문에

따로 방문 체크를 할 필요가 없었다.

그리고 파이프 앞부분 좌표 기준으로만 움직인다.

그래서 파이프 앞부분 좌표 기준,

가로, 세로, 대각선 중 현재 파이프 상태를 보고

갈 수 있는 방향을 향해 진행했다.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int N;
int house[17][17];

enum dir { GARO, SERO, DAEGAK};

typedef struct {
	int x, y;
	int status;
}node;

bool is_in(int x, int y) {
	if (x<1 || y<1 || x > N || y > N || house[x][y] == 1)
		return false;
	return true;
}

void bfs() {

	queue<node> q;

	q.push({ 1,2,GARO });
	
	int cnt = 0;

	while (!q.empty()) {
		int x = q.front().x;
		int y = q.front().y;
		int status = q.front().status;
		q.pop();
	
		if (x == N && y == N) {
			cnt++;
			continue;
		}

		if (status == GARO) {

			// 가로
			if (is_in(x, y + 1)) {
				q.push({ x ,y + 1, GARO });
			}
			// 대각
			if (is_in(x, y + 1) && is_in(x+1,y) && is_in(x+1,y+1)) {
				q.push({ x + 1 ,y + 1, DAEGAK });
			}

		}
		else if (status == SERO) {

			// 세로
			if (is_in(x + 1, y)) {
				q.push({ x + 1 ,y,SERO });
			}
			// 대각
			if (is_in(x, y + 1) && is_in(x + 1, y) && is_in(x + 1, y + 1)) {
				q.push({ x + 1 ,y + 1, DAEGAK });
			}
		}
		else {
			// 가로
			if (is_in(x, y + 1)) {
				q.push({ x ,y + 1, GARO });
			}
			// 세로
			if (is_in(x + 1, y)) {
				q.push({ x + 1 ,y, SERO });
			}
			// 대각
			if (is_in(x, y + 1) && is_in(x + 1, y) && is_in(x + 1, y + 1)) {
				q.push({ x + 1 ,y + 1, DAEGAK });
			}

		}


	}
	cout << cnt ;
}

int main(void) {
	
	cin >> N;

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			cin >> house[i][j];
		}
	}
	
	bfs();

}

728x90