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 <stdio.h>

bool syo[1000][1000];
int zi[1000][1000];
int kox[1000000];
int koy[1000000];
int kon;
int koxs[1000000];
int koys[1000000];
int kons;
int cx[10];
int cy[10];
int sx,sy;

int main()
{
	int h,w,n;
	scanf("%d %d %d",&h,&w,&n);
	for(int i = 0; i < h; i++)
	{
		char ww[1111];
		scanf("%s",ww);
		for(int ii = 0; ii < w; ii++)
		{
			if('1' <= ww[ii] && ww[ii] <= '9')
			{
				int d = ww[ii] - '1';
				cy[d] = i;
				cx[d] = ii;
			}
			if(ww[ii] == 'S')
			{
				sy = i;
				sx = ii;
			}
			if(ww[ii] == 'X')
			{
				syo[i][ii] = false;
			}
			else
			{
				syo[i][ii] = true;
			}
		}
	}
	int time = 0;
	int nx = sx;
	int ny = sy;
	for(int k = 0; k < n; k++)
	{
		for(int i = 0; i < h; i++)
		{
			for(int ii = 0; ii < w; ii++)
			{
				zi[i][ii] = 9999;
			}
		}
		zi[ny][nx] = 0;
		kox[0] = nx;
		koy[0] = ny;
		kon = 1;
		while(1)
		{
			kons = 0;
			for(int i = 0; i < kon; i++)
			{
				if(kox[i] == cx[k] && koy[i] == cy[k])
				{
					goto aa;
				}
				if(kox[i] > 0)
				{
					if(zi[koy[i]][kox[i] - 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] - 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] - 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(kox[i] < w - 1)
				{
					if(zi[koy[i]][kox[i] + 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] + 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] + 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(koy[i] > 0)
				{
					if(zi[koy[i] - 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] - 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] - 1;
						kons++;
					}
				}
				if(koy[i] < h - 1)
				{
					if(zi[koy[i] + 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] + 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] + 1;
						kons++;
					}
				}
			}
			for(int i = 0; i < kons; i++)
			{
				kox[i] = koxs[i];
				koy[i] = koys[i];
			}
			kon = kons;
		}
aa:
		time += zi[cy[k]][cx[k]];
		nx = cx[k];
		ny = cy[k];
	}
	printf("%d\n",time);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int h, w, n;
struct xydata {
    int x; int y;
};
struct xyldata {
    int xx; int yy; int dist;
};

vector< vector<char> > cmap(1010, vector<char>(1010, '.'));

int bfs(vector< vector<bool> > &xmap, xydata first, xydata goal) {
    int len = 0;
    queue<xyldata> que;
    xyldata lfirst; lfirst.xx = first.x; lfirst.yy = first.y;
    lfirst.dist = 0;
    que.push(lfirst);
    xmap.at(first.y).at(first.x) = true;

    while(!que.empty()) {
        xyldata now = que.front(); que.pop();
        if(now.xx == goal.x && now.yy == goal.y) {
            len = now.dist;
            break;
        }
        for(int i = 0; i < 4; ++i) {
            int nx = now.xx+dx[i]; int ny = now.yy+dy[i];
            int ndist = now.dist+1;
            if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                if(cmap.at(ny).at(nx) != 'X') {
                    if(!xmap.at(ny).at(nx)) {
                        xmap.at(ny).at(nx) = true;
                        xyldata next;
                        next.xx = nx; next.yy = ny; next.dist = ndist;
                        que.push(next);
                    }
                }
            }
        }
    }
    return len;
}

int main() {
    cin >> h >> w >> n;
    vector<xydata> chese(10);
    xydata start;
    for(int i = 0; i < h; ++i) {
        for(int j = 0; j < w; ++j) {
            cin >> cmap.at(i).at(j);
            xydata indata;
            if(isdigit(cmap.at(i).at(j))) {
                int no = cmap.at(i).at(j) - '0';
                no--;
                indata.x = j; indata.y = i; chese.at(no) = indata;
            }else if(cmap.at(i).at(j) == 'S') {
                start.x = j; start.y = i;
            }
        }
    }

    ll sum = 0;
    for(int i = 0; i < n; ++i) {
        vector< vector<bool> > cvisit(h, vector<bool>(w, false));
        if(i == 0) sum += bfs(cvisit, start, chese.at(i));
        else sum += bfs(cvisit, chese.at(i-1), chese.at(i));
    }
    cout << sum << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;

typedef struct{
	int x,y;
}p;
char a[1005][1005];
p acm[11];
p s,g;
int b[1005][1005];
int H,W,N;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int bfs(p s,p g)
{
	queue<p> q;
	p n;
	memset(b,-1,sizeof(b));
	q.push(s);
	b[s.x][s.y]=0;
	while(!q.empty())
	{
		p h;
		h=q.front();
		q.pop();
		if(h.x==g.x&&h.y==g.y)
		{
			break;
		}
		for(int i=0;i<4;i++)
		{
			n.x=h.x+dx[i];
			n.y=h.y+dy[i];
			if(n.x>=0&&n.x<H&&n.y>=0&&n.y<W&&a[n.x][n.y]!='X'&&b[n.x][n.y]==-1)
			{
				q.push(n);
				b[n.x][n.y]=b[h.x][h.y]+1;
			}
		}
	}
	return b[g.x][g.y];
}
int main()
{
	int i,j,t,step=0;
	scanf("%d%d%d",&H,&W,&N);
	for(i=0;i<H;i++)
	{
		scanf("%s",a[i]);
	}
	for(i=0;i<H;i++)
	for(j=0;j<W;j++)
	{
		if(a[i][j]>='1'&&a[i][j]<='9')
		{
			t=a[i][j]-'0';
		    acm[t].x=i;
		    acm[t].y=j;
		}
		else if(a[i][j]=='S')
		{
			s.x=i;
			s.y=j;
			a[i][j]='.';
		}
	}
		for(i=1;i<=N;i++)
		{
			g.x=acm[i].x;
			g.y=acm[i].y;
			step+=bfs(s,g);
			s.x=g.x;
			s.y=g.y;
			a[g.x][g.y]='.';
		}
	
	printf("%d\n",step);
	return 0;
 }