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 <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define X first
#define Y second
#define P first
#define C second

using namespace std; 

typedef pair<int, int> pii;
typedef pair<pii, int> mouse;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main(void){
    int w, h, n, res = 0; cin >> h >> w >> n;
    vs m(h);
    rep(y, h) cin >> m[y];
    pii s;
    rep(y, h) rep(x, w) if(m[y][x] == 'S') s = mp(x, y);

    rep(i, n){
        queue<mouse> q;
        q.push(mp(s, 0));

        vector< vector<bool> > used(h);
        rep(y, h) used[y].resize(w);

        while(!q.empty()){
            mouse cur = q.front();
            q.pop();

            if(used[cur.P.Y][cur.P.X]) continue;
            used[cur.P.Y][cur.P.X] = true;

            if(m[cur.P.Y][cur.P.X] == i + 1 + '0'){
                res += cur.C;
                s = mp(cur.P.X, cur.P.Y);
                break;
            }

            rep(i, 4){
                mouse next = mp(mp(cur.P.X + dx[i], cur.P.Y + dy[i]), cur.C + 1);
                if(0 <= next.P.X && next.P.X < w &&
                   0 <= next.P.Y && next.P.Y < h &&
                   m[next.P.Y][next.P.X] != 'X') q.push(next);
            }
        }
    }

    cout << res << endl;

    return 0;
}

C++: #include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define MAXN 1000
struct node{
    int x,y,st;
    bool operator == (node B) const{
        return x==B.x&&y==B.y;
    }
};
int H,W,N;
char Map[MAXN+5][MAXN+5];
bool vis[MAXN+5][MAXN+5];

const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int bfs(node S,node T){
    queue<node> Q;
    Q.push(S);
    memset(vis,0,sizeof vis);
    while(!Q.empty()){
        node u=Q.front();Q.pop();
        for(int i=0;i<4;i++){
            node tmp=u;
            tmp.x+=dir[i][0],tmp.y+=dir[i][1],tmp.st++;
            if(tmp.x<1||tmp.x>H||tmp.y<1||tmp.y>W||vis[tmp.x][tmp.y]||Map[tmp.x][tmp.y]=='X')
                continue;
            if(tmp==T)
                return tmp.st;
            vis[tmp.x][tmp.y]=1;
            Q.push(tmp);
        }
    }
    return -1;
}

pair<int,int> pos[10];

int main(){
    scanf("%d%d%d",&H,&W,&N);
    for(int i=1;i<=H;i++)
        scanf("%s",Map[i]+1);
    node S;
    for(int i=1;i<=H;i++)
        for(int j=1;j<=W;j++){
            if(Map[i][j]!='.'&&Map[i][j]!='X'&&Map[i][j]!='S')
                pos[Map[i][j]-'1'+1]=make_pair(i,j);
            if(Map[i][j]=='S')
                S.x=i,S.y=j,S.st=0;
        }
    int Ans=0;
    for(int i=1;i<=N;i++){
        Ans+=bfs(S,(node){pos[i].first,pos[i].second,0});
        S=(node){pos[i].first,pos[i].second,0};
    }
    printf("%d\n",Ans);
}

C++: #include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <stack>
using namespace std;

int h, w, n;
int map[1000][1000];
int map_t[1000][1000];
int step;
vector<pair<int, int>> point;


bool SearchAndCollect(int goal)
{
	vector<pair<int, int>> point_t;
	for (int i = 0; i < point.size(); i++)
	{
		if (point[i].first > 0)
		{
			if (map_t[point[i].first - 1][point[i].second] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first - 1][point[i].second] != -1)
			{
				map_t[point[i].first - 1][point[i].second] = -1;
				pair<int, int> temp;
				temp.first = point[i].first - 1;
				temp.second = point[i].second;
				point_t.push_back(temp);
			}
		}

		if (point[i].first < h - 1)
		{
			if (map_t[point[i].first + 1][point[i].second] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first + 1][point[i].second] != -1)
			{
				map_t[point[i].first + 1][point[i].second] = -1;
				pair<int, int> temp;
				temp.first = point[i].first + 1;
				temp.second = point[i].second;
				point_t.push_back(temp);
			}
		}

		if (point[i].second > 0)
		{
			if (map_t[point[i].first][point[i].second - 1] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first][point[i].second - 1] != -1)
			{
				map_t[point[i].first][point[i].second - 1] = -1;
				pair<int, int> temp;
				temp.first = point[i].first;
				temp.second = point[i].second - 1;
				point_t.push_back(temp);
			}
		}

		if (point[i].second < w - 1)
		{
			if (map_t[point[i].first][point[i].second + 1] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first][point[i].second + 1] != -1)
			{
				map_t[point[i].first][point[i].second + 1] = -1;
				pair<int, int> temp;
				temp.first = point[i].first;
				temp.second = point[i].second + 1;
				point_t.push_back(temp);
			}
		}
	}
	point.clear();
	for (int i = 0; i < point_t.size(); i++)
	{
		if (point.size() <= i)
		{
			point.resize(point.size() + 1);
		}
		point[i] = point_t[i];
	}
	step++;
	return false;
}

int TimeFrom(int start)
{
	step = 1;
	int goal = start + 1;
	if (start == 10)
	{
		goal = 1;
	}

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			map_t[i][j] = map[i][j];
		}
	}

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			if (map[i][j] == start)
			{
				pair<int, int> temp;
				temp.first = i;
				temp.second = j;
				point.clear();
				point.push_back(temp);
				while (SearchAndCollect(goal) == false)
				{
				}
				return step;
			}
		}
	}
}

int main()
{
	string line;
	while (cin >> h >> w >> n)
	{
		for (int i = 0; i < h; i++)
		{
			cin >> line;
			for (int j = 0; j < w; j++)
			{
				switch (line[j])
				{
				case 'S':
					map[i][j] = 10;
					break;

				case '.':
					map[i][j] = 0;
					break;

				case 'X':
					map[i][j] = -1;
					break;

				default:
					map[i][j] = line[j] - '0';
					break;
				}
			}
		}

		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			if (i == 0)
			{
				cnt = cnt + TimeFrom(10);
			}
			else
			{
				cnt = cnt + TimeFrom(i);
			}
		}

		cout << cnt << endl;
	}


	return 0;
}