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<string>
#include<algorithm>    
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<queue>
#include <tuple>
#include <cassert>
#define int long long
using namespace std;
char maze[2134][2134];
pair <int, int> iti[2134];
signed main() {
	int a, w, b, ans = 0; cin >> a >> w >> b;
	const int INF = 1000000000; typedef pair<int, int>P;
	for (int h = 1; h <= a; h++) {
		for (int i = 1; i <= w; i++) {
			cin >> maze[h][i];
			if (maze[h][i] == 'S') { iti[0].first = h; iti[0].second = i; }
			if (maze[h][i] == '1') { iti[1].first = h; iti[1].second = i; }
			if (maze[h][i] == '2') { iti[2].first = h; iti[2].second = i; }
			if (maze[h][i] == '3') { iti[3].first = h; iti[3].second = i; }
			if (maze[h][i] == '4') { iti[4].first = h; iti[4].second = i; }
			if (maze[h][i] == '5') { iti[5].first = h; iti[5].second = i; }
			if (maze[h][i] == '6') { iti[6].first = h; iti[6].second = i; }
			if (maze[h][i] == '7') { iti[7].first = h; iti[7].second = i; }
			if (maze[h][i] == '8') { iti[8].first = h; iti[8].second = i; }
			if (maze[h][i] == '9') { iti[9].first = h; iti[9].second = i; }
		}
	}
	for (int h = 0; h < b; h++) {
		int sx = iti[h].first, sy = iti[h].second, gx = iti[h + 1].first, gy = iti[h + 1].second;
		int d[1234][1234], dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
		queue<P> que;
		for (int i = 1; i <= a; i++) {
			for (int j = 1; j <= w; j++) {
				d[i][j] = INF;
			}
		}
		que.push(P(sx, sy));
		d[sx][sy] = 0;
		while (que.size()) {
			P p = que.front(); que.pop();
			if (p.first == gx && p.second == gy) {
				break;
			}
			for (int i = 0; i < 4; i++) {
				int nx = p.first + dx[i], ny = p.second + dy[i];
				if (0 < nx && nx <= a && 0 < ny && ny <= w && maze[nx][ny] != 'X' && d[nx][ny] == INF) {
					que.push(P(nx, ny));
					d[nx][ny] = d[p.first][p.second] + 1;
				}
			}
		}
		ans += d[gx][gy];
	}
	cout << ans << endl;
}

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[1123];
LL Sx, Sy, xy[11];
LL ans;
  
LL d[1123456];
std::vector<edge>G[1123456];
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()
{try{
    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 < 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;
}catch(std::bad_alloc){return 0;}catch(...){return 0;}}

C++: #include<iostream>
#include<cstring>
#include<queue>
using namespace std ;
struct ST
{
	int x ;
	int y ;
	int cnt = 0 ;
} ;
bool came[ 1000 ][ 1000 ] = {{ 0 }} ;
char str[ 1000 ] ;
int main()
{
	ST m ,s ;
	queue< ST > que ;
	int n ;
	int w ,h ;
	int kyori = 0 ;
	int min = 1000 ;
	int map[ 1000 ][ 1000 ] = {{ 0 }} ;
	int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
	int my[ 4 ] = { 1 ,-1 ,0 ,0 } ;
	cin >> h >> w >> n ;
	int cheesuu = n ;
	int cheese ;
	int fx ,fy ;
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> str ;
		for( int j = 0 ; j < w ; j++ )
		{
			if( str[ j ] == 'X' )
				map[ i ][ j ] = -1 ;
			else if( str[ j ] == 'S' )
			{
				m.x = j ;
				m.y = i ;
			}
			else if( str[ j ] != '.' )
			{
				map[ i ][ j ] = str[ j ] - 48 ;
			}
		}
	}
	bool a = true ;
	que.push( m ) ;
	while( n > 0 )
	{
		n-- ;
		cheese = cheesuu - n ;
		while( !que.empty() )
		{
			m = que.front() ;
			que.pop() ;
			came[ m.y ][ m.x ] = 1 ;
			if( map[ m.y ][ m.x ] == cheese )
			{
				if( a )
				{
				fx = m.x ;
				fy = m.y ;
				a = false ;
				}
				min = min > m.cnt ? m.cnt : min ;
			}
			for( int i = 0 ; i < 4 ; i++ )
			{
				if( m.x + mx[ i ] > -1 && m.x + mx[ i ] < w && m.y +  my[ i ] > -1 && m.y + my[ i ] < h )
				{
					s = m ;
					s.x += mx[ i ] ;
					s.y += my[ i ] ;
					s.cnt++ ;
					if( map[ s.y ][ s.x ] != -1 && came[ s.y ][ s.x ] == 0 )
					{
						que.push( s ) ;
					}
				}
			}
		}
		memset( came ,0 ,sizeof( came ) ) ;
		kyori += min ;
		min = 1000 ;
		m.x = fx ;
		m.y = fy ;
		m.cnt = 0 ;
		que.push( m ) ;
		a = true ;
	}
	cout << kyori << endl ;
	return 0 ;
}