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>
using namespace std;

char c[1000];
int H, W, N, a, i, j, k, ns;
complex<int> chs[11];
bool kuk[1002][1002];
int score[1002][1002];
complex<int> mov[4] = {complex<int>(0, 1), complex<int>(1, 0), complex<int>(0, -1), complex<int>(-1, 0)};
queue<complex<int> > qu;
complex<int> now, nm;
bool flg1, flg2 = flg1 = true;

int main(){
    scanf("%d%d%d", &H, &W, &N);
    for(i = 1; i <= H; ++i){
        scanf("%s", c);
        for(j = 0; j < W; ++j){
            kuk[i][j + 1] = true;
            switch(c[j]){
                case 'X':
                    kuk[i][j + 1] = false;
                    break;
                case 'S':
                    chs[0] = complex<int>(i, j + 1);
                    break;
                default:
                    chs[c[j] - 48] = complex<int>(i, j + 1);
                    break;
            }
        }
    }
    for(i = 1; i <= N; i += 2){
        for(j = 0; j <= H + 1; ++j){
            for(k = 0; k <= W + 1; ++k){
                score[j][k] = 2147483647;
            }
        }
        qu.push(chs[i]);
        score[real(chs[i])][imag(chs[i])] = 0;
        flg1 = true;
        flg2 = (i + 1 <= N);
        while(flg1 || flg2){
            now = qu.front();
            ns = score[real(now)][imag(now)];
            for(j = 0; j < 4; j++){
                nm = now + mov[j];
                int x = real(now + mov[j]), y = imag(now + mov[j]);
                if(chs[i - 1] == nm){
                    flg1 = false;
                }
                if(i + 1 <= N && chs[i + 1] == nm){
                    flg2 = false;
                }
                if(kuk[x][y] && score[x][y] > ns + 1){
                    qu.push(nm);
                    score[x][y] = ns + 1;
                }
            }
            qu.pop();
        }
        a += score[real(chs[i - 1])][imag(chs[i - 1])];
        if(i + 1 <= N)a += score[real(chs[i + 1])][imag(chs[i + 1])];
        queue<complex<int> > emp;
        swap(qu, emp);
    }
    cout << a << endl;
    return 0;
}

C++: #include <cstdio>
#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", &H, &W, &N);
    P s,e;
    for (int i = 0; i<H; i++) {
        getchar();
        for (int j = 0; j<W; j++) {
            Maps[i][j] = getchar();
            if (Maps[i][j]=='S') s.first = i, s.second = j;
        }
    }
    
    P tmp;
    tmp.first = s.first; tmp.second = s.second;
    Maps[tmp.first][tmp.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 nv = top.first+ver[i], nh = top.second+hor[i];
            if (nv>=0 && nv<H && nh>=0 && nh<W && dist[nv][nh]==INFI) {
                    if (Maps[nv][nh]=='.' || (isdigit(Maps[nv][nh]) && cheese!=Maps[nv][nh]-'0'))
                        que.push(P(nv,nh)),dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second]+1);
                    else if (isdigit(Maps[nv][nh]) && cheese==Maps[nv][nh]-'0') {
                        que.push(P(nv,nh));
                        dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second]+1);
                        end.first = nv; end.second = nh;
                        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;
}

C++: #include "stdio.h"
#include "queue"
#include "utility"
using namespace std;
typedef pair<int, int>P;

queue<P>Q;
P now;
P goal[10];
int N;
int W;
int H;
char map[1000][1000] = {};
int x[4] = { 1,0,-1,0 };
int y[4] = { 0,1,0,-1 };
int dis[1000][1000] ;
int ans=0;
int box = 0;

int main() {
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		scanf("%s", map[i]);
	}
	//printf("\n");
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (map[i][j] >= '1'&&map[i][j] <= '9') {
				goal[map[i][j] - '0'].first = i;
				goal[map[i][j] - '0'].second = j;
				map[i][j] = '.';
			}
			else if (map[i][j] == 'S') {
				now.first = i;
				now.second = j;
				map[i][j] = '.';
			}
		}
	}
	for (int i = 1; i <= N; i++) {
		Q.push(now);
		//printf("%d %d????????¢?´¢\n", now.first, now.second);
		box = 0;
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				dis[j][k] = 10000000;
			}
		}
		dis[Q.front().first][Q.front().second] = 0;
		while (!Q.empty()) {
			for (int j = 0; j < 4; j++) {
				if (map[Q.front().first + y[j]][Q.front().second + x[j]] == '.') {
					if (dis[Q.front().first + y[j]][Q.front().second + x[j]] > dis[Q.front().first][Q.front().second] + 1) {
						now.first = Q.front().first + y[j];
						now.second = Q.front().second + x[j];
						dis[now.first][now.second] = dis[Q.front().first][Q.front().second] + 1;
					//	printf("%d %d??????%d %d????????£???\n", Q.front().first, Q.front().second, now.first, now.second);
						Q.push(now);
						if (now.first == goal[i].first&&now.second == goal[i].second) {
						//	printf("hijiki\n");
							box = dis[now.first][now.second];
							while (!Q.empty()) {
								Q.pop();
							}
							if (Q.empty())break;
						}
					}
				}
			}
			//printf("hijiki\n");
			if (!Q.empty()) {
				Q.pop();
			}
			//printf("hijiki\n");
		}
		//printf("hijiki\n");
		ans += box;
	}
	printf("%d\n", ans);
	return 0;
}