Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10640

Re: Iterate over the adjacent elements in a matix container

From "Paul" <pchrist<nospam>or@yahoo.co.uk>
Newsgroups comp.lang.c++
Subject Re: Iterate over the adjacent elements in a matix container
Date 2011-10-03 05:31 +0100
Organization Netfront http://www.netfront.net/
Message-ID <j6bds8$9gk$1@adenine.netfront.net> (permalink)
References <13318117.2085.1317066256036.JavaMail.geo-discussion-forums@yqmw31> <3755378.2025.1317318432217.JavaMail.geo-discussion-forums@yqbr29> <j68oip$r0u$1@adenine.netfront.net>

Show all headers | View raw


"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 ---

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Iterate over the adjacent elements in a matix container Alexander <alvatov@gmail.com> - 2011-09-26 12:44 -0700
  Re: Iterate over the adjacent elements in a matix container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-26 15:49 -0400
  Re: Iterate over the adjacent elements in a matix container puppi <fabricio.puppi@gmail.com> - 2011-09-26 15:30 -0700
  Re: Iterate over the adjacent elements in a matix container "Paul" <pchristor@yahoo.co.uk> - 2011-09-27 06:43 +0100
    Re: Iterate over the adjacent elements in a matix container Juha Nieminen <nospam@thanks.invalid> - 2011-09-27 07:12 +0000
      Re: Iterate over the adjacent elements in a matix container Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-27 11:10 -0400
        Re: Iterate over the adjacent elements in a matix container Juha Nieminen <nospam@thanks.invalid> - 2011-09-27 20:32 +0000
        Re: Iterate over the adjacent elements in a matix container Juha Nieminen <nospam@thanks.invalid> - 2011-09-30 06:24 +0000
          Re: Iterate over the adjacent elements in a matix container Nobody <nobody@nowhere.com> - 2011-09-30 20:55 +0100
          Re: Iterate over the adjacent elements in a matix container Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-30 17:07 -0400
  Re: Iterate over the adjacent elements in a matix container Krice <paulkp@mbnet.fi> - 2011-09-27 03:37 -0700
  Re: Iterate over the adjacent elements in a matix container Alexander <alvatov@gmail.com> - 2011-09-29 10:47 -0700
    Re: Iterate over the adjacent elements in a matix container "Paul" <pchristor@yahoo.co.uk> - 2011-09-30 01:28 +0100
    Re: Iterate over the adjacent elements in a matix container Juha Nieminen <nospam@thanks.invalid> - 2011-09-30 06:19 +0000
    Re: Iterate over the adjacent elements in a matix container "Paul" <pchristor@yahoo.co.uk> - 2011-10-02 05:16 +0100
      Re: Iterate over the adjacent elements in a matix container "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-10-03 05:31 +0100
        Re: Iterate over the adjacent elements in a matix container "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-10-03 05:47 +0100

csiph-web