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*****/
#include <iostream>
#include <fstream>
#include <queue>

/*****マクロ定義*****/
#define START 0
#define EMPTY 10

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/
char Field[1001][1001];
bool Visited[1001][1001];
int TimeF[1001][1001];

/*****その他関数置き場*****/
int bfs(int sx,int sy,int gx,int gy){
	queue<int> Qx;
	queue<int> Qy;
	int x_now;
	int y_now;
	int x_next;
	int y_next;
	int dx[] = {0,1,0,-1};
	int dy[] = {-1,0,1,0};
	Qx.push(sx);
	Qy.push(sy);
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}

	Visited[sy][sx] = true;
	TimeF[sy][sx] = 0;
	while(!(Qx.empty() && Qy.empty())){
		x_now = Qx.front();Qx.pop();
		y_now = Qy.front();Qy.pop();
		//cout << TimeF[y_now][x_now] << endl;
		if(x_now == gx && y_now == gy)	break;
		for(int i=0;i<4;i++){
			x_next = x_now + dx[i];
			y_next = y_now + dy[i];
			if(Field[y_next][x_next] == 'X' || Visited[y_next][x_next]);
			else{
				Qx.push(x_next);
				Qy.push(y_next);
				Visited[y_next][x_next] = true;
				TimeF[y_next][x_next] = TimeF[y_now][x_now] + 1;
			}
		}
	}
	return TimeF[gy][gx];
}
/*****main関数*****/
int main(){
	/*****変数置き場*****/
	int total_time = 0;
	char row[1001];
	int height;
	int width;
	int n;
	int x[] = {0,0,0,0,0,0,0,0,0,0};
	int y[] = {0,0,0,0,0,0,0,0,0,0};
	/*****ファイルオープン*****/	
	/*ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return;
	}*/
	
	/*****処理部*****/
	cin >> height >> width >> n;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Field[i][j] = 'X';
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}
	for(int i=0;i<height;i++){
		cin >> row;
		for(int j=0;j<width;j++){
			Field[i+1][j+1] = row[j];
		}
	}
	/*for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			cout << Field[i][j];
		}
		cout << endl;
	}*/
	for(int i=0;i<height+1;i++){
		for(int j=0;j<width+1;j++){
			switch (Field[i][j]){
			case 'S':
				x[START] = j;
				y[START] = i;
				break;
			case '1':
				x[1] = j;
				y[1] = i;
				break;
			case '2':
				x[2] = j;
				y[2] = i;
				break;
			case '3':
				x[3] = j;
				y[3] = i;
				break;
			case '4':
				x[4] = j;
				y[4] = i;
				break;
			case '5':
				x[5] = j;
				y[5] = i;
				break;
			case '6':
				x[6] = j;
				y[6] = i;
				break;
			case '7':
				x[7] = j;
				y[7] = i;
				break;
			case '8':
				x[8] = j;
				y[8] = i;
				break;
			case '9':
				x[9] = j;
				y[9] = i;
				break;
			}
		}
	}
	/*for(int i=0;i<10;i++){
		cout << x[i] << " ";
	}
	cout << endl;
	for(int i=0;i<10;i++){
		cout << y[i] << " ";
	}
	cout << endl;*/
	for(int i=0;i<n;i++){
		total_time += bfs(x[i],y[i],x[i+1],y[i+1]);
	}
	/*for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			cout << Visited[i][j];
		}
		cout << endl;
	}*/
	cout << total_time << endl;
	/*****処理終了後*****/
	/*fout.close();
	fin.close();*/
	//delete[] pField;
	//delete[] pVisited;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define INF 1e+8

struct P{
    int x;
    int y;
};

int w,h,n;
char feld[1001][1001];
int d[1001][1001];
int sx,sy,gx,gy;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans=0;
P che[15];


int bfs(){
    REP(i,n){
        P sp=che[i];
        sx=che[i].x;
        sy=che[i].y;
        gx=che[i+1].x;
        gy=che[i+1].y;
        queue<P> que;
        REP(y,h)REP(x,w)d[y][x]=INF;
        que.push(sp);
        d[sy][sx]=0;
        while(!que.empty()){
            P p=que.front();
            que.pop();
            int x=p.x;
            int y=p.y;
            if(x==gx&&y==gy){
                ans+=d[gy][gx];
                break;
            }
            REP(i,4){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||ny<0&&nx>=w&&ny>=h)continue;
                if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                    P next;
                    next.x=nx;
                    next.y=ny;
                    que.push(next);
                    d[ny][nx]=d[y][x]+1;
                }
            }
        }
    }
    return 0;
}

int main(){
    cin>>h>>w>>n;
    REP(y,h){
        REP(x,w){
            scanf(" %c",&feld[y][x]);
            if(feld[y][x]=='S'){
                che[0].x=x;
                che[0].y=y;
            }else if(feld[y][x]>='1'&&feld[y][x]<='9'){
                char c=feld[y][x];
                che[c-'0'].x=x;
                che[c-'0'].y=y;
            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PA;
int main(){

  int h,w,n,a[11][2];
  char c[1001][1001];
  cin >> h >> w >> n;

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> c[i][j];
      if(c[i][j]>='1'&&c[i][j]<='9'){
	a[ c[i][j]-'0'][0] = j;
	a[ c[i][j]-'0'][1] = i;
      }
      if(c[i][j]=='S'){
	a[0][0] = j;
	a[0][1] = i;
      }
    }
  }

  int sum = 0,suji[1001][1001],flag=0;
  queue<PA> que;
  int dx[12] = {1,-1,0,0};
  int dy[12] = {0,0,1,-1};

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

    memset(suji,-1,sizeof(suji));
    que.push( PA(a[i][0],a[i][1]) );
    suji[ a[i][1] ][ a[i][0] ] = 0;
    while(!que.empty()){
      PA p = que.front();
      que.pop();
      
      for(int j=0;j<4;j++){
	int x = p.first + dx[j];
	int y = p.second + dy[j];

	if(x<0||y<0||x>=w||y>=h||suji[y][x]!=-1||c[y][x]=='X') continue;
	suji[y][x] = suji[p.second][p.first]+1;
	que.push( PA(x,y) );
      }
    }
    
    sum += suji[a[i+1][1]][a[i+1][0]];

  }

  cout << sum << endl;
  
  return (0);
}