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 "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge { LL to, cost; };

LL H, W, N;
std::string field[112300];
LL Sx, Sy, xy[1100];
LL ans;

LL d[112345600];
std::vector<edge>G[112345600];
void dijkstra(LL s)
{
	std::priority_queue<P, std::vector<P>, std::greater<P>>que;
	std::fill(d, d + H * W + 1, INF);
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty())
	{
		P p = que.top(); que.pop();
		LL v = p.second;
		if (d[v] < p.first) { continue; }
		rep(i, G[v].size())
		{
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main()
{
	in >> H >> W >> N;
	rep(i, H) { in >> field[i]; }

	rep(x, H)
	{
		rep(y, W)
		{
			if (field[x][y] == 'X') { continue; }
			if (field[x][y] == 'S') { Sx = x; Sy = y; }
			if (field[x][y] != '.') { xy[field[x][y] - '0'] = x*W + y; }
			if (x > 0)
			{
				if (field[x - 1][y] != 'X')
				{
					G[x*W + y].push_back({ (x - 1)*W + y,1 });
					G[(x - 1)*W + y].push_back({ x*W + y,1 });
				}
			}
			if (y > 0)
			{
				if (field[x][y - 1] != 'X')
				{
					G[x*W + y].push_back({ x*W + (y - 1),1 });
					G[x*W + (y - 1)].push_back({ x*W + y,1 });
				}
			}
			if (x < H - 1)
			{
				if (field[x + 1][y] != 'X')
				{
					G[x*W + y].push_back({ (x + 1)*W + y,1 });
					G[(x + 1)*W + y].push_back({ x*W + y,1 });
				}
			}
			if (y < W - 1)
			{
				if (field[x][y + 1] != 'X')
				{
					G[x*W + y].push_back({ x*W + (y + 1),1 });
					G[x*W + (y + 1)].push_back({ x*W + y,1 });
				}
			}
		}
	}
	dijkstra(Sx*W + Sy);
	rep(i, N)
	{
		ans += d[xy[i + 1]];
		dijkstra(xy[i + 1]);
	}

	out << ans << std::endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

typedef struct{
 int x;
 int y;
}P;//??¨?????????

char graph[1000][1000];
P aim[10];
P s,g;//s?????????g??????
int N,W,H;
int vis[1000][1000]; //??¨??\?????¨??????
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int bfs(P s,P g);

int main(){
    int i,j,t,step=0;
    cin>>H>>W>>N;
    
    for(i=0;i<H;i++)
    for(j=0;j<W;j++){
        cin>>graph[i][j];
      if(graph[i][j]<='9'&&graph[i][j]>='1'){ 
         t=graph[i][j]-'0';
         aim[t].x=i;
         aim[t].y=j;
      } //???1-N?????????????????¨ aim??°?????? 
      else if(graph[i][j]=='S'){
          s.x=i;
          s.y=j;
          graph[i][j]='.';
      }//?????°??????????????? 
    }
    
    for(i=1;i<=N;i++){
       g.x=aim[i].x;
       g.y=aim[i].y;//1-N ???????°???°??§???????¬?????????? ???????????? 
       step+=bfs(s,g);
       s.x=g.x;
       s.y=g.y;//??°???????????????????????????????????? 
       graph[g.x][g.y]='.';
    }
   
    cout<<step<<endl;
}

int bfs(P s,P g){
    queue<P> que;
      P n;
    
    for(int i=0;i<H;i++)
    for(int j=0;j<W;j++)
     vis[i][j]=-1;
     
     que.push(s);
     vis[s.x][s.y]=0;
     
     while(que.size()){
         
       P p=que.front();
         que.pop();
       if(p.x==g.x&&p.y==g.y)
       break;
       
       for(int i=0;i<4;i++){
           n.x=p.x+dx[i];
           n.y=p.y+dy[i];//?????????????°? 
        if(0<=n.x&&n.x<H&&0<=n.y&&n.y<W&&graph[n.x][n.y]!='X'&&vis[n.x][n.y]==-1){
               que.push(n);
               vis[n.x][n.y]=vis[p.x][p.y]+1;
           }
       }
   }
     return vis[g.x][g.y];
}

C++: #include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rb_tree>
*/
#define mem(x,y) memset(x,y,sizeof(x))
//#define P(x) cout<<#x<<" = "<<x<<endl;
#define PN(x,n) cout<<'{'<<x[0];for(int i=1;i<n;i++)cout<<','<<x[i];cout<<'}'<<endl;
#define RP(i,n) for(int i=0;i<n;i++)
#define RP2(i,a,b) for(int i=a;i<=b;i++)
#define mp make_pair
#define P pair<int,int>
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define dinf 1e99
#define OJ
#define ite iterator
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
using namespace std;/*
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> Tree;
const double PI = acos(-1.0);  
const double eps = 1e-5;
const double T = 5; //t of fire
const double delta = 0.99; //decreasing speed of t
const int INF = 1000000000;*/
const int maxn=1005;
bool vis[maxn][maxn];
int d[maxn][maxn];
int ans,n,m,k,sx,sy;
int lx[maxn],ly[maxn],G[maxn][maxn];
char bf;
queue<P> q;
int bfs(int sx,int sy,int ex,int ey){
    mem(vis,0);
    q.push(mp(sx,sy));
    vis[sx][sy] = 1;
    d[sx][sy] = 0;
    while(!q.empty()){
	P u = q.front(); q.pop();
	for(int dr=0;dr<4;dr++){
	    P v=mp(u.fi+dx[dr],u.se+dy[dr]);
	    if(v.fi<1 || v.se<1 || v.fi>n || v.se>m || vis[v.fi][v.se] || G[v.fi][v.se]) continue;
	    d[v.fi][v.se]=d[u.fi][u.se] + 1;
	    vis[v.fi][v.se] = 1;
	    q.push(v);
	}
    }
    return d[ex][ey];
}
int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++){
	    cin>>bf;
	    if(bf=='.') G[i][j] = 0;
	    else if(bf == 'S') sx = i, sy = j, G[i][j] = 0;
	    else if(bf=='X') G[i][j] = 1;
	    else lx[bf-48] = i, ly[bf-48]=j;
	}
    }
    for(int i=1;i<=k;i++){
	ans += bfs(sx,sy,lx[i],ly[i]);
	sx=lx[i], sy=ly[i];
    }
    cout<<ans<<endl;
    return 0;
}