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<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
	int h,w,n;
	string ss[1000];
	int x[1000][1000];
	int X,Y;
	int ans=0;

	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<h;i++)cin >> ss[i];

	for(int i=1;i<=n;i++){
		for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				x[j][k]=10001;
			}
		}
		for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				if(i==1){
					if(ss[j][k]=='S')x[j][k]=0;
				}
				else{
					if(ss[j][k]==(i-1)+'0')x[j][k]=0;
				}
				if(ss[j][k]==i+'0'){
					X=j,Y=k;
				}
			}
		}

		int z=0;
		while(x[X][Y]==10001){
			for(int j=0;j<h;j++){
				for(int k=0;k<w;k++){
					if(x[j][k]==z){
						if(j>=1&&ss[j-1][k]!='X')x[j-1][k]=min(x[j-1][k],z+1);
						if(k>=1&&ss[j][k-1]!='X')x[j][k-1]=min(x[j][k-1],z+1);
						if(j<h-1&&ss[j+1][k]!='X')x[j+1][k]=min(x[j+1][k],z+1);
						if(k<w-1&&ss[j][k+1]!='X')x[j][k+1]=min(x[j][k+1],z+1);
					}
				}
			}
			z++;
		}
		ans+=x[X][Y];
		/*for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				printf("%d ",x[j][k]);
			}
			printf("\n");
		}
		printf("%d\n",x[X][Y]);*/
	}

	printf("%d\n",ans);
	return 0;
}

C++: #include <stdio.h>
#include <stack>
#include <math.h>

using namespace std;

int height,width,N;
char** table;
int** distanceTable;

struct Info{
	int cheese_id,row,col;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= height-1 && col >=0 && col <= width-1)return true;
	else{
		return false;
	}
}

void func(int row,int col,int goal_row,int goal_col,int dist){
	distanceTable[row][col] = dist;
	if(row == goal_row && col == goal_col)return;

	if(rangeCheck(row-1,col) == true && (distanceTable[row-1][col] == 0 || distanceTable[row-1][col] > dist+1))func(row-1,col,goal_row,goal_col,dist+1);
	if(rangeCheck(row+1,col) == true && (distanceTable[row+1][col] == 0 || distanceTable[row+1][col] > dist+1))func(row+1,col,goal_row,goal_col,dist+1);
	if(rangeCheck(row,col-1) == true && (distanceTable[row][col-1] == 0 || distanceTable[row][col-1] > dist+1))func(row,col-1,goal_row,goal_col,dist+1);
	if(rangeCheck(row,col+1) == true && (distanceTable[row][col+1] == 0 || distanceTable[row][col+1] > dist+1))func(row,col+1,goal_row,goal_col,dist+1);
}

int main(){

	scanf("%d %d %d",&height,&width,&N);
	table = new char*[1000];
	distanceTable = new int*[1000];
	Info info[N+1];
	int info_index = 0,total_dist = 0;


	for(int i=0; i < height; i++){
		table[i] = new char[1001];
		distanceTable[i] = new int[1000];

		scanf("%s",table[i]);

		for(int k = 0; k <width; k++){
			if(table[i][k] == 'X')distanceTable[i][k] = -1;
			else{
				distanceTable[i][k] = 0;
			}

			if(table[i][k] == 'S'){
				info[info_index].cheese_id = 0;
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}else if(table[i][k] >= '1' && table[i][k] <= '9'){
				info[info_index].cheese_id = table[i][k] - '0';
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}
		}
	}


	//???????????????????????±?????????????????????
	for(int i=1; i <= N; i++){
		for(int k=N; k >= i; k--){
			if(info[k].cheese_id < info[k-1].cheese_id)swap(info[k],info[k-1]);
		}
	}

	//i????????????????????????i+1???????????????????????§??????????????¢?????¨??¢?´¢??§?±???????
	for(int i = 0; i <= N-1; i++){
		func(info[i].row,info[i].col,info[i+1].row,info[i+1].col,0);
		total_dist += distanceTable[info[i+1].row][info[i+1].col];

		for(int k = 0; k < height; k++){
			for(int p = 0; p < width; p++){
				if(distanceTable[k][p] > 0)distanceTable[k][p] = 0;
			}
		}
	}

	printf("%d\n",total_dist);

    return 0;
}

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

const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 10;

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

typedef struct {
	int x, y;
} node;
node makeNode(int x, int y) {
	node n = {x, y};
	return n;
}

int w, h, n;
// 入力されたフィールド (W * H)
char field[MAX_H][MAX_W];
// 各地点までのステップ数，未到達なら INT_MAX
int step[MAX_H][MAX_W];

int bfs(int startX, int startY, int goalX, int goalY) {
	queue<node> que;
	que.push(makeNode(startX, startY));
	step[startY][startX] = 0;
	while (!que.empty()) {
		node current = que.front();
		que.pop();
		if (current.x == goalX && current.y == goalY) {
			return step[goalY][goalX];
		}
		int nextStep = step[current.y][current.x] + 1;
		for (int i = 0; i < 4; i++) {
			int nextX = current.x + dx[i];
			int nextY = current.y + dy[i];
			if (0 <= nextX && nextX < w && 0 <= nextY && nextY < h
					&& field[nextY][nextX] != 'X' && step[nextY][nextX] == INT_MAX) {
				que.push(makeNode(nextX, nextY));
				step[nextY][nextX] = nextStep;
			}
		}
	}
	return INT_MAX;
}

int main () {
	scanf("%d %d %d", &h, &w, &n);
	int startX, startY;
	// 入力しながらスタートの座標を探す
	for (int i = 0; i < h; i++) {
		scanf("%s", field[i]);
		for (int j = 0; j < w; j++) {
			if (field[i][j] == 'S') {
				startX = j;
				startY = i;
			}
		}
	}
	
	// 工場を順番に回って合計ステップを計算する
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		// step を INT_MAX で初期化しながらゴールの座標を探す
		int goalX, goalY;
		for (int j = 0; j < h; j++) {
			for (int k = 0; k < w; k++) {
				step[j][k] = INT_MAX;
				if (field[j][k] == '0' + i) {
					goalX = k;
					goalY = j;
				}
			}
		}
		
		ans += bfs(startX, startY, goalX, goalY);
		
		startX = goalX;
		startY = goalY;
	}
	
	printf("%d\n", ans);
	return 0;
}