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>
#define min(x,y) ((x)<(y)?(x):(y))
#define INFI 100000000

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1,0,-1,0};
int ver[] = {0,-1,0,1};
int ans;


void clear();
void show();
P bfs(int cheese,P n);

int main(void) {
    cin>>H>>W>>N;
    P s,e;
    for (int i = 0; i<H; i++)
        cin>>Maps[i];
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            if (Maps[i][j]=='S') s.first = i, s.second = j;
    
    
    for (int i = 1; i<=N; i++) {
        clear();
        s = bfs(i, s);
    }
    
    cout<<ans<<endl;
    return 0;
}

P bfs(int cheese,P n) {
    queue<P> que;
    que.push(n);
    dist[n.first][n.second] = 0;
    
    P top;
    while (que.size()) {
        top = que.front();
        if (Maps[top.first][top.second]-'0'==cheese) {
            ans += dist[top.first][top.second];
            break;
        }
        
        que.pop();
        for (int i = 0; i<4; i++) {
            int nv = top.first+ver[i], nh = top.second+hor[i];
            if (nv>=0 && nv<H && nh>=0 && nh<W && dist[nv][nh]==INFI && Maps[nv][nh]!='X') {
                que.push(P(nv,nh));
                dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second]+1);
            }
        }
    }
    return top;
}

void clear() {
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            dist[i][j] = INFI;
}

C++: #include<bits/stdc++.h>
using namespace std;
char str[1001][1001];
int w, h, sx, sy, gx, gy;
int solve() {
    int x[] = { -1,0,1,0 }, y[] = { 0,-1,0,1 };
    vector<vector<int>>weight(w + 1, vector<int>(h + 1, 0));
    queue<pair<int, int>>que;
    que.push(make_pair(sx, sy));
    while (!que.empty()) {
	int curx = que.front().first, cury = que.front().second;
	que.pop();
	for (int i = 0; i < 4; ++i) {
	    if (curx + x[i] != 0 && curx + x[i] != w + 1 && cury + y[i] != 0 && cury + y[i] != h + 1) {
		if (curx + x[i] == gx && cury + y[i] == gy) return weight[curx][cury] + 1;
		if (str[cury + y[i]][curx + x[i]] == '.'&&weight[curx + x[i]][cury + y[i]] == 0) {
		    que.push(make_pair(curx + x[i], cury + y[i]));
		    weight[curx + x[i]][cury + y[i]] = weight[curx][cury] + 1;
		}
	    }
	}
    }
}
int main() {
    int sum=0,facx[10],facy[10],n;
    cin >> h >> w>>n;
    for (int i = 1; i <= h; ++i) {
	for (int j = 1; j <= w; ++j) {
	    cin >> str[i][j];
	    if (str[i][j] == 'S') {
		sx = j; sy = i;
		str[i][j]='.';
	    }
	    if (str[i][j] >= '1'&&str[i][j]<='9') {
		facx[str[i][j]-'0'] = j; facy[str[i][j]-'0'] = i;
		str[i][j]='.';
            }
	}
    }
    for(int i=1;i<=n;++i){
	gx=facx[i]; gy=facy[i];
	sum+=solve();
	sx=gx; sy=gy;
    }
    cout<<sum<<endl;
    return 0;
}