Re: Iterate over the adjacent elements in a matix container
"Paul" <pchristor@yahoo.co.uk> wrote in message
news:j68oip$r0u$1@adenine.netfront.net...
"Alexander" <alvatov@gmail.com> wrote in message
news:3755378.2025.1317318432217.JavaMail.geo-discussion-forums@yqbr29...
Krice, thank you very much! The name of the algorithm I need helps very
much i finding info.
Paul, sorry, but I'll need about a week to fully understand your idea.
I have been thinking about this algorithm and decided to write a little
example for you.
<snip>
And here's an example using recursion, its very similar. Again the
complexity seems to be in the getAdjacents function. It appears to be
simpler but there is more data stored on the stack as someone else has
already explained. I am still to understand this "breath first" method
Richard was talking about or the "seed method" Krice mentioned. I will
think more about this to try and find a better way.
/*Code. again very rough but just an example.*/
#include <iostream>
#include<vector>
std::vector<int> getAdjacents(int index, int rows=7, int cols=8){
std::vector<int> ret;
if( (index/cols==(index-1)/cols)&& index!=0 ) ret.push_back(index-1);
if((index+1)%cols) ret.push_back(index+1);
if(index-cols>=0) ret.push_back(index-cols);
if( (index+cols)<(rows*cols)) ret.push_back(index+cols);
return ret;
}
void recurse(std::vector<int>& v, int i, int initval, int newval){
std::vector<int> adjacents=getAdjacents(i);
for(int i=0; i<adjacents.size(); ++i){
if(v[adjacents[i]]==initval){
v[adjacents[i]]= newval;
recurse(v, adjacents[i], initval, newval);
}
}
}
void clickme(std::vector<int>& v, int clicked=55, int newval=4){
int initval= v[clicked];
v[clicked]=newval;
recurse(v, clicked, initval, newval);
}
int main(){
std::vector<int> v(56,2);
v[5]=3;
v[13]=3;
v[21]=3;
v[22]=3;
v[23]=3;
std::cout<<"Before clickme:";
for(int i=0; i<v.size(); ++i){
if(i%8==0) std::cout<<std::endl;
std::cout<< v[i];
}
clickme(v);
std::cout<<"\n\nAfter clickme:";
for(int i=0; i<v.size(); ++i){
if(i%8==0) std::cout<<std::endl;
std::cout<< v[i];
}
}
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---