Go: ///
// File:  0558.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:58:45 2018
//
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type pos struct {
	x, y int
}

var H, W, N int
var graph [][]string

func wfs(p pos, index int) (int, pos) {
	var q []pos
	dists := make([][]int, H)
	for i := range dists {
		dists[i] = make([]int, W)
	}

	q = append(q, p)
	for len(q) > 0 {
		p := q[0]
		dist := dists[p.x][p.y]
		q = q[1:]

		for _, d := range []pos{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
			x := p.x + d.x
			y := p.y + d.y
			if 0 <= x && x < H && 0 <= y && y < W && graph[x][y] != "X" && dists[x][y] == 0 {
				dists[x][y] = dist + 1
				q = append(q, pos{x, y})
			}
		}
	}

	for i := range graph {
		for j := range graph[i] {
			if graph[i][j] == strconv.Itoa(index) {
				return dists[i][j], pos{i, j}
			}
		}
	}

	panic("not found")
}

func main() {
	fmt.Scan(&H, &W, &N)

	var start pos

	graph = make([][]string, H)
	for i := range graph {
		var s string
		fmt.Scan(&s)
		graph[i] = strings.Split(s, "")
		for j := range graph[i] {
			if graph[i][j] == "S" {
				start.x = i
				start.y = j
			}
		}
	}

	count := 0
	for i := 1; i <= N; i++ {
		c, s := wfs(start, i)
		start = s
		count += c
	}

	fmt.Println(count)
}

C++: #include <cstdio>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int INF = 100000000;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W]; // ?????????????????\??´?????§??????????????°, 1~N??§?????????????????§????????°??????+1
int sx, sy, gx, gy;
int power = 1; // ??????????????????
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

typedef pair<int, int> P;

void reset_d() {
  for (int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) d[i][j] = INF;
}

int bfs(int c) {
  queue<P> que;
  reset_d();
  d[sx][sy] = c;
  que.push(P(sx, sy));
  while(que.size()) {
    P p = que.front(); que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if ( 0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' && d[nx][ny] == INF) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
        if (field[nx][ny] - '0' == power) {
          sx = nx;
          sy = ny;
          power++;
          return d[nx][ny];
        }
      }
    }
  }
  return INF;
}

void solve() {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c = bfs(c);
  }
  printf("%d\n", c);
}


int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", field[i]);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (field[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }
  solve();
  return 0;
}

C++: #include <iostream>

#include<queue>

#include<string.h>

using namespace std;

int main(){

    queue <int> q;

    int h,w,n;

    long ans=0;

    char map[1000][1000];

    int d[1000][1000];

    int x,fx,mx[]={0,1,0,-1};

    int y,fy,my[]={1,0,-1,0};

    cin>>h>>w>>n;

    for(int i=0;i<h;i++){

        for(int o=0;o<w;o++){

            cin>>map[i][o];

            if(map[i][o]=='S'){

                q.push(i);

                q.push(o);

                x=o;

                y=i;

            }

        }

    }
    
    for(int i=1;i<=n;i++){
        memset(d,-1,sizeof(d));

        d[y][x]=0;

        while(!q.empty()){

            y=q.front();

            q.pop();

            x=q.front();

            q.pop();

            if(map[y][x]==i+48){

                ans=ans+d[y][x];

                while(!q.empty()){

                    q.pop();

                }

                q.push(y);

                q.push(x);

                break;

            }

            for(int o=0;o<4;o++){

                fy=y+my[o];

                fx=x+mx[o];

                if(fy>=0&&fy<h&&fx>=0&&fx<w&&map[fy][fx]!='X'&&d[fy][fx]==-1){

                    d[fy][fx]=d[y][x]+1;

                    q.push(fy);

                    q.push(fx);

                }

            }

        }

    }

    cout<<ans<<endl;

}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int h,w;
    char n;
    cin >> h >> w >> n;
    char map[h][w];
    long long int did[h][w]={};
    long long int va[h][w]={};
    queue <pair <int ,int > > q;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> map[i][j];
            if(map[i][j]=='S'){
                did[i][j]=1;
                q.push(make_pair(i,j));
            }
            if(map[i][j]=='X')
                did[i][j]=1;
        }
    }
    char N='1';
    int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};
    long long count=0;
    while(q.size()){
        //cout << "------------------------------------------------------" << endl;
        /*for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cout << did[i][j];
            }
            cout << endl;
        }
        cout << "q.front().first=" << q.front().first << " q.front().second=" << q.front().second << endl;
        cout << map[q.front().first][q.front().second] << endl;*/
        long long MIN=10000000000000000;
        for(int i=0;i<4;i++){
            if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
            else if(va[q.front().first+dy[i]][q.front().second+dx[i]]!=0){
                va[q.front().first][q.front().second]= min(MIN,va[q.front().first+dy[i]][q.front().second+dx[i]]);
                MIN = va[q.front().first][q.front().second];
            }
        }
        va[q.front().first][q.front().second]++;
        if(map[q.front().first][q.front().second]==N){
            //cout << "******************************" << endl;
            if(N==n){
                count+=va[q.front().first][q.front().second];
                //cout << va[q.front().first][q.front().second] << endl;
                cout << count-1 << endl;
                break;
            }
            else{
                count+=va[q.front().first][q.front().second];
                //cout << va[q.front().first][q.front().second] << endl;
                //cout <<"count=" <<count << endl;
                N++;
                q.push(make_pair(q.front().first,q.front().second));
                q.push(make_pair(q.front().first,q.front().second));
                while(q.size()-1)
                    q.pop();
                for(int i=0;i<h;i++){
                    for(int j=0;j<w;j++){
                        va[i][j]=0;
                        did[i][j]=0;
                        if(map[i][j]=='X')
                            did[i][j]=1;
                    }
                }
                //cout << q.front().first << " " << q.front().second << endl;
                did[q.front().first][q.front().second]=1;
            }
        }

        for(int i=0;i<4;i++){
            if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
            else if(did[q.front().first + dy[i]][q.front().second + dx[i]]!=1){
                q.push(make_pair(q.front().first+dy[i],q.front().second+dx[i]));
                did[q.front().first + dy[i]][q.front().second + dx[i]]=1;
            }
        }
        q.pop();
    }
}