Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

자이의 프로그래밍

SWEA-2819 격자판의 숫자 이어붙이기 본문

Algorithm/Cases-Study

SWEA-2819 격자판의 숫자 이어붙이기

Xi_kor 2021. 4. 16. 19:17

숫자를 이어붙였다. 맵의 범위를 벗어나면 다시 찾게 했고 총 7자리의 수를 찾았을 때 체크해주면서 찾은 수를 하나씩 더해주었다. 

#include <iostream>
#include <cstring>
using namespace std;

int ch[10000100];
int arr[10][10];
int total = 0;

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

void checking(int x, int y, int cnt, int val) {
	if (cnt == 7) {
		if (ch[val] != 1) {
			ch[val] = 1;
			total += 1;
		}
		return;
	}
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];;
		if (nx < 0 || ny < 0 || nx >= 4 || ny >= 4) continue;
		checking(nx, ny, cnt + 1, val * 10 + arr[nx][ny]);
	}
}

int main() {
	int tc;
	cin >> tc;

	for (int T = 1; T <= tc; T++) {
		total = 0;
		memset(ch, 0, sizeof(ch));
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				cin >> arr[i][j];
			}
		}

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				checking(i, j, 1, arr[i][j]);
			}
		}
		cout << "#" << T << " " << total << '\n';
	}
	return 0;
}