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 <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define trep(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
#define H 1001
#define W 1001
int h, w;
int sx, sy;
int gx, gy;
char c[H][W];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n;
vector<vector<int>> d(H, vector<int>(W, INF));
int bfs(void)
{
    rep(i, h)
    {
        rep(j, w)
        {
            d[i][j] = INF;
        }
    }
    queue<P> q;
    q.push(P(sy, sx));
    d[sy][sx] = 0;
    while (q.size())
    {
        P p = q.front();
        q.pop();
        rep(i, 4)
        {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];
            if (0 <= ny && ny < h && 0 <= nx && nx < w && c[ny][nx] != 'X' && d[ny][nx] == INF)
            {
                q.push(P(ny, nx));
                d[ny][nx] = d[p.first][p.second] + 1;
            }
        }
    }
    return d[gy][gx];
}
int main()
{
    cin >> h >> w >> n;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c[i][j];
            if (c[i][j] == 'S')
            {
                sy = i;
                sx = j;
            }
        }
    }
    int ans = 0;
    trep(i, 1, n + 1)
    {
        rep(j, h)
        {
            rep(k, w)
            {
                if (c[j][k] == (i + '0'))
                {
                    gy = j;
                    gx = k;
                }
            }
        }
        ans += bfs();
        sy = gy;
        sx = gx;
    }
    cout << ans << endl;
    return 0;
}

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

typedef pair<int, int> ii;
int main()
{
	//freopen("1.txt", "r", stdin);
	int h, w, n;
	int a[10][2];
	memset(a, -1, sizeof(a));
	cin >> h >> w >> n;
	char g[h+10][w+10];
	for(int i = 0; i<h; i++){
		for(int j = 0; j<w; j++){
			cin >> g[i][j];
			if(g[i][j] == 'S'){
				a[0][0] = i;
				a[0][1] = j;
				g[i][j] = '.';
			}else if(isdigit(g[i][j])){
				a[g[i][j] - '0'][0] = i;
				a[g[i][j] - '0'][1] = j;
			}
		}
	}
	
	int way[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1} };
	int sum = 0;
	for(int i = 1; i<=n; i++){
		int x = a[i][0];
		int y = a[i][1];
		
		int d[h+10][w+10];
		int visited[h+10][w+10];
		
		memset(d, -1, sizeof(d));
		memset(visited, 0, sizeof(visited));
		
		d[a[i-1][0]][a[i-1][1]] = 0;
		visited[a[i-1][0]][a[i-1][1]] = 1;
		
		
		queue<ii> q;
		q.push(ii(a[i-1][0],a[i-1][1]));
		
		
		while(!q.empty()){
			ii data = q.front();
			q.pop();
			if(data.first == x && data.second == y){
				sum += d[x][y];
				while(!q.empty()){
					q.pop();
				}
				break;
			}
			
			
			for(int j = 0; j<4; j++){
				int x1 = data.first + way[j][0];
				int y1 = data.second + way[j][1]; 
				if(x1>=0 && x1<h && y1>=0 && y1<w && g[x1][y1] != 'X' && visited[x1][y1] == 0){
					ii node = ii(x1,y1);
					q.push(node);
					visited[x1][y1] = 1;
					d[x1][y1] = d[data.first][data.second] + 1;
				}
			}
		}
		
		
		
	}
	cout << sum << endl;
	return 0;
	
	
}

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;
}