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<stdio.h>
#include<queue>
using namespace std;

typedef pair<int, int> pi;
pair <int, int>p;
pair <int, int> po;
int n, m, c;
char ma[1001][1001];
int x, y;
int te[1001][1001];
int xw[4] = { 1, 0, -1, 0 };
int yw[4] = { 0, 1, 0, -1 };
char name[10] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int nat = 0;
int na;
int op = 10000;
int main()
{
	queue<pi> q;
	cin >> n >> m >> na;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 < m; i2++){
			cin >> ma[i][i2];
			te[i][i2] = op;
			if (ma[i][i2] == 'S'){
				p.first = i;
				p.second = i2;
			}
		}
	}

	int count;

	count = 0;
	q.push(p);

	te[p.first][p.second] = 0;
	while (!q.empty()){
		p = q.front();
		po = p;
		q.pop();

		for (int i = 0; i < 4; i++){
			x = p.first + xw[i];
			y = p.second + yw[i];
			if (0 <= x && x < n && 0 <= y && y < m && ma[x][y] != 'X' && te[x][y] == op){

				if (ma[x][y] == name[nat]){
					po.first = x;
					po.second = y;
					while (!q.empty()){ q.pop(); }
					q.push(po);
					count += te[p.first][p.second] + 1;
					nat++;
					if (nat == na){ cout << count << endl; return 0; }
					for (int i2 = 0; i2 <= n; i2++){
						for (int i3 = 0; i3 <= m; i3++){
							te[i2][i3] = op;
						}
					}
					te[x][y] = 0;
					break;
				}
				else{
					po.first = x;
					po.second = y;
					q.push(po);
					po = p;
					te[x][y] = te[p.first][p.second] + 1;
				}
			}
		}
	}
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAX_N   1000+10

using namespace std;
const int INF=1000000;
typedef pair<int,int> P;
int H,W,N,d[MAX_N][MAX_N];
char Map[MAX_N][MAX_N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int sx,sy,gx,gy,power,ans;

int bfs(char c)
{
    queue<P> que;
    for(int i=0;i<H;i++)
        for(int j=0;j<W;j++)
            d[i][j]=INF;
    que.push(P(sx,sy));
    d[sx][sy]=0;
    P p;
    while(que.size())
    {
        p=que.front();
        que.pop();
        if(Map[p.first][p.second]==c)
        {
            sx=p.first;
            sy=p.second;
            break;
        }
        for(int i=0;i<4;i++)
        {
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
            if(nx>=0&&nx<H&&ny>=0&&ny<W&&Map[nx][ny]!='X'&&d[nx][ny]==INF)
            {
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
    }
    return d[sx][sy];
}
int main()
{
    while(cin>>H>>W>>N)
    {
        ans=0;
        for(int i=0;i<H;i++)
        {
            for(int j=0;j<W;j++)
            {
                cin>>Map[i][j];
                if(Map[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                    Map[i][j]='.';
                }
            }
        }
        for(int i=1;i<=N;i++)
        {
            ans+=bfs(i+48);
        }
        cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <utility>
#include <queue>
#include <vector>

#define MAX 1000
#define MAXCHEESE 10

char map[MAX][MAX];
int d[MAX][MAX];
int W,H,N;
typedef std::pair<int, int> P;
std::vector<P> cheese(MAXCHEESE); // 0番目はスタート
int sx, sy;
int gx, gy;
int dx[4] = {0,-1,1,0};
int dy[4] = {-1,0,0,1};

int bfs(int goal){
    sx = cheese[goal-1].first;
    sy = cheese[goal-1].second;
    gx = cheese[goal].first;
    gy = cheese[goal].second;
    std::queue<P> que;
    for(int j=0; j<H; j++){
        for(int i=0; i<W; i++){
            d[i][j] = -1;
        }
    }
    que.push(P(sx,sy));
    d[sx][sy] = 0;

    while(que.size() != 0){
        P p = que.front();
        que.pop();

        for(int i = 0; i<4; i++){
            int nx = p.first + dx[i];
            int ny = p.second + dy[i];
            if(0 <= nx && nx < W && 0 <= ny && ny <= H && map[nx][ny] != 'X' && d[nx][ny] == -1){
                que.push(P(nx,ny));
                d[nx][ny] = d[p.first][p.second] + 1;
                if(nx == gx && ny == gy) return d[nx][ny];
            }
        }
    }
    return -1;
}

int main(){
    std::cin >> H >> W >> N;

    std::string tmp;
    for(int j=0; j<H; j++){
        std::cin >> tmp;
        for(int i=0; i<W; i++){
            map[i][j] = tmp[i];
            if (map[i][j] == 'S') cheese[0] = P(i,j);
            if ('0' <= map[i][j] && map[i][j] <= '9'){
                int x = map[i][j] - '0';
                cheese[x] = P(i,j);
            }
        }
    }

    int ans = 0;

    for(int i = 1; i<=N; i++){
        if(bfs(i) == -1){
            ans = -1;
            break;
        }
        ans += bfs(i);
    }

    std::cout << ans << "\n";

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define MAX_N 1002

const int INF = 114514810;
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int h, w, n;
char f[MAX_N][MAX_N];
int d[MAX_N][MAX_N] ;
vector<P> cheese(12);

bool ng(int x, int y) {
    return x<0 or x>=w or y<0 or y>=h;
}

int bfs(P start, P goal) {
    queue<P> q;
    rep(y,h) rep(x,w) d[y][x] = INF;

    q.push(start);
    d[start.second][start.first] = 0;

    while(!q.empty()) {
        P p = q.front(); q.pop();
        if(p.first == goal.first and p.second == goal.second) break;
        rep(i,4) {
            int nx = p.first+dx[i], ny = p.second+dy[i];
            if(!ng(nx,ny) and f[ny][nx] != 'X' and d[ny][nx] >= INF) {
                q.push(P(nx,ny));
                d[ny][nx] = d[p.second][p.first] + 1;
            }
        }
    }

    return d[goal.second][goal.first];
}

int main() {
    cin >> h >> w >> n;
    rep(y,h) rep(x,w) cin >> f[y][x];

    rep(y,h) rep(x,w) {
        char c = f[y][x];
        if(c == 'S') {
            cheese[0] = make_pair(x, y);
        } else if(c != '.' and c != 'X') {
            cheese[c - '0'] = make_pair(x, y);
        }
    }

    int ans = 0;
    rep(i,n) ans += bfs(cheese[i], cheese[i+1]);
    
    cout << ans << endl;

    return 0;
}