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 <string>
#include <string.h>
#include <queue>
using namespace std;
int X, Y;
int Map[1001][1001];
int sx, sy;
int dir[4][2] = { 0,1,0,-1,1,0,-1,0 };
int flag[1001][1002];
struct node {
	int x, y, dis;
	node(int a, int b, int c) :x(a), y(b), dis(c) {}
};

int Find(int aim)
{
	memset(flag, 0, sizeof(flag));
	queue <struct node> q;
	q.push(node(sx, sy, 0));
	flag[sx][sy] = 1;
	while (!q.empty())
	{
		node item = q.front(); q.pop();
		if (Map[item.x][item.y] == aim)
		{
			sx = item.x;
			sy = item.y;
			return item.dis;
		}
		for (int i = 0; i < 4; i++)
		{
			int m = item.x + dir[i][0];
			int n = item.y + dir[i][1];
			if (m <= 0 || m > X || n <= 0 || n > Y || Map[m][n] == -1 || flag[m][n] == 1 )
				continue;
			else
			{
				flag[m][n] = 1;
				q.push(node(m, n, item.dis + 1));
			}
		}
	}
}

int main()
{
	int n;
	string str;
	cin >> X >> Y >> n;
	memset(Map, -1, sizeof(Map));
	for (int i = 1; i <= X; i++)
	{
		cin >> str;
		for (int j = 1; j <= Y; j++)
		{
			if (str[j - 1] == 'S')
			{
				sx = i, sy = j;
				Map[i][j] = 0;
			}
			else if (str[j - 1] == '.')
			{
				Map[i][j] = 0;
			}
			else if (str[j - 1] >= '1' && str[j - 1] <= '9')
			{
				Map[i][j] = str[j - 1] - '0';
			}
		}
	}

	int Sum = 0;
	for (int i = 1; i <= n; i++)
	{
		Sum += Find(i);
	}
	cout << Sum<<endl;

	return 0;
}

C++: #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"ctype.h"
#include"queue"
#include"algorithm"
using namespace std;
struct mmm
{
	int x,y;
}vv;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};
char xx[1001][1001];
int vis[1001][1001],m,n;
int check(int x0,int y0)
{
	if(x0 < 0|| y0 < 0 || x0 >= m || y0 >= n)
		return 0;
	else
		return 1;
}
int bfs(int xp,int yp,int k)
{
	queue <mmm> qq;
	int n;
	vv.x = xp;
	vv.y = yp;
	qq.push(vv);
	vis[xp][yp] = 0;
	while(!qq.empty())
	{
		vv = qq.front();
		qq.pop();
		int x0 = vv.x;
		int y0 = vv.y;
		if(xx[x0][y0] == k + '0')
		{
			n = vis[x0][y0];
			break;
		}
		for(int  i = 0;i < 4;i ++)
		{
			int xt = x0 + dir[i][0];
			int yt = y0 + dir[i][1];
			if(check(xt,yt) && vis[xt][yt] == -1 && xx[xt][yt] != 'X')
			{
				vv.x = xt;
				vv.y = yt;
				qq.push(vv);
				vis[xt][yt] = vis[x0][y0] + 1;
			}
		}
	}
	return n;
}
int main()
{
	int a,b,c,d,x1,y1;
	int mm[10][2];
	while(~scanf("%d %d %d",&m,&n,&a))
	{
		for(int i = 0;i < m;i ++)
		{
			scanf("%s",xx[i]);
		}
		for(int i = 0;i < m;i ++)
		{
			for(int j = 0;j < n;j ++)
			{
				if(xx[i][j] == 'S')
				{
					x1 = i;
					y1 = j;
				}
				else if(isdigit(xx[i][j]))
				{
					int l = xx[i][j] - '0';
					mm[l][0] = i;
					mm[l][1] = j;
				}
			}
		}
		int sum = 0;
		memset(vis,-1,sizeof(vis));
		sum += bfs(x1,y1,1);
		for(int i = 2;i <= a;i ++)
		{
			memset(vis,-1,sizeof(vis));
			sum += bfs(mm[i - 1][0],mm[i - 1][1],i);
		}
		printf("%d\n",sum);
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
#define INF 10000000
using namespace std;
typedef pair<int, int> P;
int h, w, n;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int d[1000][1000];
string field[1000];
P pos[10];
int bfs(P s, P g) {
    queue<P> que;
    for (int i=0; i<h; i++)
        for (int j=0; j<w; j++) d[i][j]=INF;
    que.push(P(s.first, s.second));
    d[s.first][s.second]=0;
    while (!que.empty()) {
        P p = que.front(); que.pop();
        if (p.first==g.first&&p.second==g.second) break;
        for (int i=0; i<4; i++) {
            int nx=p.first+dx[i], ny=p.second+dy[i];
            if (0<=nx&&nx<h&&0<=ny&&ny<w&&field[nx][ny]!='X'&&d[nx][ny]==INF) {
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
    }
    return d[g.first][g.second];
}
int main() {
    scanf("%d%d%d",&h,&w,&n);
    for (int i=0; i<h; i++) {
        cin >> field[i];
        for (int j=0; j<w; j++) {
            if (field[i][j]>'0'&&field[i][j]<='9') {
                pos[field[i][j]-'0']=make_pair(i,j);
            } else if (field[i][j]=='S') {
                pos[0]=make_pair(i,j);
            }
        }
    }
    int res=0;
    for (int i=0; i<n; i++) {
        res+=bfs(pos[i], pos[i+1]);
    }
    printf("%d\n",res);
}

C++: #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
char a[1100][1100];
int vis[1100][1100];
int d[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,k,sum;
struct node{
	int x;
	int y;
	int t;
}nod;
int make(int x,int y)
{
	if(!vis[x][y]&&x>=0&&x<n&&y>=0&&y<m&&a[x][y]!='X')
	return 1;
	else
	return 0;
}
void bfs(node no,char kk)
{
	queue<node>q;
	vis[no.x][no.y]=1;
	int i,j;
	q.push(no);
	while(!q.empty())
	{
		node head=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			node tail;
			tail.x=head.x+d[i][0];
			tail.y=head.y+d[i][1];
			tail.t=head.t+1;
			if(make(tail.x,tail.y))
			{
				if(a[tail.x][tail.y]==kk)
				{
					sum+=tail.t;
					tail.t=0;
					nod=tail;
					return;
				}
				else
				{
					vis[tail.x][tail.y]=1;
					q.push(tail);
				}
			}
		}
	}
}
int main(void)
{
	int i,j,x,y;
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			scanf("%s",a[i]);
			for(j=0;j<m;j++)
			{
				if(a[i][j]=='S')
				{
					nod.x=i;      
					nod.y=j;
					nod.t=0;
				}
			}
		}
		sum=0;
		for(i=1;i<=k;i++)
		{
			memset(vis,0,sizeof(vis));
			bfs(nod,'0'+i);
		}
		printf("%d\n",sum);
	}
	return 0;
}