Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<vector>
- #include<iostream>
- #include<utility>
- #include<math.h>
- #include<set>
- #include<memory>
- #include<chrono>
- #include <ctime>
- using namespace std;
- using MATRIX = vector<vector<int>>;
- using P = pair<size_t,size_t>;
- using COORDINATES = set<pair<int,P>>;
- //set vagy multiset.
- using SEARCHRES = set<pair<P,P>>;
- enum class LOGIC
- {
- EQ=0,
- BIGGER = 1,
- SLOWER =2
- };
- struct MAP
- {
- MATRIX mat;
- COORDINATES coords;
- COORDINATES negativecoords;
- SEARCHRES c;
- size_t N;
- size_t M;
- MAP(size_t n,size_t m)
- {
- N=n;
- M=m;
- mat = vector<vector<int>>(N);
- for (size_t i = 0 ; i < n ; ++i )
- {
- mat[i].resize(M);
- }
- }
- bool isValidCoord(size_t p_i,size_t p_j)
- {
- return (p_i >= 0 && p_i < N && p_j >= 0 && p_j < M);
- }
- void print()
- {
- cout<<endl;
- for(auto vec : mat)
- {
- for(auto elem : vec)
- {
- cout<<elem<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- }
- void insert(size_t i, size_t j, int value)
- {
- if(i >= N || i < 0 || j >= M || j < 0)
- {
- cout<<"insert failed with ("<<i<<" "<<j<<") coordinates"<<endl;
- }
- else
- {
- mat[i][j]=value;
- if(value>0)
- {
- coords.insert(pair<int,P>(value,P(i,j)));
- }
- else if(value!=0)
- {
- negativecoords.insert(pair<int,P>(value,P(i,j)));
- }
- }
- }
- size_t getDistance(size_t x,size_t y, size_t o_x,size_t o_y)
- {
- int fst = o_x-x;
- int snd = o_y-y;
- return abs(fst)+abs(snd);
- }
- int calculate()
- {
- int sum=0;
- for(size_t i=0; i<N; ++i)
- {
- for(size_t j=0; j<M; ++j)
- {
- int elem=mat[i][j];
- if(elem>0)
- {
- //itt kell okosítani mehet sorba de a legoptimálisabbat találja meg majd a push negative.
- while(mat[i][j])
- {
- sum+=pushToNegative(i,j,mat[i][j]);
- }
- }
- }
- }
- return sum;
- }
- int pushToNegative(size_t p_i,size_t p_j,int value)
- {
- size_t steps=0;
- for(size_t i=0; i<N; ++i)
- {
- for(size_t j=0; j<M; ++j)
- {
- if(p_i==i &&p_j==j)
- {
- continue;
- }
- int elem=mat[i][j];
- int absol=abs(mat[i][j]);
- if(elem<0)
- {
- if(absol==value)
- {
- steps=absol*getDistance(i,j,p_i,p_j);
- mat[i][j]=0;
- mat[p_i][p_j]=0;
- }
- else if(absol>value)
- {
- mat[p_i][p_j]=0;
- mat[i][j]=elem+value;
- steps=value*getDistance(i,j,p_i,p_j);
- }
- else
- {
- mat[p_i][p_j]=value+elem;
- mat[i][j]=0;
- steps=absol*getDistance(i,j,p_i,p_j);
- }
- return steps;
- }
- }
- }
- }
- //search
- void search(size_t& p_i,size_t& p_j,int elem)
- {
- //?bool
- search1(p_i,p_j,elem);
- if(c.size()!=0)
- {
- return;
- }
- return;
- }
- void search1(size_t& p_i,size_t& p_j,int elem)
- {
- //jobbra
- if(isValidCoord(p_i+1,p_j) && mat[p_i+1][p_j]<0)
- {
- int value = mat[p_i+1][p_j];
- int absol = abs(value);
- if(absol>=elem)
- {
- c.insert(pair<P,P>(P(1,elem),P(p_i+1,p_j)));
- }
- }
- ////balra
- if(isValidCoord(p_i-1,p_j) && mat[p_i-1][p_j]<0)
- {
- int value = mat[p_i-1][p_j];
- int absol = abs(value);
- if(absol>=elem)
- {
- c.insert(pair<P,P>(P(1,elem),P(p_i-1,p_j)));
- }
- }
- //fel
- if(isValidCoord(p_i,p_j-1) && mat[p_i][p_j-1]<0)
- {
- int value = mat[p_i][p_j-1];
- int absol = abs(value);
- if(absol>=elem)
- {
- c.insert(pair<P,P>(P(1,elem),P(p_i,p_j-1)));
- }
- }
- //le
- if(isValidCoord(p_i,p_j+1) && mat[p_i][p_j+1]<0)
- {
- int value = mat[p_i][p_j+1];
- int absol = abs(value);
- if(absol>=elem)
- {
- c.insert(pair<P,P>(P(1,elem),P(p_i,p_j+1)));
- }
- }
- }
- int searchMinimums()
- {
- int sum=0;
- for(size_t i=0; i<N; ++i)
- {
- for(size_t j=0; j<M; ++j)
- {
- int elem=mat[i][j];
- if(elem>0)
- {
- search(i,j,elem);
- }
- }
- }
- return sum;
- }
- };
- int main()
- {
- auto start = std::chrono::system_clock::now();
- int n,m;
- cin>>n;
- cin>>m;
- MAP ma(n,m);
- for(int i=0;i<n;++i)
- {
- for(int j=0;j<m;++j)
- {
- int value;
- cin>>value;
- ma.insert(i,j,value);
- }
- }
- cout<<endl;
- for(auto p : ma.c)
- {
- cout<<p.first.first<<endl;
- }
- ma.searchMinimums();
- cout<<endl;
- ma.print();
- //cout<<ma.calculate();
- /*cout<<endl;
- for(auto p : ma.coords)
- {
- cout<<p.first<<endl;
- }
- cout<<endl;
- for(auto p : ma.negativecoords)
- {
- cout<<p.first<<endl;
- }
- */
- cout<<endl;
- for(auto p : ma.c)
- {
- cout<<p.first.first<<" "<< p.first.second<<" " << p.second.first<<" "<<p.second.second<<endl;
- }
- auto end = std::chrono::system_clock::now();
- std::chrono::duration<double> elapsed_seconds = end-start;
- std::time_t end_time = std::chrono::system_clock::to_time_t(end);
- std::cout << "elapsed time: " << elapsed_seconds.count() <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement