Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
961 B
1
Indexable
Never
#include<iostream>
using namespace std;

int n, a[20][5];
int answer = 0;
int dy[3] = {-1,0,1};

void Try(int row, int col, int coin, int van) {
	if(a[row][col] == 2 && van == 0) { 
		return;
	} else if(a[row][col] == 2 && van == 1) { 
		van--;
		coin -= 2; 
	}
	if(row == 0) {
		if(coin > answer) { 
			answer = coin;
		}
		return;
	}
	for(int i = 0; i < 3; i++) { 
		int xx = row - 1;
		int yy = col + dy[i];
		if (xx >= 0 && yy >= 0 && yy < 5) {
			Try(xx, yy, coin + a[xx][yy], van); 
		}
	}
}

void reset() { 
	for(int i = 0 ; i < 20; i++) { 
		for(int j = 0; j < 5; j++) { 
			a[i][j] = 0;
		}
	}
}

int main() {
	//freopen("input.txt", "r", stdin);
	int T; 
	cin>>T;
	for(int tc = 1; tc <= T; tc++) { 
		cin>>n;
		reset();
		for(int i = 0; i < n; i++) { 
			for(int j = 0; j < 5; j++) { 
				cin>>a[i][j];
			}
		}
		answer = -1;
		Try(n, 2, 0, 1);
		cout<<"#"<<tc<<" "<<answer<<endl;
	}
	return 0;
}