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

C++: #include <cstdio>
#include <utility>
#include <queue>
#include <cstdlib>
using namespace std;

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

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

void bfs()
{
    for (int n = 1; n < N + 1; n++)
    {
        queue<pair<int, int>> que;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                d[i][j] = INF;
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;

        while (que.size())
        {
            pair<int, int> p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + (char)n)
            {
                cheese = p;
                break;
            }

            for (int i = 0; i < 4; i++)
            {
                int nx = p.first + dx[i];
                int ny = p.second + dy[i];

                if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' && d[nx][ny] == INF)
                {
                    que.push(pair<int, int>(nx, ny));
                    d[nx][ny] = d[p.first][p.second] + 1;
                }
            }
        }
        result += d[cheese.first][cheese.second];
        start = cheese;
    }
}

int main() {
    scanf(" %d %d %d", &H, &W, &N);
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            scanf(" %c", &field[i][j]);
            if (field[i][j] == 'S')
            {
                start.first = i;
                start.second = j;
            }
        }
    }
    bfs();
    printf("%d\n", result);
}

C++: // from : http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=1811250#1

#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;

#define DEBUG(x) cout<<(#x)<<": "<<(x)<<endl
 
int nowS = 0;
int x, y, n;
int mp[1000][1000] = {};
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
queue<int> bufX, bufY, bufD;
 
bool search(int xx, int yy, int dd) {
    bufX.pop();
    bufY.pop();
    bufD.pop();
 
    for (int i = 0; i < 4; i++) {
        int nx = xx+k[i];
        int ny = yy+h[i];
        if (nx < 0 || nx >= x || ny < 0 || ny >= y)//?????????
            continue;
        if (mp[ny][nx] == -1) {//?????
            continue;
        }
        if (cx[nowS] == nx && cy[nowS] == ny) {//??????
            nowS++;
            while (!bufX.empty()) {
                bufX.pop();
                bufY.pop();
                bufD.pop();
            }
            if (nowS == n) {//?????????????????????
                cout << dd + 1 << endl;
                return true;
            }
            else {//
                bufX.push(nx);
                bufY.push(ny);
                bufD.push(dd + 1);
                mp[ny][nx] = nowS + 1;
            }
            return false;
        }
        if(mp[ny][nx] >= 0 && mp[ny][nx] < nowS + 1){
            bufX.push(nx);
            bufY.push(ny);
            bufD.push(dd + 1);
            mp[ny][nx] = nowS + 1;
        }
    }
    return false;
}
 
int main() {
    int ch = 0;
    cin >> y >> x >> n;
    for (int i = 0; i < y; i++) {//??\???
        char c[1001];
        cin >> c;
        for (int j = 0; j < x; j++) {
            if(c[j] == 'X')
                mp[i][j] = -1;
            else if(c[j] == 'S'){
                bufX.push(j);
                bufY.push(i);
                bufD.push(0);
                mp[j][i] = 1;
            }   
            else {
                mp[i][j] = 0;
            }
            if (c[j] >= 48 && c[j] <= 57) {
                int m;
                m = c[j] - 49;
                cx[m] = j;
                cy[m] = i;      
                ch++;
            }
        }
    }
     
 
    while(!search(bufX.front(), bufY.front(), bufD.front())){}//???????????????§?????????
 
    return 0;
}