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<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct k{
    int nx;
    int ny;
    int power;
    int nowans;
};
int fiemin[15][1003][1003];
int main(){
    int h,w,n;
    char fie[1003][1003];
    for(int i=0;i<15;i++){
        for(int j=0;j<1003;j++){
            for(int k=0;k<1003;k++){
                fiemin[i][j][k]=2100000000;
            }
        }
    }
        for(int j=0;j<1003;j++){
            for(int k=0;k<1003;k++){
                fie[j][k]='X';
            }
        }
    int nx,ny,po,judge;
    struct k now;
    queue<k> que;
    scanf("%d %d %d",&h,&w,&n);
    for(int i=0;i<h;i++)
        scanf("%s",&fie[i]);
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(fie[i][j]=='S'){
                now.ny=i,now.nx=j,now.power=0,now.nowans=0;
                fie[i][j]='.';
                goto a;
            }
        }
    }
a:
    que.push(now);
    for(int i=0;;i++){
        now=que.front(),que.pop();

        //printf("[%d][%d][%d][%d]\n",now.nx,now.ny,now.nowans,now.power);

        if(now.power==n){
            printf("%d\n",now.nowans);
            break;
        }
        if(0<=now.ny-1){
            if(fie[now.ny-1][now.nx]==now.power+1+'0'){
                if(now.nowans+1<fiemin[now.power+1][now.ny-1][now.nx]){
                        while (!que.empty())
                            que.pop();
                        now.ny--,now.power++,now.nowans++;
                        que.push(now);
                        fiemin[now.power+1][now.ny][now.nx]=now.nowans;
                        now.ny++,now.power--,now.nowans--;
                }
            }
            else if(fie[now.ny-1][now.nx]!='X'){
                if(now.nowans+1<fiemin[now.power][now.ny-1][now.nx]){
                        now.ny--,now.nowans++;
                        que.push(now);
                        fiemin[now.power][now.ny][now.nx]=now.nowans;
                        now.ny++,now.nowans--;
                }
            }
        }
        if(now.nx+1<w){
            if(fie[now.ny][now.nx+1]==now.power+1+'0'){
                if(now.nowans+1<fiemin[now.power+1][now.ny][now.nx+1]){
                        while (!que.empty())
                            que.pop();
                        now.nx++,now.power++,now.nowans++;
                        que.push(now);
                        fiemin[now.power+1][now.ny][now.nx]=now.nowans;
                        now.nx--,now.power--,now.nowans--;
                }
            }
            else if(fie[now.ny][now.nx+1]!='X'){
                if(now.nowans+1<fiemin[now.power][now.ny][now.nx+1]){
                        now.nx++,now.nowans++;
                        que.push(now);
                        fiemin[now.power][now.ny][now.nx]=now.nowans;
                        now.nx--,now.nowans--;
                }
            }
        }
        if(now.ny+1<h){
            if(fie[now.ny+1][now.nx]==now.power+1+'0'){
                if(now.nowans+1<fiemin[now.power+1][now.ny+1][now.nx]){
                        while (!que.empty())
                            que.pop();
                        now.ny++,now.power++,now.nowans++;
                        que.push(now);
                        fiemin[now.power+1][now.ny][now.nx]=now.nowans;
                        now.ny--,now.power--,now.nowans--;
                }
            }
            else if(fie[now.ny+1][now.nx]!='X'){
                if(now.nowans+1<fiemin[now.power][now.ny+1][now.nx]){
                        now.ny++,now.nowans++;
                        que.push(now);
                        fiemin[now.power][now.ny][now.nx]=now.nowans;
                        now.ny--,now.nowans--;
                }
            }
        }
        if(0<=now.nx-1){
            if(fie[now.ny][now.nx-1]==now.power+1+'0'){
                if(now.nowans+1<fiemin[now.power+1][now.ny][now.nx-1]){
                        while (!que.empty())
                            que.pop();
                        now.nx--,now.power++,now.nowans++;
                        que.push(now);
                        fiemin[now.power+1][now.ny][now.nx]=now.nowans;
                        now.nx++,now.power--,now.nowans--;
                }
            }
            else if(fie[now.ny][now.nx-1]!='X'){
                if(now.nowans+1<fiemin[now.power][now.ny][now.nx-1]){
                        now.nx--,now.nowans++;
                        que.push(now);
                        fiemin[now.power][now.ny][now.nx]=now.nowans;
                        now.nx++,now.nowans--;
                }
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#define min(x,y) ((x)<(y)?(x):(y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
int Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1,0,-1,0};
int ver[] = {0,-1,0,1};
int ans;


void clear();
void show();
P bfs(int cheese,P n);

int main(void) {
    scanf("%d %d %d\n", &H, &W, &N);
    P s,e;
    for (int i = 0; i<H; i++) {
        for (int j = 0; j<W; j++) {
            Maps[i][j] = getchar();
            if (Maps[i][j]=='S') {s.first = i; s.second = j;}
            if (Maps[i][j]=='0'+N) {e.first = i; e.second = j;}
        }
        getchar();
    }

    
    P tmp;
    tmp.first = s.first; tmp.second = s.second;
    
    for (int i = 1; i<=N; i++) {
        clear();
        tmp = bfs(i, tmp);
    }
    
    
    
    printf("%d\n", ans);
    return 0;
}

P bfs(int cheese,P n) {
    P end;
    queue<P> que;
    que.push(n);
    dist[n.first][n.second] = 0;
    
    while (que.size()) {
        P top = que.front();
        
        que.pop();
        for (int i = 0; i<4; i++) {
            int nx = top.first+ver[i], ny = top.second+hor[i];
            if (nx>=0 && nx<H && ny>=0 && ny<W && dist[nx][ny]==INFI) {
                if (Maps[nx][ny]=='.' || (isdigit(Maps[nx][ny]) && cheese!=Maps[nx][ny]-'0'))  {
                    que.push(P(nx,ny));
                    dist[nx][ny] = min(dist[nx][ny], dist[top.first][top.second]+1);
                } else if (isdigit(Maps[nx][ny]) && cheese==Maps[nx][ny]-'0') {
                    que.push(P(nx,ny));
                    dist[nx][ny] = min(dist[nx][ny], dist[top.first][top.second]+1);
                    end.first = nx; end.second = ny;
                    break;
                }
            }
        }
    }
    ans += dist[end.first][end.second];
    return end;
}

void clear() {
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            dist[i][j] = INFI;
}