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>
#define r(i,n) for(int i=0;i<n;i++)
#define mk make_pair
using namespace std;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
int main(){
  int h,w,n,sum=0,x1,y1;
  cin>>h>>w>>n;
  string s[h];
  for(int i=0;i<h;i++)cin>>s[i];
  r(i,h)r(j,w)if(s[i][j]=='S')y1=i,x1=j;
  for(int o=1;o<=n;o++){
    int used[h][w]={};
    queue<pair<pair<int,int>,int> >q;
    q.push(mk(mk(y1,x1),0));
    used[y1][x1]=1;
    while(!q.empty()){
      pair<pair<int,int>,int> p=q.front();q.pop();
      int x=p.first.second,y=p.first.first,sum2=p.second;
      if(s[y][x]-'0'==o){
        sum+=sum2;
        y1=y;
        x1=x;
        goto L;
      }
      r(j,4){
        int xx=x+dx[j],yy=y+dy[j];
        if(xx>=0&&yy>=0&&yy<h&&xx<w)
          if(!used[yy][xx])
            if(s[yy][xx]!='X'){
              q.push(mk(mk(yy,xx),sum2+1));
              used[yy][xx]++;
            }
      }
    }L:;
  }
  cout<<sum<<endl;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std ;
struct ST
{
	int x ;
	int y ;
	int cnt = 0 ;
} ;
bool came[ 1000 ][ 1000 ];
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 ];
	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++ )
	{
		fill( map[ i ] ,map[ i ] + w ,0 ) ;
		fill( came[ i ] ,came[ i ] + w ,0 ) ;
		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 ;
			}
		}
	}
	que.push( m ) ;
	while( n )
	{
		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 )
			{
				fx = m.x ;
				fy = m.y ;
				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 ) ;
					}
				}
			}
		}
		for( int i = 0 ; i < h ; i++ )
		{
			fill( came[ i ] ,came[ i ] + w ,0 ) ;
		}
		kyori += min ;
		min = 1000 ;
		m.x = fx ;
		m.y = fy ;
		m.cnt = 0 ;
		que.push( m ) ;
	}
	cout << kyori << endl ;
	return 0 ;
}

C++: #include <iostream>
#include <queue>
using namespace std;

#define INFTY 10000000

int d[1001][1001],n;
char map[1001][1001];
int h,w;
pair< int,int > T[10];

int bfs( pair<int, int> p1, pair<int, int> p2 ) {
  
  queue< pair<int,int> > Q;
  
  for ( int i = 1; i <= h; i++ ) {
    for ( int j = 1; j <= w; j++ ) d[i][j] = INFTY;
  }
  
  d[p1.first][p1.second] = 0;
  Q.push(p1);   
  int u,v;
    
  while ( !Q.empty() ) {
    p1 = Q.front(); Q.pop();
    u = p1.first;
    v = p1.second;
        	
    if ( map[u+1][v] != 'X' &&  u+1 <= h && d[u+1][v] == INFTY ) {
	p1.first = u+1;
	p1.second = v;
	d[u+1][v] = d[u][v]+1;
	Q.push(p1);
    } 
    if ( map[u][v+1] != 'X' && v+1 <= w && d[u][v+1] == INFTY ) {
	p1.first = u;
	p1.second = v+1;
	d[u][v+1] = d[u][v]+1;
	Q.push(p1);
    } 
    if ( map[u-1][v] != 'X' && u-1 >= 1 && d[u-1][v] == INFTY ) {
	p1.first = u-1;
	p1.second = v;
	d[u-1][v] = d[u][v]+1;
	Q.push(p1);
    } 
    if ( map[u][v-1] != 'X' && v-1 >= 1 && d[u][v-1] == INFTY ) {
	p1.first = u;
	p1.second = v-1;
	d[u][v-1] = d[u][v]+1;
	Q.push(p1);
      }
	
  }
  
  /* for ( int i = 1; i <= h; i++ ) {
    for ( int j = 1; j <= w; j++ )  cout << d[i][j] << " ";
    cout << endl;
  }

  cout << endl;
  */
  return d[p2.first][p2.second];

}

int main() {
  
  cin >> h >> w >> n;
  for ( int i = 1; i <= h; i++ ){
    for ( int j = 1; j <= w; j++ ) {
      cin >> map[i][j];
      if ( map[i][j] == 'S' ){
	T[0].first = i;
	T[0].second = j;
      }
    }
  }
  
  for ( int i = 1; i <= h; i++ ) {
    for ( int j = 1; j <= w; j++ ) {
      if ( map[i][j] >= '1' && map[i][j] <= '9' ) {
	T[ map[i][j]-'0' ].first = i;
	T[ map[i][j]-'0' ].second = j;
      }
    }
  }

  int ans = 0;
  for ( int i = 0; i < n; i++ ) ans += bfs( T[i], T[i+1] );

  cout << ans << endl;
     
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstring>
using namespace std;
class Node{
public:
    int x,y,cost;
    Node(int x,int y,int cost):x(x),y(y),cost(cost){}
    Node(){}
};
char map[1010][1010];
bool flag[1010][1010];
int h,w,n;
int bfs(char s,char e);
int main()
{
    cin>>h>>w>>n;
    for(int j=0;j<h;++j){
        for(int i=0;i<w;++i){
            cin>>map[i][j];
        }
    }
    int ans=0;
    char f=n+'0';
    ans+=bfs('S','1');
    for(char S='1';S<f;++S){
        ans+=bfs(S,S+1);
    }
    cout<<ans<<endl;
    return 0;
}
int bfs(char s,char e)
{
    int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
    memset(flag,0,sizeof(flag));
    queue<Node> q;
    for(int j=0;j<h;++j){
        for(int i=0;i<w;++i){
            if(map[i][j]==s){
                q.push(Node(i,j,0));
            }
        }
    }
    while(!q.empty())
    {
        Node t=q.front();q.pop();
        if(map[t.x][t.y]==e)return t.cost;
        if(flag[t.x][t.y])continue;
        for(int i=0;i<4;++i){
            Node next=t;
            next.cost++;
            next.x+=dx[i];next.y+=dy[i];
            if(next.x>=0&&next.y>=0&&next.x<w&&next.y<h&&map[next.x][next.y]!='X')q.push(next);
        }
        flag[t.x][t.y]=true;
    }
}