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<queue>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
 
using namespace std;
#define loop(i,n) for(int i = 0; i < (n); i++)
#define loops(i,f,n) for(int i = (f); i < (n); i++)
#define VEC vector<int>
#define INF 9999999
#define inta long long
typedef pair<int, int>pii;
typedef pair<int, bool>pib;
// vector<VEC> v (n, VEC(n))
//loop(i,n)loop(j,n)dp[i][j]=-1;
//printf("%d\n", n);
struct point
{
	int x, y;
};
int h, w, n;
char map[1005][1005];
int d[1005][1005];
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int BFS(int sx, int sy, int gx, int gy)
{
	queue<pii> que;
	loop(i, h)loop(j, w)d[i][j] = INF;
	que.push(pii(sx, sy));
	d[sx][sy] = 0;
 
	while (que.size())
	{
		pii p = que.front(); que.pop();
		if (p.first == gx&&p.second == gy)break;
		loop(i, 4)
		{
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (0 <= nx&&nx <= w && 0 <= ny&&ny <= h&&map[nx][ny] != 'X'&&d[nx][ny] == INF)
			{
				que.push(pii(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
	return d[gx][gy];
}
int main()
{
	cin.tie(0);
	cin.sync_with_stdio(0);
	cin >> h >> w >> n;
	vector<point> points(n + 1);
	loop(i, h)
	{
		loop(j, w)
		{
			cin >> map[i][j];
			if (map[i][j] == 'S')points[0].x = i, points[0].y = j;
			if (map[i][j] == '1')points[1].x = i, points[1].y = j;
			if (map[i][j] == '2')points[2].x = i, points[2].y = j;
			if (map[i][j] == '3')points[3].x = i, points[3].y = j;
			if (map[i][j] == '4')points[4].x = i, points[4].y = j;
			if (map[i][j] == '5')points[5].x = i, points[5].y = j;
			if (map[i][j] == '6')points[6].x = i, points[6].y = j;
			if (map[i][j] == '7')points[7].x = i, points[7].y = j;
			if (map[i][j] == '8')points[8].x = i, points[8].y = j;
			if (map[i][j] == '9')points[9].x = i, points[9].y = j;
		}
	}
	int ans = 0;
	loop(i, n)
	{
		ans += BFS(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y);
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <limits.h>

using namespace std;

int d[1000][1000];

int search(string* field, int w, int h, int sx, int sy, int n)
{
    static const int dx[] = {-1, 0,  0, 1};
    static const int dy[] = { 0, 1, -1, 0};
    int ans = 0;

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < h; j++) {
            for (int k = 0; k < w; k++) {
                d[j][k] = INT_MAX;
            }
        }

        d[sy][sx] = 0;
        queue<pair<int, int> > q;
        q.push(pair<int, int>(sx, sy));
        while (!q.empty()) {
            pair<int, int> coord;
            coord = q.front();
            q.pop();

            if (i == field[coord.second][coord.first] - '0') {
                ans += d[coord.second][coord.first];
                sx = coord.first;
                sy = coord.second;
                break;
            }

            for (int j = 0; j < 4; j++) {
                int nx = coord.first + dx[j];
                int ny = coord.second + dy[j];

                if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
                        field[ny][nx] != 'X' && (d[coord.second][coord.first] + 1) < d[ny][nx]) {
                    d[ny][nx] = d[coord.second][coord.first] + 1;
                    q.push(pair<int, int>(nx, ny));
                }
            }
        }
    }
    return ans;
}

int main(int argc, char **argv)
{
    int w, h, n;
    string field[1000];

    cin >> h >> w >> n;
    for (int i = 0; i < h; i++) {
        cin >> field [i];
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j] == 'S') {
                cout << search(field, w, h, j, i, n) << endl;
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
using namespace std;

int H,W,N;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char MAP[1000+1][1000+1];
int f[1000+1][1000+1];
int d[1000+1][1000+1];
int main(){
	cin >> H >> W >> N;
	vector<int> ans(N+1);
	int startx,starty;
	for(int i=1; i<H+1; ++i){
		for(int j=1; j<W+1; ++j){
			cin >> MAP[i][j];
			f[i][j]=0;
			if(MAP[i][j]=='S'){
				startx=i,starty=j;
				//qux.push(i);
				//quy.push(j);
				f[i][j]=1;
				d[i][j]=0;
			}
		}
	}
	//cout << qux.size() << " " << quy.size() << "\n";
	for(int i=1; i<N+1; ++i){
		int x;
		int y;
		queue<int> qux;
		queue<int> quy;
		qux.push(startx);
		quy.push(starty);
		while(1){
			if(!(qux.size()&&quy.size())){
				cout << x << " " << y << "\n";
				break;
			}
			x=qux.front();
			y=quy.front();
			//cout << x << " " << y << " " << d[x][y] << "\n";
			qux.pop();
			quy.pop();
				if(atoi(&MAP[x][y])==i){
					ans[i]=d[x][y];
					//cout << i << " " <<  d[x][y] << "\n";
					for(int j=1; j<H+1; ++j){
						for(int k=1; k<W+1; ++k){
							f[j][k]=0;
						}
					}
					startx=x;
					starty=y;
					d[x][y]=0;
					break;
				}
			for(int j=0; j<4; ++j){
				int nx=x+dx[j];
				int ny=y+dy[j];
				if(1<=nx&&nx<=H&&1<=ny&&ny<=W&&MAP[nx][ny]!='X'&&f[nx][ny]==0){
					//cout << "ok\n";
					qux.push(nx);
					quy.push(ny);
					f[nx][ny]=1;
					d[nx][ny]=d[x][y]+1;
				}
			}
		}
	}
	int ANS=0;
	for(int i=1; i<N+1; ++i){
		ANS+=ans[i];
	}
	cout << ANS << "\n";
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
int map[1002][1002]={0},r[1002][1002]={0};
struct A{
int t,h,x,y;
};
int main(){
int h,w,n,i,j,m=0,g[2],mark=0,b;
g[0]=1;
g[1]=-1;
char s[1001];
A a;
queue<A> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
map[i][0]=-1;
map[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
map[0][i]=-1;
map[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",s);
	for(j=0;j<=w;j++){
		b=s[j];
		if(b>48&&b<58)
			map[i][j+1]=b-48;
		else if(s[j]=='S'){
			a.x=i;
			a.y=j+1;
			a.t=0;
			a.h=1;
			q.push(a);
		}
		else if(s[j]=='X')
			map[i][j+1]=-1;
	}
}
while(!q.empty()){
	a=q.front();
	q.pop();
	if(map[a.x][a.y]==a.h){
		a.h++;
		mark=0;
		while(!q.empty()){
			q.pop();
		}
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				r[i][j]=0;
			}
		}
	}
	mark++;
	r[a.x][a.y]=mark;
	if(a.h==n+1){
		if(m==0||m>a.t){
			m=a.t;
			break;
		}
	}
	a.t++;
	for(i=0;i<2;i++){
		if(map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]>mark||map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]==0){
			a.x+=g[i];
			q.push(a);
			a.x-=g[i];
		}
		if(map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]>mark||map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]==0){
			a.y+=g[i];
			q.push(a);
			a.y-=g[i];
		}
	}
}
cout<<m<<endl;
return 0;
}