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++: #define _CRT_SECURE_NO_WARNINGS

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<list>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>

using namespace std;

int h, w, n;
vector<pair<int, int>> map;
vector<pair<int, int>> List;

queue<int> q;

int loop(int start, int end) {
	for (int i = 0; i < h * w; i++) {
		map[i].second = 0;
	}
	while (!q.empty()) {
		q.pop();
	}
	queue<int> tmp;
	q.push(start);
	for (int i = 1; true; i++) {
		while (!q.empty()) {
			int front = q.front();
			q.pop();
			map[front].second = -1;

			if (front / w != 0) {
				if (map[front - w].first != 'X' && map[front - w].second != -1) {
					if (front - w == end) {
						return i;
					}
					map[front - w].second = -1;
					tmp.push(front - w);
				}
			}

			if (front / w != h - 1) {
				if (map[front + w].first != 'X' && map[front + w].second != -1) {
					if (front + w == end) {
						return i;
					}
					map[front + w].second = -1;
					tmp.push(front + w);
				}
			}

			if (front % w != 0) {
				if (map[front - 1].first != 'X' && map[front - 1].second != -1) {
					if (front - 1 == end) {
						return i;
					}
					map[front - 1].second = -1;
					tmp.push(front - 1);
				}
			}

			if (front % w != w - 1) {
				if (map[front + 1].first != 'X' && map[front + 1].second != -1) {
					if (front + 1 == end) {
						return i;
					}
					map[front + 1].second = -1;
					tmp.push(front + 1);
				}
			}
		}
		swap(q, tmp);
	}
}

void set_map() {
	pair<int, int> map_p;
	pair<int, int> list_p;
	for (int i = 0; i < h; i++) {
		string tmp;
		cin >> tmp; // map状況
		for (int j = 0; j < w; j++) {
			map_p.first = tmp[j]; // map状況
			map_p.second = 0; //queのための値
			map.push_back(map_p);
			if (map_p.first != 'X' && map_p.first != '.') {
				list_p.first = i * w + j; // map上の何番目なのか
				if (map_p.first == 'S') { // 何番目のゴールなのか
					list_p.second = 0;
				} else {
					list_p.second = map_p.first - '0';
				}
				vector<pair<int, int>>::iterator ite = List.begin();
				int k = 0;
				for (; k < List.size(); ite = std::next(ite, 1), k++) {
					if (list_p.second < List[k].second) {
						List.insert(ite, list_p);
						break;
					}
				}
				if (k == List.size()) {
					List.push_back(list_p);
				}
			}
		}
	}
}

int main(int argc, string *argv){

	cin >> h >> w >> n;
	set_map();
	int count = 0;
	for (int i = 1; i < List.size(); i++) {
		count += loop(List[i - 1].first, List[i].first);
	}
	cout << count << endl;
	return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int A[1002][1002],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
				A[S.y][S.x]=0;
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			A[S.y][S.x]=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();

		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
				A[S.y][S.x]=1;
		if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			for(i=0;i<h;i++){
		for(j=0;j<w;j++){
				A[S.y][S.x]=0;
		}
		}
			l=-1;
		

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

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

using namespace std;

int H, W, N;
char map[1024][1024];
bool visited[1024][1024];
int factories[10];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void solve()
{
    int result = 0;
    for (int i = 0; i < N; ++i)
    {
        queue<pair<int, int> > loc_time;
        memset(visited, false, sizeof visited);
        int fx = factories[i + 1] % W;
        int fy = factories[i + 1] / W;
        loc_time.push(make_pair(factories[i], 0));
        
        while (!loc_time.empty())
        {
            int loc = loc_time.front().first;
            int time = loc_time.front().second;
            loc_time.pop();
            int x = loc % W;
            int y = loc / W;
            visited[y][x] = true;
            if (y == fy && x == fx)
            {
                result += time;
                break;
            }
            for (int i = 0; i < 4; ++i)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (0 <= nx && nx < W && 0 <= ny && ny < H &&
                    map[ny][nx] != 'X' && !visited[ny][nx])
                {
                    loc_time.push(make_pair(ny * W + nx, time + 1));        
                }
            }
        }
    }
    cout << result << endl;
}

int main()
{
    cin >> H >> W >> N;
    for (int h = 0; h < H; ++h)
    {
        for (int w = 0; w < W; ++w)
        {
            cin >> map[h][w];
            if (isdigit(map[h][w]))
            {
                factories[map[h][w] - '0'] = h * W + w;
            }
            if (map[h][w] == 'S')
            {
                factories[0] = h * W + w;
            }
        }
    }
	solve();
    return 0;
}

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

typedef struct{
 int x;
 int y;
}P;//表示坐?

char graph[1000][1000];
P aim[10];
P s,g;//s起点，g?点
int N,W,H;
int vis[1000][1000]; //用来存?距?
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int bfs(P s,P g);

int main(){
    int i,j,t,step=0;
    cin>>H>>W>>N;
    
    for(i=0;i<H;i++)
    for(j=0;j<W;j++){
        cin>>graph[i][j];
      if(graph[i][j]<='9'&&graph[i][j]>='1'){ 
         t=graph[i][j]-'0';
         aim[t].x=i;
         aim[t].y=j;
      } //把1-N的坐?保存在 aim数?中 
      else if(graph[i][j]=='S'){
          s.x=i;
          s.y=j;
          graph[i][j]='.';
      }//得到最初的起点 
    }
    
    for(i=1;i<=N;i++){
       g.x=aim[i].x;
       g.y=aim[i].y;//1-N ，从小到大，?次把一个 作??点 
       step+=bfs(s,g);
       s.x=g.x;
       s.y=g.y;//到??个?点后??点?起点 
       graph[g.x][g.y]='.';
    }
   
    cout<<step<<endl;
}

int bfs(P s,P g){
    queue<P> que;
      P n;
    
    for(int i=0;i<H;i++)
    for(int j=0;j<W;j++)
     vis[i][j]=-1;
     
     que.push(s);
     vis[s.x][s.y]=0;
     
     while(que.size()){
         
       P p=que.front();
         que.pop();
       if(p.x==g.x&&p.y==g.y)
       break;
       
       for(int i=0;i<4;i++){
           n.x=p.x+dx[i];
           n.y=p.y+dy[i];//向四方漫射 
        if(0<=n.x&&n.x<H&&0<=n.y&&n.y<W&&graph[n.x][n.y]!='X'&&vis[n.x][n.y]==-1){
               que.push(n);
               vis[n.x][n.y]=vis[p.x][p.y]+1;
           }
       }
   }
     return vis[g.x][g.y];
}