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<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char a[1001][1001];
int v[1001][1001],n,h,w,sx,sy,ans;
int dir[4][2]={-1,0,1,0,0,1,0,-1};
char b[1001];
int s[101][101];
struct st
{
    int x,y;
}st1,st2,fl,pp;
int check(st kk)
{
    if(kk.x>=0&&kk.x<h&&kk.y>=0&&kk.y<w)
        return 1;
    else
    	return 0;
}
int  bfs(char c)
{
	queue<st>qq;	
	qq.push(pp);
    memset(v,0,sizeof(v));
    memset(s,0,sizeof(s));
    while(!qq.empty())
    {   	
		st1=qq.front();		
        qq.pop();
     	if(a[st1.x][st1.y]==c)
     	{
	     	pp=st1;			
			//printf("+%d %d+ %d\n",st1.x,st1.y,s[pp.x][pp.y]);	
	 		break;  
    	}             
        for(int i=0;i<4;i++)
        {
            st2.x=st1.x+dir[i][0];
            st2.y=st1.y+dir[i][1];
            if(check(st2)&&v[st2.x][st2.y]==0&&a[st2.x][st2.y]!='X'&&s[st2.x][st2.y]==0)
            {
               	v[st2.x][st2.y]=1;
                s[st2.x][st2.y]=s[st1.x][st1.y]+1;
                //printf("-%d %d %d-\n",st2.x,st2.y,s[st2.x][st2.y]);
                qq.push(st2);
            }
        }
    }	
	return s[pp.x][pp.y];
   // printf("(%d %d %d %c)\n",st1.x,st1.y,s[st1.x][st1.y],c);
   
}
int main()
{
    int  i,j,k,m,l;
    while(~scanf("%d %d %d",&h,&w,&n))
    {    
    	getchar();
        memset(v,0,sizeof(v));
        memset(s,0,sizeof(s));
        for(i=0;i<h;i++)
        {
            scanf("%s",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            b[i]=i+1+'0';
        }
        for(i=0;i<h;i++)
        {
            for(j=0;j<w;j++)
            {
                if(a[i][j]=='S')
                {
                    pp.x=i;
                    pp.y=j;
                }
            }
        }
        //printf("%d %d\n",sx,sy);
        int sum=0;
		for(i=0;i<n;i++)
       { 
            sum+=bfs(b[i]);
            //printf("[%d]\n",sum);
        }
        printf("%d\n",sum);
    }
    return 0;
}

C++: //
//  0558.cpp
//  
//
//  Created by Yoshida Satoshi on 2017/11/16.
//
//

#include <iostream>
#include <queue>
#include <utility>
#include <stdlib.h>

#define MAX 1000
#define INF 100000000
#define FOR(index,max) for(int index=0;index<max;index++)

using namespace std;

typedef pair<int, int> P;

int main(){
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    
    int nx,ny;
    
    queue<P> que;
    int H,W,N;
    int state[MAX][MAX];//0:????????°or????????? -1:????????¶???
    bool isVisited[MAX][MAX];
    int d[MAX][MAX];
    int ans=0;
    int sx[9];
    int sy[9];
    P p;
    char in;
    cin>>H>>W>>N;
    FOR(i,H){
        FOR(j,W){
            cin>>in;
            if(in=='S'){
                sx[0]=i;
                sy[0]=j;
                state[i][j]=0;
            }
            else if(in=='X') state[i][j]=-1;
            else if(in=='.') state[i][j]=0;
            else{
                state[i][j]=0;
                sx[atoi(in)]=i;
                sy[atoi(in)]=j;
                
            }
        }
    }
    
    FOR(k,N){
        FOR(i,H){
            FOR(j,W){
                isVisited[i][j]=false;
                d[i][j]=INF;
            }
        }
        que.push(P(sx[k],sy[k]));
        d[sx[k]][sy[k]]=0;
        while(que.size()){
            p=que.front();
            que.pop();
            
            if(p==P(sx[k+1],sy[k+1]))   break;
            
            FOR(l,4){
                nx=p.first+dx[l];
                ny=p.seond+dy[i];
                
                if(!(nx<0||ny<0||nx>H-1||ny>W-1||state[nx][ny]==-1||d[nx][ny]==INF)){
                    que.push(P(nx,ny));
                    d[nx][ny]=d[p.first][p.second]+1;
                }
            }
        }
        
        ans += d[start[k+1].first][start[k+1].second];
    }
    
    cout<<ans<<endl;
    
    return 0;
}

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

int h, w, n;
vector<vector<bool>> mp;
vector<pair<int,int>> f;

int calc(pair<int,int> start, pair<int,int> end){
	vector<vector<int>> hi(w, vector<int>(h, 1145141919));
	priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>>> q;
	q.push(make_pair(0, start));
	while(q.size()){
		int c = q.top().first;
		pair<int,int> p = q.top().second;
		q.pop();
		if(p == end) return c;
		int x = p.first;
		int y = p.second;
		for(int dx = -1; dx <= 1; dx++){
			for(int dy = -1; dy <= 1; dy++){
				if((!!dx ^ !!dy) && 0 <= x+dx && x+dx < w && 0 <= y+dy && y+dy < h && !mp[x+dx][y+dy] && hi[x+dx][y+dy] > c+1){
					hi[x+dx][y+dy] = c+1;
					q.push(make_pair(c+1, make_pair(x+dx, y+dy)));
				}
			}
		}
	}
	return -1;
}

int main(){
	cin >> h >> w >> n;
	mp.assign(w, vector<bool>(h, false));
	f.assign(n+1, make_pair(0, 0));
	for(int y = 0; y < h; y++){
		for(int x = 0; x < w; x++){
			char a;
			cin >> a;
			if(a == 'X') mp[x][y] = true;
			if(a == 'S') f[0] = make_pair(x, y);
			if('1' <= a && a <= '9') f[a - '0'] = make_pair(x, y);
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++){
		ans += calc(f[i-1], f[i]);
	}
	cout << ans << endl;
	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 map[ 1000 ][ 1000 ] ;
int main()
{
	ST m ,s ;
	queue< ST > que ;
	int n ;
	int w ,h ;
	int kyori = 0 ;
	int min = 1000 ;
	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 ;
				min = 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 ) ) ;
		kyori += min ;
		min = 1000 ;
		m.x = fx ;
		m.y = fy ;
		m.cnt = 0 ;
		que.push( m ) ;
	}
	cout << kyori << endl ;
	return 0 ;
}