Untitled
unknown
plain_text
2 years ago
4.1 kB
10
Indexable
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 #include<iostream> using namespace std; int a[25][5]; int X[3] = { 1,-1, 0}; int Y[3] = {-1,-1, -1}; int ans; int n; void Try(int row, int col, int bomb, int sum){ if(sum ==-1) return; if(row ==0){ if (ans < sum )ans = sum; return; } for(int h =0; h<3; h++){ int r = row +Y[h]; int c = col +X[h]; if(r >=0 && r<n && c>=0 && c<5){ if ( a[r][c] <2 ){ Try(r, c, bomb, sum + a[r][c]); } if (a[r][c] == 2){ if(bomb){ for(int i =r; i>r-5; i--){ for(int j =0; j<5; j++){ if(a[i][j] == 2) a[i][j] = 3; } } Try(r,c,0,sum); for(int i =r; i>r-5; i--){ for(int j =0; j<5; j++){ if(a[i][j] == 3) a[i][j] = 2; } } } else { Try(r,c,0,sum-1); } } if (a[r][c] == 3){ Try(r,c,0,sum); } } } } int main(){ freopen("Text.txt", "r", stdin); int test; cin >> test; for(int tc= 1; tc <= test; tc++){ cin >> n; for(int i=0; i < n; i++){ for(int j= 0; j < 5; j++){ cin >> a[i][j]; } } ans =-1; Try(n, 2, 1, 0); cout << "Case #" << tc << endl; cout << ans << endl; } return 0; } #include<iostream> using namespace std; int ans=-1; int dx[3]={-1,-1,-1}; int dy[3]={0,-1,1}; int N; int a[15][15]; void chay(int r, int c, int sum, int bom) { if(r==0){ if(ans<sum) { ans =sum; } return ; } for(int i=0; i<3;i++){ int x1= r+dx[i]; int x2= c+dy[i]; if( x1>=0 && x1<N && x2>=0 && x2<5){ if(sum>=0){ if(a[x1][x2]<2)chay(x1,x2,sum+a[x1][x2],bom); if(a[x1][x2]==2) { if(bom){ for( int j=x1;j>x1-5;j--){ for(int c =0; c<5; c++){ if(a[j][c]==2){ a[j][c]=3; } } } chay(x1,x2,sum,0); for( int j=x1;j>x1-5;j--){ for(int c =0; c<5; c++){ if(a[j][c]==3){ a[j][c]=2; } } } } else chay(x1,x2,sum-1,0); } else if(a[x1][x2]==3&& sum>=0) chay(x1,x2,sum,0); } } } } int main() { freopen("text.txt","r",stdin); int t; cin>>t; int sum; for(int tc=1; tc<=t;tc++){ cin>>N; for(int i=0;i<N;i++){ for(int j=0; j<5;j++){ cin>>a[i][j]; } } sum=0; ans = -1; chay(N,2,sum,1); cout<<"Case #"<<tc<<endl; cout<<ans<<endl; } return 0; }
Editor is loading...