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<iostream>
#include<queue>
using namespace std;
int h,w,n,ans=0;
char c[1002][1002];
pair<int,int> a[12],pi;
bool f[1002][1002];

int dist(pair<int,int> s,pair<int,int> g){
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)f[i][j]=false;
	//cout<<s.first<<","<<s.second<<" "<<g.first<<","<<g.second<<endl;
	queue<pair<pair<int,int>,int>> que;
	pair<pair<int,int>,int> p;
	p.first=s; p.second=0;
	que.push(p);
	while(que.size()>0){
		p=que.front(); que.pop();
		if(p.first==g)return p.second;
		f[p.first.first][p.first.second]=true;
		//cout<<p.first.first<<","<<p.first.second<<endl;
		//if(p.first.first==g.first&&p.first.second==g.second)return p.second;
		if(c[p.first.first-1][p.first.second]!='X'&&!f[p.first.first-1][p.first.second]){p.first.first--; p.second++; que.push(p); p.first.first++; p.second--;}
		if(c[p.first.first+1][p.first.second]!='X'&&!f[p.first.first+1][p.first.second]){p.first.first++; p.second++; que.push(p); p.first.first--; p.second--;}
		if(c[p.first.first][p.first.second-1]!='X'&&!f[p.first.first][p.first.second-1]){p.first.second--; p.second++; que.push(p); p.first.second++; p.second--;}
		if(c[p.first.first][p.first.second+1]!='X'&&!f[p.first.first][p.first.second+1]){p.first.second++; p.second++; que.push(p); p.first.second--; p.second--;}
	}
	return -1;
}
	

int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)c[i][j]='X';
	for(int i=1;i<=h;i++){
		scanf("\n");
		for(int j=1;j<=w;j++){
			scanf("%c",&c[i][j]);
			if('1'<=c[i][j]&&c[i][j]<='9'){
				a[c[i][j]-48].first=i;
				a[c[i][j]-48].second=j;
			}
			if(c[i][j]=='S'){
				a[0].first=i; a[0].second=j;
			}
		}
	}
		/*for(int i=1;i<=h;i++){	
			for(int j=1;j<=w;j++){
				printf("%c",c[i][j]);
			}
			printf("\n");
		}*/
	for(int i=1;i<=n;i++){
		//cout<<i<<endl;
		ans+=dist(a[i-1],a[i]);
	}
	printf("%d\n",ans);
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>

#define p(x) cout << "(" << x.first << "," << x.second << ")" << endl;

using namespace std;
const int MAX_H = 1005;
const int MAX_W = 1005;

char D[MAX_H][MAX_W];
bool visited[MAX_H][MAX_W];

int H, W, N;

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef vector<PII> VII;

int bfs(PII from, PII to) {
  queue<PIII> que;
  memset(visited, false, sizeof(visited[0][0]) * MAX_H * MAX_W);

  que.push(make_pair(from, 0));
  while (!que.empty()) {
    PIII p = que.front(); que.pop();
    int d = p.second;
    if (p.first == to) {
      return d;
    }
    for (int k = 0; k < 4; k++) {
      int i2 = p.first.first + dx[k];
      int j2 = p.first.second + dy[k];
      if (0 <= i2 && i2 < H && 0 <= j2 && j2 < W && D[i2][j2] != 'X' && !visited[i2][j2]) {
        visited[i2][j2] = true;
        que.push(make_pair(make_pair(i2, j2), d+1));
      }
    }
  }
  return -1;
}

int main() {
  cin >> H >> W >> N;

  VII pos(N+1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      D[i][j] = c;
      if (c == 'S') {
        pos[0] = make_pair(i, j);
      } else if (c > '0') {
        pos[c - '0'] = make_pair(i, j);
      }
    }
  }

  int res = 0;
  for (int i = 0; i < N; i++) {
    res += bfs(pos[i], pos[i+1]);
  }
  cout << res << endl;

  return 0;
}

C++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)

using namespace std;

typedef long long ll;
typedef pair<int,int> P;



int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int bfs(P x,P y,vector<vector<int> > map){
    vector<vector<bool> > d(map.size(),vector<bool>(map[0].size()));
    queue<P> que;
    que.push(x);
    d[x.first][x.second]==true;
    while(que.size()){
        P p=que.front();que.pop();
        //cout<<"lll"<<p.first<<p.second<<endl;
        if(p.first == y.first and p.second==y.second) {break;}
        for(int i=0;i<4;i++){
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
            if(0<=nx && nx<map.size() && 0<=ny &&ny<map[0].size() && map[nx][ny]!=-1 && d[nx][ny]==false){
                d[nx][ny]=true;
                que.push(P(nx,ny));
                map[nx][ny]=map[p.first][p.second]+1;
                //cout<<nx<<ny<<map[nx][ny]<<endl;
                
            }
        }
    }
    return map[y.first][y.second];

}



int main(){
    int x,y,z;
    cin>>x>>y>>z;
    vector<vector<int> > map;
    vector<pair<int,int> > pl(z+1);

    int sx,sy;
    rp(i,0,x){
        vector<int> tmp;
        string s;
        cin>>s;
        rp(j,0,y){
            if(s[j]=='X') tmp.push_back(-1);
            else if(s[j]=='.') tmp.push_back(0);
            else if(s[j]=='S') sx=i,sy=j,tmp.push_back(0);
            else {pl[(int)(s[j] - '0')]=make_pair(i,j);tmp.push_back(0);}
        }
        map.push_back(tmp);
    }

    int ans=0;
    //cout<<sx<<sy<<pl[1].first<<pl[1].second<<endl;
    ans+=bfs(P(sx,sy),pl[1],map);
    for(int i=2;i<=z;i++){
        ans+=bfs(pl[i-1],pl[i],map);
    }
    cout<<ans<<endl;
    return 0;
};