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 <queue>

#define H_MAX 1000
#define W_MAX 1000
#define N_MAX 9
#define NEST 'S'
#define BLANK '.'
#define BLOCK 'X'
#define START '1'
#define INF -1

using namespace std;

typedef pair<int, int> P;

char map[H_MAX][W_MAX];
int root[H_MAX][W_MAX];
int w, h, n, sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*void show_root(void) {
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) printf("%2d,", root[i][j]);
		printf("\n");
	}
	printf("\n");
}*/

int cheese(char goal) {
	//printf("%c\n", goal);
	queue<P> que;
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			root[i][j] = INF;
		}
	}
	que.push(P(sx, sy));
	root[sx][sy] = 0;
	
	while( que.size() ) {
		P p = que.front(); que.pop();
		if(map[p.first][p.second] == goal) {
			sx = p.first;
			sy = p.second;
			//printf("%d, %d\n", sx, sy);
			break;
		}
		for(int i = 0; i < 4; i++) {
			int nx = p.first + dx[i], ny = p.second + dy[i];
			//printf("nx = %d, ny = %d\n", nx, ny);
			if(0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] != BLOCK && root[nx][ny] == INF) {
				que.push(P(nx, ny));
				root[nx][ny] = root[p.first][p.second] + 1;
				//show_root();
			}
		}
	}
	return root[sx][sy];
}

int main(void) {
	scanf("%d %d %d", &h, &w, &n);
	for(int i = 0; i < h; i++) {
		scanf("%s", &map[i]);
		for(int j = 0; j < w; j++) {
			if(map[i][j] == NEST) sx = i, sy = j;
		}
	}
	
	int time = 0;
	for(int i = 0; i < n; i++) {
		time += cheese(START + (char)i);
		//show_root();
	}
    
	printf("%d\n", time);
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <queue>
 
using namespace std;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

typedef pair<int, int> P;

char maze[1001][1001];
int dis[1001][1001];

int main(void){
    int H, W, N;
    int sx, sy;
    cin >> H >> W >> N;
    rep(i, H) {
        rep(j, W) {
            cin >> maze[i][j];
            if(maze[i][j]=='S') sy = i, sx = j;
        }
    }
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};

    int ans = 0;

    const int INF = 99999999;

    rep(HP, N+1) {
        queue<P> que;
        rep(i, H) rep(j, W) dis[i][j] = INF;
        que.push(P(sx, sy));
        dis[sy][sx] = 0;

        while(que.size()) {
            P p = que.front(); que.pop();
            if(maze[p.second][p.first] == HP+'0') {
                sx = p.first;
                sy = p.second;
                ans += dis[sy][sx];
                break;
            }
            rep(i, 4) {
                int nx = p.first + dx[i];
                int ny = p.second + dy[i];
                if(nx >= 0 && nx < W && ny >= 0 && ny < H 
                   && maze[ny][nx] != 'X' && dis[ny][nx]==INF) {
                    que.push(P(nx, ny));
                    dis[ny][nx] = dis[p.second][p.first] + 1;
                }
            }
        }
    }
    cout << ans << endl;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 100000000
using namespace std;

//firstにh、secondにwを入れる
typedef pair<int, int> PII;

//S、1...Nまでの座標を格納する
PII cheese[10];

const int MAX_H = 1010, MAX_W = 1010;
int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];

int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

bool infield(int h, int w){
    return (0<=h && h<H && 0<=w && w<W);
}

//sからgまでの最短手数を返す
int bfs(PII s, PII g){
    queue<PII> que;
    //init
    /*
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++)
            d[i][j] = INF;
    }
    */
    fill(d, d+sizeof(d), INF);
    que.push(s);
    d[s.first][s.second] = 0;
    
    while(!que.empty()){
        PII p = que.front(); que.pop();
        if(p == g)
            break;
        for(int i=0; i<4; i++){
            int nh = p.first+dh[i];
            int nw = p.second+dw[i];
            
            if(infield(nh, nw) && field[nh][nw]!='X' && d[nh][nw]==INF){
                que.push(PII(nh, nw));
                d[nh][nw] = d[p.first][p.second] + 1;
            }
        }
    }
    
    return d[g.first][g.second];
}

void solve(){
    int ans = 0;
    
    //cheeseリストを作る
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(field[i][j] == 'S')
                cheese[0] = PII(i, j);
            
            for(char c='1'; c<='9'; c++){
                if(field[i][j] == c)
                    cheese[c-'0'] = PII(i, j);
            }
        }
    }
    for(int i=0; i<N; i++)
        ans += bfs(cheese[i], cheese[i+1]);
    printf("%d\n", ans);
}

int main(){
    scanf("%d%d%d", &H, &W, &N);
    for(int i=0; i<H; i++)
        scanf("%s", field[i]);
    solve();
    return 0;
}

C++: #include<iostream>
#include<set> 
#include<vector>
#include<stack>
#include<string>
#include<ctype.h>
#include<sstream> 
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<cstring> 
#include<queue>
using namespace std; 
typedef struct{
	int x;
	int y;
}P;//坐标 

char graph[1024][1024];
P aim[10];
P s, g; // s起点，g终点 
int N,W,H;
int vis[1000][1000];//存储距离 
 int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
 
int bfs(P s, P g);
int main()
{	
	int i, j ,t;
	int step = 0;
	cin>>H>>W>>N;
	for(i = 0; i < H; i++){
		for(j = 0; j < W; j++){
			cin>>graph[i][j];
			if(graph[i][j] <= '9' && graph[i][j] >= '1'){
				t = graph[i][j] - '0';
				aim[t].x = i;
				aim[t].y = j;
			}
			else if(graph[i][j] == 'S'){
				s.x = i;
				s.y = j;
				graph[i][j] = '.';	
			}
		}
	}
	for(i = 1; i <= N; i++){
		g.x = aim[i].x;
		g.y = aim[i].y;
		step += bfs(s, g);
		s.x = g.x;
		s.y = g.y;
		graph[i][j] = '.';
	}
	cout<<step<<endl;
	return 0;
 } 
 
int bfs(P s, P g)
{
	queue<P> que;
	P n;
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			vis[i][j] = -1;
		}
	}
	que.push(s);
	vis[s.x][s.y] = 0;
	while(que.size()){
		P p = que.front();
		que.pop();
		if(p.x == g.x && p.y == g.y) break;
		for(int i = 0; i < 4; i++){
			n.x = p.x + dx[i];
			n.y = p.y + dy[i];
			if(0 <= n.x && n.x < H && n.y < W && n.y >= 0 && graph[n.x][n.y] != 'X' && vis[n.x][n.y] == -1){
				que.push(n);
				vis[n.x][n.y] == vis[p.x][p.y] + 1;
				
			}
		}
	}
	return vis[g.x][g.y];
 
}