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 <string>
#include <queue>
using namespace std;
#define INF 100000000
int main(void){
    int h,w,n,ans=0;
    pair<int,int> start;
    cin>>h>>w>>n;
    vector<string>field(h+2,string(w+2,'X'));
    vector<vector<int>>d(h+2,vector<int>(w+2,0));
    vector<pair<int,int>>goal(n);
    int dx[4]={1,0,-1,0};
    int dy[4]={0,1,0,-1};

    for(int i=1;i<h+1;i++){
        for(int j=1;j<w+1;j++){
            cin>>field[i][j];
            if(field[i][j]=='S'){
                start.first=j;
                start.second=i;
            }else if(field[i][j]!='.'&&field[i][j]!='X'){
                goal[(int)(field[i][j]-'1')].first=j;
                goal[(int)(field[i][j]-'1')].second=i;
            }
        }
    }
    for(int i=0;i<n;i++){
        queue<pair<int,int>> que;
        for(int j=0;j<h+2;j++){
            for(int k=0;k<w+2;k++){
                d[j][k]=INF;
            }
        }
        que.push(pair<int,int>(start.first,start.second));
        d[start.second][start.first]=0;
        while(que.size()){
            pair<int,int> p=que.front();
            que.pop();
            if((p.first==goal[i].first)&&(p.second==goal[i].second)){
                break;
            }
            for(int j=0;j<4;j++){
                int nx=p.first+dx[j];
                int ny=p.second+dy[j];
                if(field[ny][nx]!='X'&&d[ny][nx]==INF){
                    que.push(pair<int,int>(nx,ny));
                    d[ny][nx]=d[p.second][p.first]+1;
                }
            }
        }
        ans+=d[goal[i].second][goal[i].first];
        start.first=goal[i].first;
        start.second=goal[i].second;
    }
    cout<<ans<<endl;
}

C++: #include "cstring"
#include "cstdio"
#include "queue"
using namespace std;

const int maxn=1024;

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

char mp[maxn][maxn];
int vis[maxn][maxn];
int wx[10],wy[10];
int n,m,k;

int bfs(int x1,int y1,int x2,int y2)
{
	memset(vis,0,sizeof(vis));
	queue<int>qx;qx.push(x1);
	queue<int>qy;qy.push(y1);
	queue<int>qt;qt.push(0);
	vis[x1][y1]=1;
	
	while(!qx.empty())
	{
		int nx=qx.front();qx.pop();
		int ny=qy.front();qy.pop();
		int nt=qt.front();qt.pop();
		
		if(nx==x2 && ny==y2)return nt;
		
		for(int i=0;i<4;i++)
		{
			int px=nx+dx[i];
			int py=ny+dy[i];
			if(px>=0 && py>=0 && px<n && py<m && mp[px][py]!='X' && !vis[px][py])
			{
				qx.push(px);
				qy.push(py);
				qt.push(nt+1);
				vis[px][py]=1;
			}
		}
		
	}
	
	return 0;
}

int main()
{
	//freopen("f.txt","r",stdin);
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			mp[i][j]=getchar();
			while(mp[i][j]=='\n' || mp[i][j]=='\0')mp[i][j]=getchar();
			if(mp[i][j]>='1' && mp[i][j]<='9')
			{
				wx[mp[i][j]-'0']=i;
				wy[mp[i][j]-'0']=j;
			}
			if(mp[i][j]=='S')
			{
				wx[0]=i;
				wy[0]=j;
			}	
		}
	int ans=0;
	for(int i=0;i<k;i++)
		ans+=bfs(wx[i],wy[i],wx[i+1],wy[i+1]);
	printf("%d\n",ans);
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int map[1002][1002], was[1002][1002];
int main(void)
{
	int count = 0, H, W, N, i, j;
	char c=0;
	pair<int, int> q,a[10];
	queue<pair<int, int> > que;
	queue<int> quec;
	quec.push(0);
	scanf("%d %d %d", &H, &W, &N);
	for (i = 0; i <= H + 1; i++)
	{
		map[i][0] = -1;
		map[i][W + 1] = -1;
		was[i][0] = -1;					/*?????????????£???????(???)*/
		was[i][W + 1] = -1;
	}
	for (j = 1; j <= W; j++)
	{
		map[0][j] = -1;
		map[H + 1][j] = -1;           /*?????????????£???????????¨????*/
		was[0][j] = -1;
		was[H + 1][j] = -1;
	}
	for (i = 1; i <= H; i++)
	{
		for (j = 1; j <= W; j++)			/*???????????????(??????????????°???????????????????????????????????¨??°??????????????????*/
		{
			cin >> c;
			if (c == 'X')
				map[i][j] = -1;
			if (c == 'S')
			{
				q.first = i;
				q.second = j;
			}
			if (c == '1')
			{
				a[1].first = i;
				a[1].second = j;
			}
			if (c == '2')
			{
				a[2].first = i;
				a[2].second = j;
			}
			if (c == '3')
			{
				a[3].first = i;
				a[3].second = j;
			}
			if (c == '4')
			{
				a[4].first = i;
				a[4].second = j;
			}
			if (c == '5')
			{
				a[5].first = i;
				a[5].second = j;
			}
			if (c == '6')
			{
				a[6].first = i;
				a[6].second = j;
			}
			if (c == '7')
			{
				a[7].first = i;
				a[7].second = j;
			}
			if (c == '8')
			{
				a[8].first = i;
				a[8].second = j;
			}
			if (c == '9')
			{
				a[9].first = i;
				a[9].second = j;
			}
		}
	}
	/*for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			cout << map[i][j];
		}
		cout << endl;
	}*/
	que.push(q);
	i = 1;
	while (i <= N)
	{
		count = quec.front();
		quec.pop();
		q = que.front();
		que.pop();

		if (q == a[i])
		{

			i++;
			memset(was, 0, sizeof(was));
			while (!que.empty())
				que.pop();
			while (!quec.empty())
				quec.pop();
			quec.push(count);
			que.push(q);
			continue;
		}
		if (map[q.first][q.second + 1] != -1 && was[q.first][q.second + 1] != -1)
		{
			was[q.first][q.second+1] = -1;
			q.second++;
			que.push(q);					/*???????????????*/
			count++;
			quec.push(count);
			count--;
			q.second--;
		}
		if (map[q.first][q.second - 1] != -1 && was[q.first][q.second-1] != -1)
		{
			was[q.first][q.second-1] = -1;
			q.second--;
			que.push(q);
			count++;						/*???????????????*/
			quec.push(count);
			count--;
			q.second++;
		}
		if (map[q.first + 1][q.second] != -1 && was[q.first + 1][q.second] != -1)
		{
			was[q.first + 1][q.second] = -1;
			q.first++;
			que.push(q);
			count++;						/*???????????????*/
			quec.push(count);
			count--;
			q.first--;
		}
		if (map[q.first - 1][q.second] != -1 && was[q.first - 1][q.second] != -1)
		{
			was[q.first-1][q.second] = -1;
			q.first--;
			que.push(q);
			count++;							/*???????????????*/
			quec.push(count);
			count--;
			q.first++;
		}
	}

	printf("%d\n", count);					/*????????£???????????????????¨??????????*/
	return 0;
}