Go: package main

import (
    "bufio"
    "os"
    "fmt"
)

var sc = bufio.NewScanner(os.Stdin)

/**************  Cell  *****************/
type Cell struct {
    item interface{}
    next *Cell
    prev *Cell
}

func NewCell(x interface{}, ne *Cell, pr *Cell) *Cell {
    newCp := new(Cell)
    newCp.item = x
    newCp.next = ne
    newCp.prev = pr
    return newCp
}
/************  End Cell  ***************/

/**************  Queue  ****************/
type Queue struct {
    size int
    front *Cell
    rear *Cell
}

func NewQueue() *Queue {
    return new(Queue)
}

func (q *Queue) Push(x interface{}) {
    newNode := NewCell(x, nil, nil)
    if q.size == 0 {
        q.front = newNode
        q.rear = newNode
    } else {
        newNode.prev = q.rear
        q.rear.next = newNode
        q.rear = newNode
    }
    q.size++
}

func (q *Queue) PopLeft() interface{} {
    if q.size == 0 {
        return nil
    } else {
        x := q.front.item
        if q.size == 1 {
            q.front = nil
            q.rear = nil
        } else {
            q.front = q.front.next
            q.front.prev = nil
        }
        q.size--
        return x
    }
}

func (q *Queue) IsEmpty() bool {
    return q.size == 0
}

func (q *Queue) Length() int {
    return q.size
}

type State struct {
    color string
    cnt int
}

func main() {
    for {
        sc.Scan()
        init := sc.Text()
        if init == "0" {
            break
        }
        bfs(init)
    }
}

func bfs(init string) {
    dic := make(map[string]int)
    que := NewQueue()
    que.Push(State{init, 0})
    length := len(init)
    isSolved := false
    for !que.IsEmpty() {
        state := que.PopLeft().(State)
        _, ok := dic[state.color]
        if ok {
            continue
        } else {
            dic[state.color] = state.cnt
        }

        if isSingleColor(state.color) {
            fmt.Println(state.cnt)
            isSolved = true
            break
        }

        for i := 0;i < length - 1; i++ {
            color1, color2 := string(state.color[i]), string(state.color[i + 1])
            if color1 != color2 {
                color3 := other(color1, color2)
                if i <= length - 2 {
                    que.Push(State{state.color[:i] + color3 + state.color[i + 2:], state.cnt + 1})
                } else {
                    que.Push(State{state.color[:i] + color3, state.cnt + 1})
                }
            }
        }
    }
    if !isSolved {
        fmt.Println("NA")
    }
}

func other(color1, color2 string) string {
    if color1 == "r" {
        if color2 == "g" {
            return "bb"
        } else if color2 == "b" {
            return "gg"
        }
    } else if color1 == "g" {
        if color2 == "r" {
            return "bb"
        } else if color2 == "b" {
            return "rr"
        }
    } else if color1 == "b" {
        if color2 == "r" {
            return "gg"
        } else if color2 == "g" {
            return "rr"
        }
    }
    return ""
}

func isSingleColor(color string) bool {
    for _, c := range color {
        if string(c) != string(color[0]) {
            return false
        }
    }
    return true
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  int N, M, dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};
  while(scanf("%d", &N) && N){
    int x[32], y[32];
    rep(i, N) scanf("%d%d", x+i, y+i);
    scanf("%d", &M);
    int nx = 10, ny = 10, dis, d;
    bool field[21][21] = {};
    field[ny][nx] = true;
    char com[4];
    rep(i, M){
      scanf("%s%d", com, &dis);
      switch(com[0]){
      case 'N': d = 0; break;
      case 'E': d = 1; break;
      case 'S': d = 2; break;
      case 'W': d = 3; break;
      }
      rep(j, dis){
	nx += dx[d]; ny += dy[d];
	field[ny][nx] = true;
      }
    }
    /*    rep(i, 21){
      rep(j, 21) printf("%d ", field[i][j]?1:0);
      puts("");
      }*/
    bool f = true;
    rep(i, N)
      if(!field[y[i]][x[i]]) f = false;
    if(f) puts("Yes");
    else puts("No");
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

string s;

int judge(string j){
  rep(i, j.size()-1) if(j[i] != j[i+1]) return 0;
  return 1;
}

int main(){
  while(cin >> s){
    if(s[0] == '0') break;

    queue<pair<string,int> >q;
    map<string, int>m;
    q.push(MP(s, 0));
    m[s] = 1;

    while(!q.empty()){
      pair<string, int> p = q.front(); q.pop();

      if(judge(p.F)){
	printf("%d\n", p.S);
	goto e;
      }

      rep(i, s.size()-1){
	string n = p.F;
	if(n[i] == n[i+1]) continue;
	if(n[i] == 'r' && n[i+1] == 'g') n[i] = n[i+1] = 'b';
	else if(n[i] == 'r' && n[i+1] == 'b') n[i] = n[i+1] = 'g';
	else if(n[i] == 'g' && n[i+1] == 'r') n[i] = n[i+1] = 'b';
	else if(n[i] == 'g' && n[i+1] == 'b') n[i] = n[i+1] = 'r';
	else if(n[i] == 'b' && n[i+1] == 'g') n[i] = n[i+1] = 'r';
	else if(n[i] == 'b' && n[i+1] == 'r') n[i] = n[i+1] = 'g';

	if(m.find(n) != m.end()) continue;
	q.push(MP(n, p.S+1));
	m[n] = 1;
      }
    }
    puts("NA");
  e:;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <set>
using namespace std;
typedef pair<string,int> P;
int main() {
	string str;
	while(cin>>str,str!="0") {
		queue<P> Q;
		Q.push(P(str,0));
		set<string> S;
		S.insert(str);
		bool NA=true;
		while(!Q.empty()) {
			P p=Q.front(); Q.pop();
			bool Flag=true;
			for(int i=0;i<p.first.size()-1;i++) {
				if(p.first[i]!=p.first[i+1]) {Flag=false;break;}
			}
			if(Flag) {printf("%d\n",p.second);NA=false;break;}
			for(int i=0;i<p.first.size()-1;i++) {
				string tmp=p.first;
				if((tmp[i]=='r' && tmp[i+1]=='g') || (tmp[i+1]=='r' && tmp[i]=='g')) {
					tmp[i]=tmp[i+1]='b';
				}else if((tmp[i]=='r' && tmp[i+1]=='b') || (tmp[i+1]=='r' && tmp[i]=='b')) {
					tmp[i]=tmp[i+1]='g';
				}else if((tmp[i]=='b' && tmp[i+1]=='g') || (tmp[i+1]=='b' && tmp[i]=='g')) {
					tmp[i]=tmp[i+1]='r';
				}else continue;
				if(S.find(tmp)==S.end()) {
					S.insert(tmp);
					Q.push(P(tmp,p.second+1));
				}
			}
		}
		if(NA) printf("NA\n");
	}
}