Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
#include <iostream>
#include <stdio.h>
using namespace std;
int n, e, k, t;
int data[1001][5];
int dy[] = {-1, 0, 1};
int maxx = 0;
int vtdx,vtdy;
int van = 1;
int ktra = 0;

void backtrack(int k, int h, int count) {
	if (k == 0) {
		ktra = 1;
		if (count > maxx) {
			maxx = count;
		}
		return;
	}

	for (int j = 0; j < 3; j++) {
		if ( k-1 >=0 && h + dy[j] >= 0 && h+dy[j] < 5 && (van == 1 || data[k-1][h+dy[j]] != 2) ) {
			if (data[k-1][h+dy[j]] == 2) {
				van = 0;
				backtrack(k-1, h+dy[j], count);
				van = 1;
			}

			else {
				backtrack(k-1, h+dy[j], count + data[k-1][h+dy[j]]);
			}
		}
		
	}
}

int main() {
	//freopen("input.txt", "r", stdin);
	int sl;
	cin >> sl;
	for (int stt = 1; stt <= sl; stt++) {
	   maxx = 0; van = 1;ktra = 0;
	   cin >> n;
	   for (int i = 0; i < n; i++) {
		   for (int j = 0; j < 5; j++) {
			   data[i][j] = 0;
		   }
	   }
	   vtdx = n-1;
	   vtdy = 2;
	   for (int i = 0; i < n; i++) {
		   for (int j = 0; j < 5; j++) {
			   cin >> data[i][j];
		   }
	   }

	   backtrack(n, 2, 0);
	   cout <<"#"<<stt<<" ";
	   if (ktra) {
	   cout << maxx<<endl;
	   }
	   else {
		   cout << -1<<endl;
	   }

		
	}
		return 0;
}