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<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;

typedef pair<int, int> pi;
pair <int, int>p;
pair <int, int> po;
int n, m, c;
char ma[1001][1001];
int ix, iy, x, y;
int te[1001][1001];
int xw[4] = { 1, 0, -1, 0 };
int yw[4] = { 0, 1, 0, -1 };
char name[10] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int nat = 0;
int na;
int op = 10000;
int main()
{
	queue<pi> q;
	cin >> n >> m >> na;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 < m; i2++){
			cin >> ma[i][i2];
			te[i][i2] = op;
			if (ma[i][i2] == 'S'){
				p.first = i;
				p.second = i2;
			}
		}
	}

	int count;

	count = 0;
	q.push(p);

	te[p.first][p.second] = 0;
	while (!q.empty()){
		p = q.front();
		po = p;
		q.pop();

		for (int i = 0; i < 4; i++){
			x = p.first + xw[i];
			y = p.second + yw[i];
			if (0 <= x && x<n && 0 <= y && y<m&&ma[x][y] != 'X'&&te[x][y] == op){

				if (ma[x][y] == name[nat]){
					po.first = x;
					po.second = y;
					q.push(po);
					po = p;
					count += te[p.first][p.second] + 1;
					nat++;
					if (nat == na){ cout << count << endl; return 0; }
					//memset(te, op, sizeof(te));
					for (int i2 = 0; i2 <= n; i2++){
						for (int i3 = 0; i3 <= m; i3++){
							te[i2][i3] = op;
						}
					}
					te[x][y] = 0;
					break;
				}

				else{
					po.first = x;
					po.second = y;
					q.push(po);
					po = p;
					te[x][y] = te[p.first][p.second] + 1;
				}
			}
		}
	}
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<queue>
using namespace std;

typedef pair<int,int> COR;

int H,W,C;
vector<string> M;
int INF= 100000;

int cost[10][10];
bool come[1000][1000];

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


void solve(){

	for(int i=0;i<10;i++)
		for(int j=0;j<10;j++)
			cost[i][j]=INF;

	M.clear();
	vector<pair<int,int> > V;
	
	for(int i=0;i<H;i++){
		string h;
		cin>>h;
		for(int j=0;j<h.size();j++)
			if(isdigit(h[j]) || h[j]=='S')
				V.push_back(make_pair(i,j));
			
		M.push_back(h);
	}

	int now;
	for(int i=0;i<V.size();i++){
		if(isdigit(M[V[i].first][V[i].second]))
			now=M[V[i].first][V[i].second]-'0';

		if(M[V[i].first][V[i].second]=='S')
			now=0;

		memset(come,false,sizeof(come));
		queue<pair<COR,int> > Q;
		Q.push(make_pair(V[i],0));
		int count=0;
		while(!Q.empty() && count<C+1){
			pair<COR,int> p=Q.front();
			Q.pop();
			int y=p.first.first,x=p.first.second,c=p.second;
			if(y<0 || x<0 || y>=H || x>=W ||come[y][x] || M[y][x]=='X')
				continue;
			come[y][x]=true;
			if(M[y][x]=='0'+now+1){
				cost[now][now+1]=c;
				break;
			}

			for(int r=0;r<4;r++)
				Q.push(make_pair(make_pair(y+dy[r],x+dx[r]),c+1));
		}
	}

	int res=0;

	for(int i=0;i<V.size()-1;i++)
		res+=cost[i][i+1];
	cout<<res<<endl;
}

int main()
{
	while(cin>>H>>W>>C){
		solve();
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <string>
using namespace std;
#define MAXN 1001
char A[MAXN][MAXN];
int w,h,n;
int s[2];

void input()
{
    cin>>h>>w>>n;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>A[i][j];
            if(A[i][j]=='S'){
                s[0]=i;
                s[1]=j;
            }
        }
    }
}

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
typedef pair<pair<int,int>,int> P;

int known[MAXN][MAXN];

int bfs(int& x, int& y, char k)
{
    queue<P>que;
    que.push(P(make_pair(x, y), 0));
    while(!que.empty()){
        P cur = que.front();
        // cout<<"*"<<cur.first.first<<" "<<cur.first.second<<endl;
        que.pop();
        if(A[cur.first.first][cur.first.second]==k){
            x = cur.first.first;
            y = cur.first.second;
            return cur.second;
        }
        for(int i=0;i<4;i++){
            int nx = cur.first.first+dx[i];
            int ny = cur.first.second+dy[i];
            if(0<=nx&&nx<h && 0<=ny&&ny<w && A[nx][ny]!='X' && !known[nx][ny]){
                que.push(P(make_pair(nx, ny), cur.second+1));
                known[nx][ny]=1;
            }
        }
    }
    return 0x3fffffff;
}

int main()
{
    input();
    int res = 0;
    int x, y;
    x = s[0];
    y = s[1];

    for(int i=1;i<=n;i++){
        memset(known, 0, sizeof(int)*MAXN*MAXN);
        int tmp = bfs(x, y, '0'+i);
        res += tmp;
    }
    cout<<res<<endl;
    return 0;
}