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"

using namespace std;

//#define int long long

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

#define pb push_back
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()

#define INTMAX 2147483647
#define INF 200000000
#define TINT(str) atoi(str.c_str())
#define TSTR(num) to_string(num)

using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VII = vector<vector<int>>;

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

PII goal[10];
char k[1000][1000];
int fx, fy;
int H,W;

int BFS(int x, int y)
{
    VII cost;
    cost = VII(H, VI(W, INF));

    cost[fx][fy] = 0;

    queue<PII> q;

    q.push(make_pair(fx, fy));

    while(!q.empty())
    {
        PII now = q.front();
        q.pop();
        int nx = now.first;
        int ny = now.second; 

        REP(i, 4)
        {
            if(nx + dx[i] >= H ||
            nx + dx[i] < 0) continue;
            if(ny + dy[i] >= W || 
            ny + dy[i] < 0) continue;

            if(k[nx + dx[i]][ny + dy[i]] != 'X')
            {
                if(nx + dx[i] == x &&
                    ny + dy[i] == y)
                {
                    return cost[nx][ny] + 1;
                }

                if(cost[nx + dx[i]][ny + dy[i]] > 
                cost[nx][ny])
                {
                    cost[nx + dx[i]][ny + dy[i]] =
                        cost[nx][ny] + 1;
                    q.push(make_pair(nx + dx[i], ny + dy[i]));
                }
            }
            else continue;
        }
    }

    return -1;
}

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

    char str[1001];
    REP(h, H)
    {
        scanf("%s", str);
        REP(w, W)
        {
            k[h][w] = str[w];
            if(k[h][w] == 'S')
            {
                fx = h;
                fy = w;
            }
            if(k[h][w] != '.' &&
                k[h][w] != 'X')
            {
                goal[k[h][w] - '1'] = make_pair(h, w);
            }
        }
    }
    int ans = 0;

    REP(i, N)
    {
        ans += BFS(goal[i].first, goal[i].second);
        fx = goal[i].first;
        fy = goal[i].second;
    }

    cout << ans << endl;

    return 0;
}

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

char map[1000][1000];
int check[1000][1000];
int cx[10];
int cy[10];

int main(){
	int i,j,k,l,h,w,ch,pd;
	int tex,tey,ted;
	std::queue<int> qx;
	std::queue<int> qy;
	std::queue<int> qd;

	scanf("%d %d %d",&h,&w,&ch);
	scanf("%c",&map[0][0]);
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++){
			scanf("%c",&map[i][j]);
			if(map[i][j]=='S'){cy[0]=i; cx[0]=j;}
			if(map[i][j]=='1'){cy[1]=i; cx[1]=j;}
			if(map[i][j]=='2'){cy[2]=i; cx[2]=j;}
			if(map[i][j]=='3'){cy[3]=i; cx[3]=j;}
			if(map[i][j]=='4'){cy[4]=i; cx[4]=j;}
			if(map[i][j]=='5'){cy[5]=i; cx[5]=j;}
			if(map[i][j]=='6'){cy[6]=i; cx[6]=j;}
			if(map[i][j]=='7'){cy[7]=i; cx[7]=j;}
			if(map[i][j]=='8'){cy[8]=i; cx[8]=j;}
			if(map[i][j]=='9'){cy[9]=i; cx[9]=j;}
		}
	}

	i=0;
	while(i<ch){
		qx.push(cx[i]); qy.push(cy[i]); qd.push(0);
		++i;
		while(!qx.empty()){
			tex=qx.front(); tey=qy.front(); ted=qd.front();
			qx.pop(); qy.pop(); qd.pop();
			check[tey][tex]=1;
			if(tey==cy[i] && tex==cx[i]){
				pd+=ted;
				while(!qx.empty())qx.pop();
				while(!qy.empty())qy.pop();
				while(!qd.empty())qd.pop();
				for(k=0;k<h;k++){
					for(l=0;l<=w;l++)check[k][l]=0;
				}
				break;
			}
			++ted;
			if(tex+1<w && map[tey][tex+1]!='X' && check[tey][tex+1]==0){
				qx.push(tex+1); qy.push(tey); qd.push(ted);
			}
			if(tex-1>=0 && map[tey][tex-1]!='X' && check[tey][tex-1]==0){
				qx.push(tex-1); qy.push(tey); qd.push(ted);
			}
			if(tey+1<h && map[tey+1][tex]!='X' && check[tey+1][tex]==0){
				qx.push(tex); qy.push(tey+1); qd.push(ted);
			}
			if(tey-1>=0 && map[tey-1][tex]!='X' && check[tey-1][tex]==0){
				qx.push(tex); qy.push(tey-1); qd.push(ted);
			}
		}
	}

	printf("%d\n",pd);
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[1001][1001];
long long  check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	m=1;
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=m;
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			m++;
		}
		else{
			for(i=0;i<4;i++){
				if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=m&&cheese[y.a][y.b]!=-1){
					x.a=y.a+q[i];
					x.b=y.b+p[i];
					x.c=y.c+1;
					x.d=y.d;
					Q.push(x);
				}
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}