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 <cstdlib>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> pii;
int table[1100][1100];
int map[1100][1100];
int main()
{
	int height,width,n;
	cin>>height>>width>>n;
	int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
	int ans=0;
	pii f;
	memset(map,-1,sizeof(map));
	for(int y=1;y<=height;y++)
	{
		for(int x=1;x<=width;x++)
		{
			char m;cin>>m;
			int temp;
			if(m=='S')
			{
				f.first=x;
				f.second=y;
				temp=0;
			}
			else if(m=='X')
			{
				temp=-1;
			}
			else if(m=='.')
			{
				temp=0;
			}
			else 
			{
				temp=m-'0';
			}
			map[y][x]=temp;
		}
	}
	
	
	
	
	for(int u=1;u<=n;u++)
	{
		queue< pii > que;
		memset(table,-1,sizeof(table));
		que.push(f);
		table[f.second][f.first]=0;
		while(!que.empty())
		{
			pii now=que.front();que.pop();
			if(map[now.second][now.first]==u)
			{
				ans+=table[now.second][now.first];
				f=now;
				break;
			}
			for(int i=0;i<4;i++)
			{
				int x,y;
				x=now.first+dx[i];
				y=now.second+dy[i];
				
				if(map[y][x]!=-1&&table[y][x]==-1)
				{
					table[y][x]=table[now.second][now.first]+1;
					pii next;
					next.first = x;
					next.second = y;
					que.push(next);
				}
			}
			/*
			for(int y=1;y<=height;y++)
			{
				for(int x=1;x<=width;x++)
				{
					cout<<table[y][x];
				}
				cout<<endl;
			}
			*/
		}
	}
	
	cout<<ans<<endl;
}

C++: #include<queue>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j;
  int c[10],d[10],n,w,h,x,y,z;
  int sm=0;
  char s;
  bool a[1002][1002],b[1002][1002];
  queue<pair<pair<int,int>,int> > q;
  memset(a,0,sizeof(a));
  cin>>h>>w>>n;
  for(i=1;i<h+1;i++){
    for(j=1;j<w+1;j++){
      cin>>s;
      if(s!='X'){
	a[j][i]=1;
	if(0){
	}else if(s=='.'){
	}else if(s=='S'){
	  c[0]=j;
	  d[0]=i;
	}else{
	  c[s-'0']=j;
	  d[s-'0']=i;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    memset(b,0,sizeof(b));
    while(q.empty()==0)
      q.pop();
    q.push(make_pair(make_pair(c[i],d[i]),0));
    for(;;){
      x=q.front().first.first;
      y=q.front().first.second;
      z=q.front().second;
      q.pop();
      if(a[x][y]&&b[x][y]==0){
	if(x==c[i+1]&&y==d[i+1])
	  break;
	b[x][y]=1;
	q.push(make_pair(make_pair(x-1,y),z+1));
	q.push(make_pair(make_pair(x,y-1),z+1));
	q.push(make_pair(make_pair(x+1,y),z+1));
	q.push(make_pair(make_pair(x,y+1),z+1));
      }
    }
    sm+=z;
  }
  cout<<sm<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct coor{
    int y,x;
    coor(){}
    coor(int Y,int X) : y(Y), x(X){}
    bool operator!=(coor a){return y!=a.y||x!=a.x;}
};

int main(){
    int h,w,n; cin>>h>>w>>n;
    vector<coor> check(n+1);
    vector<vector<int>> field(h+2,vector<int>(w+2,1));
    for(int i=1;i<=h;i++) for(int j=1;j<=w;j++){
        char c; cin>>c;
        if(c!='X') field[i][j]=0;
        if(c=='S') check[0]=coor(i,j);
        if(isdigit(c)) check[c-'0']=coor(i,j);
    }
    int ans=0;
    for(int i=0;i<n;i++){
        auto copy=field;
        auto start=check[i];
        auto goal=check[i+1];
        queue<pair<int, coor>> que;
        que.push(make_pair(0,start));
        while(que.front().second!=goal){
            auto dist=que.front().first;
            auto cur=que.front().second;
            que.pop();
            if(copy[cur.y][cur.x]==1) continue;
            copy[cur.y][cur.x]=1;
            for(int j=0;j<4;j++){
                int v[]={1,0,-1,0,1};
                que.push(make_pair(dist+1,coor(cur.y+v[j], cur.x+v[j+1])));
            }
        }
        ans+=que.front().first;
    }
    cout<<ans<<endl;
	return 0;
}

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

int main()
{
    int h,w,n;
    cin >> h >> w >> n;
    char mas[h+2][w+2];
    int x[4]={1,0,-1,0},y[4]={0,1,0,-1},co=0;
    int qx[n],qy[n];
    int a[h+2][w+2],c[h+2][w+2];
    for(int i=0;i<h+2;i++)
        for(int j=0;j<w+2;j++){
            a[i][j]=-1;
            c[i][j]=0;
        }
    for(int i=1;i<h+1;i++)
        for(int j=1;j<w+1;j++){
            cin >> mas[i][j];
            if(mas[i][j]=='.')
                a[i][j]=0;
            else if(mas[i][j]=='S'){
                qx[0]=i;
                qy[0]=j;
                a[i][j]=0;
            }
            else if(mas[i][j]=='X') continue;
            else{
                a[i][j]=0;
                qx[(int)mas[i][j]-'1'+1]=i;qy[(int)mas[i][j]-'1'+1]=j;
                c[i][j]=(int)mas[i][j]-'1'+1;
            }
        }
    int b[h+2][w+2];
    queue< pair<int,int> > q[n];
    for(int i=0;i<n;i++){
        int life=i+1;
        for(int k=0;k<h+2;k++)
            for(int j=0;j<w+2;j++)
                b[k][j]=a[k][j];
        q[i].push(make_pair(qx[i],qy[i]));
        int ty=0;
        for(;;){
            for(int ij=0;ij<4;ij++){
                if(b[q[i].front().first+x[ij]][q[i].front().second+y[ij]]==0){
                    if(c[q[i].front().first+x[ij]][q[i].front().second+y[ij]]==life){
                        co+=b[q[i].front().first][q[i].front().second]+1;
                        ty=1;
                        break;
                    }
                    b[q[i].front().first+x[ij]][q[i].front().second+y[ij]]=b[q[i].front().first][q[i].front().second]+1;
                    q[i].push(make_pair(q[i].front().first+x[ij],q[i].front().second+y[ij]));
                }
            }
            q[i].pop();
            if(ty==1){
                ty=0;
                break;
            }
        }
    }
    cout << co << endl;
    return 0;
}