Răspuns :
#include <iostream>
using namespace std;
void afisare(int a[100][100],int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
int n,m,k,k0,i,j,trag,nr_lov,a[100][100];
cin>>n>>m>>k;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>a[i][j];
}
}
k0=k;
while(k>0){
cin>>trag;
for(j=0;j<m;j++){
nr_lov=0;
for(i=n-1;i>=0;i--){
if(a[i][j]==trag){
nr_lov++;
}
else{
if(nr_lov>0){
a[i+nr_lov][j]=a[i][j];
}
}
}
for(i=0;i<nr_lov;i++){
a[i][j]=0;
}
}
k--;
cout<<"Dupa tragerea "<<k0-k<<":\n";
afisare(a,n,m);
}
return 0;
}
using namespace std;
void afisare(int a[100][100],int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
int n,m,k,k0,i,j,trag,nr_lov,a[100][100];
cin>>n>>m>>k;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>a[i][j];
}
}
k0=k;
while(k>0){
cin>>trag;
for(j=0;j<m;j++){
nr_lov=0;
for(i=n-1;i>=0;i--){
if(a[i][j]==trag){
nr_lov++;
}
else{
if(nr_lov>0){
a[i+nr_lov][j]=a[i][j];
}
}
}
for(i=0;i<nr_lov;i++){
a[i][j]=0;
}
}
k--;
cout<<"Dupa tragerea "<<k0-k<<":\n";
afisare(a,n,m);
}
return 0;
}
Acesta e rezolvarea.
Aici e si codul (nu va fi formatat cum trebuie:
#include <iostream>
using namespace std;
int n,m,k;
//starts from 1
int wall[101][101]={{0, 0, 0, 0, 0, 0},
{0, 3, 5, 4, 5, 1},
{0, 2, 1, 1, 5, 3},
{0, 1, 1, 5, 5, 1},
{0, 5, 5, 1, 4, 3}}; //FOR TESTS-DEFAULT -delete {}
//starts from 1
int weapon_pos,weapon[101]={0,1,5,1}; //FOR TESTS-DEFAULT-delete {}
void show_arr(); //used for debugging-shows the content of 'wall' aray
void Attack_Wall(int weapon); //simulates the attack of the weapon-destroys bricks(marking them with 0)
void Simulate_Gravity(); //simulates gravity(bricks falling on the 'ground')
int main(){
cin>>n>>m>>k;
n=4;m=5;k=3; //FOR TESTS-DEFAULT-remove this line
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
//cin>>wall[i][j]; //COMMENTED FOR TESTS-DEFAULT (decomment this afterwards)
}
for(int i=1;i<=k;i++) Attack_Wall(weapon[i]);
show_arr();
system("pause");
return 0;
}
void Attack_Wall(int weapon){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(wall[i][j]==weapon) wall[i][j]=0;
Simulate_Gravity();
}
void Simulate_Gravity(){
for(int i=n-1;i>0;i--){ //from DOWN to UP
if(wall[i+1][j]==0) { //i+1 e randul de dedesubt..si daca e gol(adica 0)
wall[i+1][j]=wall[i][j]; //aici ma mut cu caramida
wall[i][j]=0; //apoi fa-l pe acesta 0
}
}
void show_arr(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<wall[i][j]<<" ";
}
cout<<endl;
}
}
Am facut cateva functii pt. tine:Attack_Wall si Simulate_Gravity. Prima seteaza in array-ul wall toate caramizile ale caror cod corespunde si 'weapon' cu 0. Apoi cea de-a doua verifica caramizile de jos in sus in incearca 'sa le impinga de sus in jos'. Astfel ca daca sub o caramida (nesetata cu 0) exista un gol(cadica o caramida 0) atunci va fi mutata acolo. Asta e tot. Poti sa faci codul putin mai compact.
Aici e si codul (nu va fi formatat cum trebuie:
#include <iostream>
using namespace std;
int n,m,k;
//starts from 1
int wall[101][101]={{0, 0, 0, 0, 0, 0},
{0, 3, 5, 4, 5, 1},
{0, 2, 1, 1, 5, 3},
{0, 1, 1, 5, 5, 1},
{0, 5, 5, 1, 4, 3}}; //FOR TESTS-DEFAULT -delete {}
//starts from 1
int weapon_pos,weapon[101]={0,1,5,1}; //FOR TESTS-DEFAULT-delete {}
void show_arr(); //used for debugging-shows the content of 'wall' aray
void Attack_Wall(int weapon); //simulates the attack of the weapon-destroys bricks(marking them with 0)
void Simulate_Gravity(); //simulates gravity(bricks falling on the 'ground')
int main(){
cin>>n>>m>>k;
n=4;m=5;k=3; //FOR TESTS-DEFAULT-remove this line
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
//cin>>wall[i][j]; //COMMENTED FOR TESTS-DEFAULT (decomment this afterwards)
}
for(int i=1;i<=k;i++) Attack_Wall(weapon[i]);
show_arr();
system("pause");
return 0;
}
void Attack_Wall(int weapon){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(wall[i][j]==weapon) wall[i][j]=0;
Simulate_Gravity();
}
void Simulate_Gravity(){
for(int i=n-1;i>0;i--){ //from DOWN to UP
if(wall[i+1][j]==0) { //i+1 e randul de dedesubt..si daca e gol(adica 0)
wall[i+1][j]=wall[i][j]; //aici ma mut cu caramida
wall[i][j]=0; //apoi fa-l pe acesta 0
}
}
void show_arr(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<wall[i][j]<<" ";
}
cout<<endl;
}
}
Am facut cateva functii pt. tine:Attack_Wall si Simulate_Gravity. Prima seteaza in array-ul wall toate caramizile ale caror cod corespunde si 'weapon' cu 0. Apoi cea de-a doua verifica caramizile de jos in sus in incearca 'sa le impinga de sus in jos'. Astfel ca daca sub o caramida (nesetata cu 0) exista un gol(cadica o caramida 0) atunci va fi mutata acolo. Asta e tot. Poti sa faci codul putin mai compact.