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 <algorithm>
#include <queue>

#define sz(v) v.size()
#define pb(e) push_back(e)
#define mp(x, y) make_pair(x, y)
#define fst first
#define scd second
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAX = 1000;
const int INF = MAX * MAX + MAX;

int dx[] = { 0, 0, -1, 1 };
int dy[] = { -1, 1, 0, 0 };

int hyo[MAX][MAX];
pii pt[10];

bool bfs(int h, int w, int s, int e, int low)
{
	queue<pii> Q;
	pii end = pt[e];
	
	Q.push(pt[s]);
	
	while (!Q.empty())
	{
		pii t = Q.front(); Q.pop();
		for (int d = 0; d < 4; ++d)
		{
			int mx = t.X + dx[d];
			int my = t.Y + dy[d];
			if (0 > mx || mx >= w || 0 > my || my >= h || hyo[my][mx] == -1) continue;
			if (hyo[my][mx] <= low || hyo[my][mx] > hyo[t.Y][t.X] + 1)
			{
				hyo[my][mx] = hyo[t.Y][t.X] + 1;
				Q.push(mp(mx, my));
				if (end.X == mx && end.Y == my) return true;
			}
		}
	}
	return false;
}

int main ( void )
{
	int h, w, n;
	
	while (cin >> h >> w >> n)
	{
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				char c;
				
				cin >> c;
				if (c == '.') hyo[y][x] = INF;
				else if (c == 'X') hyo[y][x] = -1;
				else if (c == 'S') { hyo[y][x] = 0; pt[0] = mp(x, y); }
				else { hyo[y][x] = INF; pt[c-'0'] = mp(x, y); }
			}
		}
		
		for (int i = 0; i < n; ++i)
		{
			pii t = pt[i];
			bfs(h, w, i, i+1, hyo[t.Y][t.X]);
		}
		
		pii t = pt[n];
		cout << hyo[t.Y][t.X] << 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\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;
        }
        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 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<iostream>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
struct nagase {
		int x;
		int y;
		int c;
		int r;
};
int kani[4]={0,1,-1,0};
int ebi[4]={1,0,0,-1};
long long map[1001][1001]={0};
int s[1001][1001]={0};
int main(){
	queue<nagase>Q;
	nagase kotani;
	nagase yanagi;
	int h,w,n;
	cin>>h>>w>>n;
	char a;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>a;
			if(a=='S'){
				kotani.x=i;
				kotani.y=j;
				kotani.c=1;
				kotani.r=0;
				s[i][j]=1;
				Q.push(kotani);
			}
			else if(a=='X'){
				map[i][j]=-1;
			}
			else if(a=='.'){
				map[i][j]=0;
			}
			else{
				map[i][j]=(int)(a-48);
			}
		}
	}
	while(!Q.empty()){
		yanagi=Q.front();
		Q.pop();
		if(yanagi.c==map[yanagi.x][yanagi.y]){
			if(yanagi.c==n){
				cout<<yanagi.r<<endl;
				break;
			}
			while(!Q.empty()){
				Q.pop();
			}
			yanagi.c++;
			memset(s,0,sizeof(s));
			s[yanagi.x][yanagi.y]=1;
			Q.push(yanagi);
		}
		else{
			for(int i=0;i<4;i++){
				if(yanagi.x+kani[i]>=0&&yanagi.x+kani[i]<h&&yanagi.y+ebi[i]>=0&&yanagi.y+ebi[i]<w&&map[yanagi.x+kani[i]][yanagi.y+ebi[i]]!=-1&&s[yanagi.x+kani[i]][yanagi.y+ebi[i]]!=1){
					kotani.c=yanagi.c;
					kotani.x=yanagi.x+kani[i];
					kotani.y=yanagi.y+ebi[i];
					kotani.r++;
					s[yanagi.x+kani[i]][yanagi.y+ebi[i]]=1;
					Q.push(kotani);
				}
			}
		}
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;

#define REP(var, i, j) for (int var=(i); var<(j); var++)

typedef pair<int, int> P;

//定数
const int MAX_H = 1000;
const int MAX_W = 1000;
const int INF = MAX_H*MAX_W;
//入力
char factory[MAX_H+5][MAX_W+5];
P list[9];

int bfs(int x1, int y1, int x2, int y2){
    int res[MAX_H][MAX_W];
    REP(i, 0, MAX_H){
        REP(j, 0, MAX_W){
            res[i][j] = INF;
        }
    }
    res[x1][y1]  = 0;
    queue<P> que;
    que.push(P(x1, y1));
    while (que.size()){
        P temp = que.front();
        que.pop();
        if (temp.first == x2 && temp.second == y2) break;
        int nx, ny;
        int dx[4] = {1, 0, -1, 0};
        int dy[4] = {0, 1, 0, -1};

        REP(i, 0, 4){
            nx = temp.first + dx[i];  
            ny = temp.second + dy[i];
            if (factory[nx][ny] == 'X') continue;
            if (res[nx][ny] == INF){
                res[nx][ny] = res[temp.first][temp.second]+1;
                que.push(P(nx, ny));
            } 
        }
    }
    return res[x2][y2];
}

int solve(int x, int y, int N){
    int res = bfs(x, y, list[0].first, list[0].second);
    REP(i, 1, N){
        res += bfs(list[i-1].first, list[i-1].second, list[i].first, list[i].second); 
    }
    return res;
}

int main(){
    //入力
    int H, W, N;
    cin >> H;
    cin >> W;
    cin >> N;
    
    REP(i, 0, MAX_H+5){
        REP(j, 0, MAX_W+5){
            factory[i][j] = 'X';
        }
    }

    char number[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

    P S;
    REP(i, 1, H+1){
        REP(j, 1, W+1){
            cin >> factory[i][j];
            REP(k, 0, N){
                if (factory[i][j] == number[k]) list[k] = P(i, j);
            } 
            if (factory[i][j] == 'S') S = P(i, j);
        }
    }
    int res;
    res = solve(S.first, S.second, N);

    cout << res << endl;

    return 0;
}