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 <vector>
#include <cctype>
#include <queue>
#include <string>
using namespace std;
const int dx[4] = {0,1,-1,0};
const int dy[4] = {-1,0,0,1};;
int bfs(vector<vector<char> > &field,int sx,int sy,int gx,int gy){
  int cost = 0;
  queue<pair<int,int> > que;
  que.push(pair<int,int>(sx,sy));
  vector<vector<int> > keep(1001,vector<int>(1001,0));
  while(!que.empty()){
    pair<int,int> tmp = que.front();que.pop();
    int cx = tmp.first;
    int cy = tmp.second;
    for(int i = 0;i < 4;i++){
      int nx = cx +dx[i];
      int ny = cy +dy[i];
      if(nx >= 0 && nx < field[0].size() && ny >= 0 && ny < field.size() && field[ny][nx] != 'X' && keep[ny][nx] == 0){
	if(nx == gx && ny == gy){
	  return keep[cy][cx]+1;
	}else{
	  keep[ny][nx] = keep[cy][cx]+1;
	  que.push(pair<int,int>(nx,ny));
	}
      }
    }
  }
  return -1;
}
int main(){
  int h,w,n;
  cin >> h >> w >> n;
  vector<vector<char> > field(h,vector<char>(w));
  vector<pair<int,int> > che(n);
  int sx,sy;
  for(int i = 0;i < h;i++){
    string str;
    cin >> str;
    for(int j = 0;j < w;j++){
      field[i][j] = str[j];
      if(isdigit(field[i][j])){
	che[(field[i][j] - '0')-1] = pair<int,int>(i,j);//y,x
      }else if(field[i][j] == 'S'){
	sx = j;sy = i;
      }
    }
  }
  //  cout << "aaaaaa" << endl;
  int ans =0;
  for(int i = 0;i < n;i++){
    if(i == 0){
      ans += bfs(field,sx,sy,che[i].second,che[i].first);
    }else{
      ans += bfs(field,che[i-1].second,che[i-1].first,che[i].second,che[i].first);
    }
  }
  cout << ans << endl;
  
  return 0;
}

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

char ma[1001][1001];
bool visited[1001][1001];
int h,w,n;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int sx,sy;
vector<pair<int,int> >vec(10);
queue<pair<pair<int,int>,int> >que;
int bfs(int d){
    while(que.size()){
        pair<pair<int,int>,int>q=que.front();
        que.pop();
        int x=q.first.first;
        int y=q.first.second;
        if(visited[y][x])continue;
        if(ma[y][x]-'0'==d){
            return q.second;
        }
        visited[y][x]=1;
        for(int i=0;i<4;i++){
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx>=0&&nx<w&&ny>=0&&ny<h&&ma[ny][nx]!='X'&&!visited[ny][nx]){
                que.push(make_pair(make_pair(nx, ny),q.second+1));
            }
        }
    }
    return 0;
}

int main(){
    cin>>h>>w>>n;
    for(int i=0;i<h;i++){
        string str;
        cin>>str;
        for(int j=0;j<w;j++){
            ma[i][j]=str[j];
            if(ma[i][j]>'0'&&ma[i][j]<='9'){
                vec[ma[i][j]-'0']=make_pair(j, i);
            }
            if(ma[i][j]=='S'){
                vec[0]=make_pair(j, i);
            }
        }
    }
    int c=0;
    for(int i=1;i<=n;i++){
        while(que.size())que.pop();
        memset(visited,0,sizeof(visited));
        que.push(make_pair(make_pair(vec[i-1].first,vec[i-1].second),0));
        c+=bfs(i);
    }
    cout<<c<<endl;
}

C++: #include<iostream>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int>p;
int m,n,h,sx,sy;
int total=0;
char a[1001][1001];
int d[1001][1001];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int bfs()
{
    int num=0;
    queue<p>q;
    for(int cc=0;cc<h;cc++)
    {
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            d[i][j]=INF;
            d[sx][sy]=0;
            q.push(p(sx,sy));
    while(q.size())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        if((int)a[x][y]==49+cc)
        {
            num=d[x][y];
            sx=x;
            sy=y;
            break;
        }
        for(int k=0;k<4;k++)
        {
            int nx=x+dx[k];
            int ny=y+dy[k];
            if(nx>=0&&nx<m&&ny>=0&&ny<n&&a[nx][ny]!='X'&&d[nx][ny]==INF)
            {
                q.push(p(nx,ny));
                d[nx][ny]=d[x][y]+1;
            }
        }
    }
    total+=num;
    while(!q.empty())
        q.pop();
    }
    return total;
}
int main()
{
    cin>>m>>n>>h;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
        cin>>a[i][j];
        if(a[i][j]=='S')
        {
            sx=i;
            sy=j;
        }
        }
    }
    int cwt=bfs();
    cout<<cwt<<endl;
    return 0;
}