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++: //???2:???????????¢?´¢
#include <stdio.h>
#include <stack>
#include <math.h>
#include <queue>

using namespace std;

int height,width,N;
char** table;
bool** checkTable;


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

struct Data{
	int row,col,dist;
};



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

queue<Data> Q;

int func(int row,int col,int goal_row,int goal_col,int dist){

	Data data,tmp;
	data.row = row;
	data.col = col;
	data.dist = 0;
	Q.push(data);

	int ret = 0;

	while(!Q.empty()){
		tmp = Q.front();
		Q.pop();
		checkTable[tmp.row][tmp.col] = true;

		if(tmp.col == goal_col && tmp.row == goal_row){
			ret = tmp.dist;
			break;
		}

		if(rangeCheck(tmp.row-1,tmp.col) == true && checkTable[tmp.row-1][tmp.col] == false && table[tmp.row-1][tmp.col] != 'X'){
			Data new_data;
			new_data.row = tmp.row-1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row+1,tmp.col) == true && checkTable[tmp.row+1][tmp.col] == false && table[tmp.row+1][tmp.col] != 'X'){
			Data new_data;
			new_data.row = tmp.row+1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col-1) == true && checkTable[tmp.row][tmp.col-1] == false && table[tmp.row][tmp.col-1] != 'X'){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col-1;
			new_data.dist = tmp.dist+1;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col+1) == true && checkTable[tmp.row][tmp.col+1] == false && table[tmp.row][tmp.col+1] != 'X'){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col+1;
			new_data.dist = tmp.dist+1;
			Q.push(new_data);
		}
	}

	while(Q.empty())Q.pop();

	return ret;
}

int main(){

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


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

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

		for(int k = 0; k <width; k++){
			checkTable[i][k] = false;
			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]);
		}
	}

	for(int i = 0; i <= N-1; i++){
		total_dist += func(info[i].row,info[i].col,info[i+1].row,info[i+1].col,0);
		for(int k = 0; k < height; k++){
			for(int p = 0; p < width; p++)checkTable[k][p] = false;
		}
	}

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

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
int h, w, n;
char field[1000][1000];
bool visited[10][1000][1000];
struct state {
	int i, j, d;
};

bool out(int i, int j) {
	return i < 0 || h <= i || j < 0 || w <= j;
}

int search(int i, int j, int num) {
	if(num == n + 1) return 0;
	int sum = 0;
	queue<state> q;
	q.push({ i, j, 0 });
	while(!q.empty()) {
		state s = q.front(); q.pop();
		if(out(s.i, s.j) || field[s.i][s.j] == 'X' || visited[num][s.i][s.j]) continue;
		if(field[s.i][s.j] == '0' + num) {
			sum += s.d;
			break;
		}
		visited[num][s.i][s.j] = true;
		int dir[] = { 1, 0, -1, 0, 1 };
		for(int k = 0; k < 4; k++) {
			if(!visited[num][s.i + dir[k]][s.j + dir[k + 1]])
				q.push({ s.i + dir[k], s.j + dir[k + 1], s.d + 1 });
		}
	}
	for(int ii = 0; ii < h; ii++) {
		for(int jj = 0; jj < w; jj++) {
			if(field[ii][jj] == '0' + num) {
				sum += search(ii, jj, num + 1);
			}
		}
	}
	return sum;
}


int main() {
	cin >> h >> w >> n;
	for(int i = 0; i < h; i++) {
		cin >> field[i];
	}
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			if(field[i][j] == 'S') {
				cout << search(i, j, 1) << endl;
				return 0;
			}
		}
	}

}

C++: // 2011/09/20 Tazoe

#include <iostream>
#include <queue>
#include <map>
using namespace std;

struct point{
	int y;
	int x;
};

struct state{
	int y;
	int x;
	int t;
};

int main()
{
	int H, W, N;
	cin >> H >> W >> N;

	char M[1002][1002];
	for(int y=0; y<=H+1; y++)
		for(int x=0; x<=W+1; x++)
			M[y][x] = 'X';

	struct point P[10];
	for(int y=1; y<=H; y++){
		for(int x=1; x<=W; x++){
			cin >> M[y][x];
			if(M[y][x]=='S'){
				P[0].y = y;
				P[0].x = x;
				M[y][x] = '.';
			}
			else if(M[y][x]>='1'&&M[y][x]<='9'){
				P[M[y][x]-'0'].y = y;
				P[M[y][x]-'0'].x = x;
				M[y][x] = '.';
			}
		}
	}

	int sum = 0;
	for(int I=0; I<N; I++){
		struct state now;
		now.y = P[I].y;
		now.x = P[I].x;
		now.t = 0;

		queue<struct state> que;
		que.push(now);

		map<int, bool> memo;

		while(true){
			if(que.empty())
				break;

			now = que.front();
			que.pop();

			int yx = now.y*10000+now.x;
			if(memo[yx])
				continue;
			else
				memo[yx] = true;

			if(now.y==P[I+1].y&&now.x==P[I+1].x){
				sum += now.t;
				break;
			}

			struct state nxt = now;
			nxt.y--;
			nxt.t++;
			if(M[nxt.y][nxt.x]=='.')
				que.push(nxt);

			nxt = now;
			nxt.y++;
			nxt.t++;
			if(M[nxt.y][nxt.x]=='.')
				que.push(nxt);

			nxt = now;
			nxt.x--;
			nxt.t++;
			if(M[nxt.y][nxt.x]=='.')
				que.push(nxt);

			nxt = now;
			nxt.x++;
			nxt.t++;
			if(M[nxt.y][nxt.x]=='.')
				que.push(nxt);
		}
	}

	cout << sum << endl;

/*
	for(int y=0; y<=H+1; y++){
		for(int x=0; x<=W+1; x++)
			cout << M[y][x];
		cout << endl;
	}

	for(int i=0; i<=N; i++)
		cout << P[i].y << ' ' << P[i].x << endl;
*/

	return 0;
}