본문 바로가기

c++/Baekjoon Online

백준 17070 파이프 옮기기 1 c++

www.acmicpc.net/problem/17070

 

17070번: 파이프 옮기기 1

유현이가 새 집으로 이사했다. 새 집의 크기는 N×N의 격자판으로 나타낼 수 있고, 1×1크기의 정사각형 칸으로 나누어져 있다. 각각의 칸은 (r, c)로 나타낼 수 있다. 여기서 r은 행의 번호, c는 열의

www.acmicpc.net

그냥 시뮬레이션 기초 그 자체

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

int answer = 0;
int map[20][20] = {0, };
int n;
// first의 값이 똑같으면 가로!
// second의 값이 똑같으면 세로
// 둘다 다르면 대각선

bool in_map(int r, int c)
{
	if (r <= 0 || r > n || c <= 0 || c > n)
		return false;

	return true;
}

void move_pipe(pair<int, int> tail, pair<int, int> head)
{
	if (head.first == n && head.second == n)
	{
		answer++;
		return;
	}
	
	if (head.first == tail.first)//가로!
	{
		if (in_map(head.first, head.second + 1))// 갈곳이 지도 안이고
		{
			if (map[head.first][head.second + 1] == 0)// 옆으로 한칸
				move_pipe(head, make_pair(head.first, head.second + 1));
		}

		if (in_map(head.first + 1, head.second + 1))//대각선
		{
			if(map[head.first+1][head.second + 1] == 0 && map[head.first][head.second + 1] == 0 && map[head.first+1][head.second] == 0)
				move_pipe(head, make_pair(head.first+1, head.second + 1));
		}

	}
	else if (head.second == tail.second)//현재 세로방향
	{
		if (in_map(head.first+1, head.second))
		{
			if (map[head.first+1][head.second] == 0)// 아래로 한칸
				move_pipe(head, make_pair(head.first+1, head.second));
		}
		
		if (in_map(head.first + 1, head.second + 1))//대각선
		{
			if (map[head.first + 1][head.second + 1] == 0 && map[head.first][head.second + 1] == 0 && map[head.first + 1][head.second] == 0)
				move_pipe(head, make_pair(head.first + 1, head.second + 1));
		}
	}
	else//현재 대각선
	{
		if (in_map(head.first, head.second + 1))
		{
			if (map[head.first][head.second + 1] == 0)// 옆으로 한칸
				move_pipe(head, make_pair(head.first, head.second + 1));
		}
		
		if (in_map(head.first + 1, head.second + 1))//대각선
		{
			if (map[head.first + 1][head.second + 1] == 0 && map[head.first][head.second + 1] == 0 && map[head.first + 1][head.second] == 0)
				move_pipe(head, make_pair(head.first + 1, head.second + 1));
		}
		if (in_map(head.first + 1, head.second))
		{
			if (map[head.first + 1][head.second] == 0)// 아래로 한칸
				move_pipe(head, make_pair(head.first + 1, head.second));
		}
	}


}

int main()
{
	pair<int, int> head;
	pair<int, int> tail;
	head = make_pair(1, 2);
	tail = make_pair(1, 1);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int k = 1; k <= n; k++)
		{
			cin >> map[i][k];
		}
	}

	move_pipe(tail, head);

	cout << answer << endl;

	return 0;
}