Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
8.9 kB
2
Indexable
Never
There is an airplane game which to avoid enemies and gather coins.

The game's map has height is N and width is 5 (5 ≤ N ≤ 12), but due to the limit of the screen, the gaming zone is 5x5. Below the gaming zone, there is control zone, which is one line at the bottom of the screen where airplane move.

At the start of game, the airplane locates at center point of control zone.

Game rule :

         - Movement of airplane can be one of 3 options : left - right - stay at current column

         - In each turn, after airplane move, game map will move down one line

         - There is an option to use Bomb : bomb can be used to destroy all enemies in gaming zone, after used, all enemies will disappear and all coins will remain in the map. Bomb can be used only one.

         - When airplane meets a cell with coin, number of coins collected will increase by 1, if it meet an enemy, number of coins will decrease by 1. If number of coins < 0 -> Game Over.

Given the map Nx5 with C is value of each cell (0: nothing, 1: coin, 2:enemy), find out the maximum amount of coins can be achieved after finishing the game. If the game can not be finished (Game Over), return -1.

[Input]

The first line is the total number of test cases T ( T <= 50)

The first line of each test case contain N, which is the height of the map, then the N lines following descript the map's data.

[Output]

The maximum number of coins that can be collected after finishing the game.

If the game can not be finished, print -1.

 

 

Ex:

Input

2

5

1 1 0 0 0

1 2 2 2 1

1 1 2 2 1

2 2 2 1 2

2 2 0 2 0

8

2 0 2 0 2

1 0 1 2 0

0 0 0 2 1

2 0 2 0 1

1 2 1 2 0

0 2 2 0 2

2 1 1 2 2

0 2 1 2 0

 

Output

Case #1

3

Case #2

4
//////////////////////////////////////////////////
50
7
2 1 2 2 0
0 2 2 0 0
2 0 2 1 0
0 0 2 1 2
0 2 1 0 2
1 2 1 0 2
1 1 2 0 2
5
0 0 1 0 0
1 2 1 0 2
2 2 0 0 1
2 1 2 1 0
2 1 2 0 2
9
2 2 2 2 2
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
2 2 2 2 0
6
2 2 2 2 2
0 0 0 0 0
0 0 2 0 0
2 0 0 0 2
0 0 0 0 0
1 2 2 2 1
10
1 0 1 0 1
2 2 2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 2
8
1 0 2 1 0
0 2 1 0 2
2 0 0 0 1
0 0 2 2 0
1 2 1 0 2
0 1 1 1 1
1 2 1 2 1
2 0 0 1 2
9
1 2 1 0 2
2 2 2 1 1
2 1 1 2 1
1 2 1 0 2
1 2 1 1 0
1 1 1 2 2
0 1 0 2 0
0 0 0 0 0
1 2 0 0 1
8
2 2 0 0 1
2 1 2 2 1
0 0 0 1 1
0 2 1 1 0
0 2 2 1 1
1 0 0 0 2
2 1 2 2 1
1 1 2 0 0


//////////////////////////////////
#include<iostream>

using namespace std;
int N;
int map[15][5];
// màn hình chơi game chỉ có 5*5
int mapcop[5][5];
int ans;
int dx[] = {-1,-1,-1}; // trái, giữa, phải
int dy[] = {-1, 0, 1};
void reset(){
   // mảng cố định và mảng 5*5
	for (int i = 0; i < N; i++){
			for ( int j = 0; j < 5; j++){
			   map[i][j] = 0;
			}
	}
	for (int i = 0; i < 5; i++){
			for ( int j = 0; j < 5; j++){
				mapcop[i][j] = 0;
			}
	}
}
void backtrack(int x, int y, int sum){
	// lên đỉnh
	if(x == 0){
	  if(ans < sum) ans = sum;
	  return;
	}
	// đi 3 hướng
	for(int i = 0; i < 3; i++){
	  int tempx = x + dx[i];
	  int tempy = y + dy[i];
	  if(tempx >= 0 && tempx < N && tempy >= 0 && tempy < 5){
		  backtrack(tempx,tempy, sum + map[tempx][tempy]);
	  }
	}
}

void No_map(int x){
	// tính từ vị trí dùng
       for (int i = x; i < 5+x; i++){
			for ( int j = 0; j < 5; j++){
		        mapcop[i-x][j] = map[i][j]; // copy mảng trước khi nổ
				// cho nổ hết các giá trị  -1(hay chính là 2)
				if(map[i][j] == -1){
				   map[i][j] = 0;
				}
			}
		}
}
/// trả lại giá trị vào mảng 
void Push_map(int x){
	for(int i = x; i < 5+x; i++){
		for(int j = 0; j < 5; j++){
			map[i][j] = mapcop[i-x][j]; // gán lại mảng sau mỗi lần nổ xong
		}
	}
}
int main(){

	int TC; cin >> TC;
	for (int tc = 1; tc <= TC; tc++){
		reset();
		cin >> N;
		for (int i = 0; i < N; i++){
			for ( int j = 0; j < 5; j++){
			  cin >> map[i][j];
			  if(map[i][j] == 2){
			     map[i][j] = -1;
				 }
			  }
			}
		ans = -1;
		for(int i = 0; i <= N-5; i++){
			No_map(i); // nổ map
			backtrack(N,2,0);
			Push_map(i); // gán lại map cho bước di chuyển mới
		}
		cout << "Case #" << tc << endl;
		cout << ans << endl;
		}
	return 0;
}
Leave a Comment