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 <utility>
#include <queue>
using namespace std;

typedef pair<int, int> P;

int main()
{
	int H, W, N, gn = 0, res = 0;
	cin >> H >> W >> N;
	char ch;
	vector< vector<int> > a(W, vector<int>(H, 1000002));
	P start;
	vector< P > goal(N);

	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> ch;
			if (ch == 'X') {
				a[j][i] = -1;
			}
			else if (ch == 'S') {
				start.first = j;
				start.second = i;
			}
			else if (ch >= '1' && ch <= '9') {
				goal[ch - '1'].first = j;
				goal[ch - '1'].second = i;
			}
		}
	}
	while (gn < N) {
		int ite = 1;
		queue< P > bfs;
		bfs.push(start);
		a[start.first][start.second] = 0;
		while (!bfs.empty()) {
			P temp = bfs.front();
			bfs.pop();
			if (temp.first + 1 < W && a[temp.first + 1][temp.second] == 1000002) {
				if (goal[gn].first == temp.first + 1 && goal[gn].second == temp.second) {
					res += ite;
					break;
				}
				else {
					a[temp.first + 1][temp.second] = ite;
					bfs.push(P(temp.first + 1, temp.second));
				}
			}
			if (temp.first - 1 >= 0 && a[temp.first - 1][temp.second] == 1000002) {
				if (goal[gn].first == temp.first - 1 && goal[gn].second == temp.second) {
					res += ite;
					break;
				}
				else {
					a[temp.first - 1][temp.second] = ite;
					bfs.push(P(temp.first - 1, temp.second));
				}
			}
				a[temp.first - 1][temp.second] = ite;
			if (temp.second + 1 < H && a[temp.first][temp.second + 1] == 1000002) {
				if (goal[gn].first == temp.first && goal[gn].second == temp.second + 1) {
					res += ite;
					break;
				}
				else {
					a[temp.first][temp.second + 1] = ite;
					bfs.push(P(temp.first, temp.second + 1));
				}
			}
			if (temp.second - 1 >= 0 && a[temp.first][temp.second - 1] == 1000002) {
				if (goal[gn].first == temp.first && goal[gn].second == temp.second - 1) {
					res += ite;
					break;
				}
				else {
					a[temp.first][temp.second - 1] = ite;
					bfs.push(P(temp.first, temp.second - 1));
				}
			}

			ite++;
		}
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (a[j][i] != -1) {
					a[j][i] = 1000002;
				}
			}
		}
		start = goal[gn];
		gn++;
	}
	cout << res << endl;
	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;
			}
		}
	}
	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;
			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)
		{
			was[q.first][q.second] = -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][q.second] = -1;
			q.first--;
			que.push(q);
			count++;							/*???????????????*/
			quec.push(count);
			count--;
			q.first++;
		}
	}

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