Back to Problem Solutions forum
Hello, could someone help me? There is my code. It works good for exmample. I followed all step by step with example and didn't find any problem. But in real task I recive incorrect score.
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
class Cube{
private:
vector<vector<int>> cubeMatrix;
int score;
void Update(){ //There is offset all columns and rows
for(int i=0;i<cubeMatrix[0].size();i++){
for(int j=cubeMatrix.size()-1;j>=0;j--){
if(cubeMatrix[j][i]==0){
for(int k=j-1;k>=0;k--){
if(cubeMatrix[k][i]!=0){
swap(cubeMatrix[k][i],cubeMatrix[j][i]);
break;
}
}
}
}
}
for(int i=0;i<cubeMatrix[0].size();i++){
if(cubeMatrix[cubeMatrix.size()-1][i]==0){
for(int j=i+1;j<cubeMatrix[0].size();j++){
if(cubeMatrix[cubeMatrix.size()-1][j]!=0){
for(int k=0;k<cubeMatrix.size();k++){
swap(cubeMatrix[k][j],cubeMatrix[k][i]);
}
break;
}
}
}
}
}
int Score(int cubes){
return cubes*(cubes+1)/2;
}
void RemoveCubes(pair<int,int> coordinates){
queue <pair<int,int>> remove;
swap(coordinates.first,coordinates.second);
coordinates.first=cubeMatrix.size()-1-coordinates.first;
int numOfCube=cubeMatrix[coordinates.first][coordinates.second];
remove.push(coordinates);
int countCubes=0;
if(numOfCube!=0){ \\Wave alghoritm for find all nodes with founded num
while(!remove.empty()){
cubeMatrix[remove.front().first][remove.front().second]=0;
countCubes++;
if(remove.front().first-1>=0){
if(cubeMatrix[remove.front().first-1][remove.front().second]==numOfCube)
remove.push(make_pair(remove.front().first-1,remove.front().second));
}
if(remove.front().first+1<cubeMatrix.size()){
if(cubeMatrix[remove.front().first+1][remove.front().second]==numOfCube)
remove.push(make_pair(remove.front().first+1,remove.front().second));
}
if(remove.front().second-1>=0){
if(cubeMatrix[remove.front().first][remove.front().second-1]==numOfCube)
remove.push(make_pair(remove.front().first,remove.front().second-1));
}
if(remove.front().second+1<cubeMatrix[0].size()){
if(cubeMatrix[remove.front().first][remove.front().second+1]==numOfCube)
remove.push(make_pair(remove.front().first,remove.front().second+1));
}
remove.pop();
}
}
score+=Score(countCubes);
}
public:
Cube(vector<vector<int>> &cubeInit):cubeMatrix(cubeInit),score(0){}
void DoStep(pair<int,int> coordinates){
RemoveCubes(coordinates);
Update();
}
int GetScore(){
return score;
}
};
int main()
{
vector<vector<int>> initCube;
int rows;
cin>>rows;
cin.ignore(1000, '\n');
for(int i=0;i<rows;i++){
string temp;
getline(cin,temp);
vector <int> row;
for(int j=0;j<temp.size();j++){
row.push_back(temp[j]-'0');
}
initCube.push_back(row);
}
Cube game(initCube);
cin.ignore(1000, '\n');
string temp;
getline(cin,temp);
temp.erase(remove(temp.begin(),temp.end(),','));
istringstream in(temp);
pair<int,int> coordinates;
while(in>>coordinates.first>>coordinates.second){
game.DoStep(coordinates);
}
cout<<game.GetScore()<<endl;
return 0;
}
Problem was in wave alghoritm, could admin delete my code?