Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
2
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int N;
int arr[300][5];
int visited[300][5];
int dx[3] = { -1,-1,-1 };
int dy[3] = {-1, 0, 1};
int maxXu;

enum dn {
	DI,
	XU,
	LO
};

void clear_visited() {
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			visited[i][j] = 0;
		}
	}
}

void bt(int row, int col, int van, int sodongxu) {
	if (van < 0) {
		return;
	}
	if (row < 0) {
		if (maxXu == -1) maxXu = 0;
		if (sodongxu > maxXu) maxXu = sodongxu;
		return;
	}
	for (int i = 0; i < 3; i++)
	{
		int x = row + dx[i];
		int y = col + dy[i];
		if (y >= 0 && y < 5 && visited[x][y] == 0) {
			if (arr[x][y] == DI) {
				visited[x][y] = 1;
				bt(x, y, van, sodongxu);
				visited[x][y] = 0;
			}
			if (arr[x][y] == XU) {
				visited[x][y] = 1;
				bt(x, y, van, sodongxu + 1);
				visited[x][y] = 0;
			}
			if (arr[x][y] == LO) {
				visited[x][y] = 1;
				bt(x, y, van-1, sodongxu);
				visited[x][y] = 0;
			}
		}
	}
}

int main() {
//	freopen("input.txt", "r", stdin);

	int T;
	cin >> T;
	for (int tc = 1; tc <= T; tc++)
	{
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < 5; j++)
			{
				cin >> arr[i][j];
			}
		}
		maxXu = -1;
		clear_visited();
		bt(N, 2, 1, 0);
		if (maxXu == -1) {
			cout << "#" << tc << " " << -1 << endl;
		}
		else cout << "#" << tc << " " << maxXu << endl;
	}
	return 0;
}