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 <cstring>
#include <cassert>
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <queue>
#include <utility>

using namespace std;

#define MH 1000
int H, W, N;
string F[MH];

void input() {
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        cin >> F[i];
    }
}

struct P {
    int y, x, t;
    P(int y, int x, int t = 0) : y(y), x(x), t(t) {}
};
P pos(char x) {
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (F[i][j] == x) return P(i, j);
        }
    }
    assert(0);
}

int bfs(char from, char to) {
    P init = pos(from);
    queue<P> Q; Q.push(init);
    static const int dy[] = {0, 1, 0, -1},
                     dx[] = {1, 0, -1, 0};
    bool used[H][W]; memset(used, 0, sizeof(used));
    while (!Q.empty()) {
        P cur = Q.front(); Q.pop();
        int y = cur.y,
            x = cur.x;
        if (F[y][x] == to) return cur.t;
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i],
                nx = x + dx[i];
            if (ny < 0 || ny >= H) continue;
            if (nx < 0 || nx >= W) continue;
            if (used[ny][nx]) continue;
            used[ny][nx] = true;
            if (F[ny][nx] != 'X') {
                Q.push(P(ny, nx, cur.t + 1));
            }
        }
    }
    assert(0);
}

void solve() {
    char N1 = N + '0';
    int ans = bfs('S', '1');
    for (char n = '1'; n < N1; n++) {
        ans += bfs(n, n +1);
    }
    cout << ans << endl;
}

int main() {
    input();
    solve();
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<limits.h>
#include<queue>
char o[1002][1003],*p;
int h,w;
int F(int sx,int sy,int gx,int gy)
{
	struct S{int x,y,s;S(int x,int y,int s):x(x),y(y),s(s){}};
	S s(sx,sy,0);
	std::queue<S>q;
	static int d[1001][1001],i,j,a[]={-1,0,1,0};
	for(i=1;i<=h;++i)for(j=1;j<=w;++j)d[i][j]=INT_MAX;
	q.push(s);
	while(!q.empty())
	{
		s=q.front();q.pop();
		if(o[s.y][s.x]=='X' || d[s.y][s.x]<=s.s)continue;
		d[s.y][s.x]=s.s;
		for(i=0;i<4;++i) q.push(S(s.x+a[i], s.y+a[(i+1)%4],s.s+1));
	}
	return d[gy][gx];
}
int main()
{
	int n,i,j,x[10],y[10],z,s=0;
	scanf("%d%d%d",&h,&w,&n);
	for(i=0;i<h;++i)
	{
		scanf("%s",&o[i+1][1]);
		p=&o[i+1][1];
		while(p=strpbrk(p,"S123456789"))
			z=isdigit(*p)?*p-'0':0,y[z]=i+1,x[z]=p-o[i+1],++p;
	}
	for(j=0;j<w+2;++j)o[0][j]=o[h+1][j]='X';
	for(i=0;i<h+2;++i)o[i][0]=o[i][w+1]='X';
	for(i=0;i<n;++i)s+=F(x[i],y[i],x[i+1],y[i+1]);
	printf("%d\n",s);
	return 0;
}

C++: #include<bits/stdc++.h>
#include<ctype.h>
#define int long long int
#define rep(a,b) for(int a=0;a<b;a++)
#define INF 1000006
using namespace std;

int tx[] = {1,0,-1,0};
int ty[] = {0,1,0,-1};
int w,h,che,sx,sy;
char bases;
int base[1003][1003],base2[1003][1003],memo[1003][1003];

int chee(int x,int y,int n){
    int ans=INF;
    if(memo[x][y]>0&&memo[x][y]!=INF)return memo[x][y];
    else if(base[x][y]==n)return 0;
    else if(base[x][y]>=0){
        base[x][y]=-1;
        for(int i=0;i<4;i++){
            if(x+tx[i]>=0&&x+tx[i]<w&&y+ty[i]>=0&&y+ty[i]<h){
                ans = min(ans,chee(x+tx[i],y+ty[i],n)+1);
            }
        }base[x][y]=0;
    }
    memo[x][y]=ans;
    return ans;
}
void sho(int b[1003][1003],int a[1003][1003]){
    rep(i,1003){
        rep(j,1003){
            b[i][j]=a[i][j];
        }
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int zx[20],zy[20];
    cin >> h >> w >> che;
    rep(f,h){
        rep(s,w){
            cin >> bases;
            switch(bases){
                case 'S':sx=s;sy=f;
                case '.':base[s][f]=0;
                break;
                case 'X':base[s][f]=-1;
                break;
                default:{base[s][f]=bases - '0';
                        zx[base[s][f]]=s;
                        zy[base[s][f]]=f;
                        }

            }
        }
    }
    sho(base2,base);
    int ans=0;
    ans+=chee(sx,sy,1);
    for(int i=2;i<=che;i++){
        sho(base,base2);
        fill((int*)memo,(int*)(memo+1003),0);
        ans+=chee(zx[i-1],zy[i-1],i);
    }
    cout << ans << endl;
return 0;
}

C++: #include<iostream>
#include<queue>
const int MAX_H = 1001;
const int MAX_W = 1001;
const int vx[4] = { 0, 1, 0, -1 };
const int vy[4] = { 1, 0, -1, 0 };
const int INF = 10000000;
const char num[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char c[MAX_H][MAX_W], b;
int y[MAX_H][MAX_W];
int h, w, n;
typedef std::pair<int, int> P;
P p[9];
P nest;
int bfs(P start, P end){
	std::queue<P> Q;
	P b;
	for (int i = 0; i<h; i++){
		for (int j = 0; j<w; j++){
			y[i][j] = INF;
		}
	}
	Q.push(start);
	y[start.first][start.second] = 0;
	while (Q.size()){
		b = Q.front();
		Q.pop();
		if (b == end){
			break;
		}
		for (int i = 0; i<4; i++){
			if (y[b.first][b.second] + 1<y[b.first + vx[i]][b.second + vy[i]] && y[b.first + vx[i]][b.second + vy[i]] == INF && c[b.first + vx[i]][b.second + vy[i]] != 'X'){
				y[b.first + vx[i]][b.second + vy[i]] = y[b.first][b.second] + 1;
				Q.push(P(b.first + vx[i], b.second + vy[i]));
			}
		}
	}
	return y[end.first][end.second];
}
int main(){
	int ok = 0;
	int ans = 0;
	std::cin >> h >> w >> n;
	for (int i = 0; i<h; i++){
		for (int j = 0; j<w; j++){
			std::cin >> b;
			if (b != 'S' && b != '.' && b != 'X'){
				for (int k = 0; k < 9; k++){
					if (b == num[k])ok = k;
				}
				p[ok].first = i;
				p[ok].second = j;
			}
			else if (b == 'S'){
				nest.first = i;
				nest.second = j;
			}
			c[i][j] = b;
		}
	}
	ans += bfs(nest, p[0]);
	for (int i = 1; i <= n - 1; i++){
		ans += bfs(p[i - 1], p[i]);
	}
	std::cout << ans << "\n";
}