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 rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;

const int INF = 1001001001;
int di[] = { 1,-1, 0, 0};
int dj[] = { 0, 0, 1,-1};

int main(){
    int h,w,n;
    cin >> h >> w >> n;

    vector<string> map(h);
    rep(i,h) cin >> map[i];

    int si,sj;
    rep(i,h) rep(j,w) if(map[i][j] == 'S') {si = i; sj = j;}

    queue<P> q;
    q.emplace(si,sj);

    int dist[h][w];
    rep(i,h) rep(j,w) dist[i][j] = INF;
    dist[si][sj] = 0;

    int ans = 0;
    rep(goal,n){
        char G = goal + 1 + '0';
        int gi,gj;
        rep(i,h) rep(j,w) if(map[i][j] == G){gi = i; gj = j;}
        while(!q.empty()){
            P p = q.front(); q.pop();
            int i = p.first, j = p.second;
            rep(dir,4){
                int ni = i + di[dir];
                int nj = j + dj[dir];
                if(ni<0||nj<0||ni>=h||nj>=w)continue;
                if(map[ni][nj] == 'X'||dist[ni][nj] != INF)continue;
                dist[ni][nj] = dist[i][j] + 1;
                q.emplace(ni,nj);
            }
        }
        ans += dist[gi][gj];
        q.emplace(gi,gj);
        rep(i,h) rep(j,w) dist[i][j] = INF;
        dist[gi][gj] = 0;
    }

    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<queue>
#include <map>
#include<cmath>
#include<vector>
#define MAX_N 1000000
#define INF 1000000
using namespace std;



struct note
{
    int x, y;
};
note start, ende;
typedef pair<int, int> P;
int H, W, N;
int sum;
int d[100][100];
char s[100][100];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
void bfs(note t, note e)
{
    queue<P> que;
    memset(d, -1, sizeof(d));
    int x = t.x;
    int y = t.y;
    d[x][y] = 0;
    que.push(P(x, y));
    while (!que.empty())
    {
        P p = que.front();
        que.pop();
        int v = p.first;
        int u = p.second;
        for (int i = 0; i < 4; i++)
        {
            int xx = v + dx[i];
            int yy = u + dy[i];
            if (xx >= 0 && xx < H && yy >= 0 && yy < W && s[xx][yy] != 'X' && d[xx][yy] == -1)
            {
                d[xx][yy] = d[v][u] + 1;
                que.push(P(xx, yy));
            }
        }
    }
    sum += d[e.x][e.y];
    start = e;
    return ;
}
void solve()
{
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (s[i][j] == 'S')
            {
                start.x = i;
                start.y = j;
                i = H;
                j = W;
            }
        }
    }
    for (int k = 0; k < N; k++)
    {
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (s[i][j] == 49 + k)
                {
                    ende.x = i;
                    ende.y = j;
                    bfs(start, ende);
                    i = H;
                    j = W;
                }
            }
        }
    }
    printf("%d\n", sum);
    return ;
}
int main()
{

    while (~scanf("%d%d%d", &H, &W, &N))
    {
    s[0][0] = '\0';
    sum = 0;
    for (int i = 0; i < H; i++)
    {
        scanf("%s", s[i]);
    }
    solve();
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
 
typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;
const int MAX=(1<<14)-1;
mouse Q[MAX+1];
int s,num;

mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n;
int cost;
 
main(){
  scanf("%d%d%d ",&h,&w,&n);
  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h)read(ku[i],1,w+1,0);
 
 
  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }
 
 
  Q[s+num++]=(mouse){cx,cy,0,0};
  maxch[cx][cy]=0;
 
  while(true){
    t=Q[s++&MAX];
    num--;
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;
 
    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
      if(ku[nx][ny]=='X')continue;
      nch=ch;
      if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
        nch=ch+1;
      }
 
      if(maxch[nx][ny]>=nch)continue;
      if(nch==n){
        printf("%d\n",cost+1);
        return 0;
      }
      maxch[nx][ny]=nch;
      Q[s+num++&MAX]=(mouse){nx,ny,cost+1,nch};
    }
  }
}