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<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<ctype.h>
#include<limits.h>

int h, w, n;
int sx, sy;
int gx, gy;
int p = 1;
char map[1002][1002];
int d[1001][1001];
bool visited[1001][1001];
bool used[10];
int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 };
int ans;

typedef struct{
	int num;
  	int d;
	int x, y;
}data;

std::vector<data> vec;

bool compare( const data &d1, const data &d2 )
{
	return d1.d > d2.d;
}

void bfs()
{
	d[sx][sy] = 0;
	visited[sx][sy] = true;
	std::queue<std::pair<int,int> > q;
	q.push( std::make_pair<int,int>( sx, sy ) );
	
	while( !q.empty() ){
		std::pair<int,int> p = q.front(); q.pop();
		int x = p.first;
		int y = p.second;
		
		for( int i = 0; i < 4; i++ ){
			int ix = x + dx[i];
			int iy = y + dy[i];
			
			if( ix >= 1 && ix <= w && iy >= 1 && iy <= h && map[ix][iy] != 'X' && !visited[ix][iy] ){
			 	d[ix][iy] = d[x][y] + 1;
				q.push( std::make_pair<int,int>( ix, iy ) );
				visited[ix][iy] = true;
			}
		}	
	}

	return;
}

int main()
{
	std::cin >> h >> w >> n;
	
	for( int i = 1; i <= h; i++ ){
	 	for( int j = 1; j <= w; j++ ){
			std::cin >> map[j][i];
			if( map[j][i] == 'S' ){
			 	sx = j;
				sy = i;
			}
		}
	}
	
	while( n-- ){
		for( int i = 1; i <= h; i++ ){
		 	for( int j = 1; j <= w; j++ ){
			 	d[j][i] = INT_MAX / 4;
				visited[j][i] = false;
			}
		}	
		
		bfs();
		
		for( int i = 1; i <= h; i++ ){
		 	for( int j = 1; j <= w; j++ ){
				if( isalnum( map[j][i] ) && !used[ map[j][i] - '0' ] ){
					data dat;
					dat.d = d[j][i];
					dat.x = j;
					dat.y = i;
					dat.num = map[j][i] - '0';
					vec.push_back( dat ); 	
				}	
			}
		}
		
		std::sort( vec.begin(), vec.end(), compare );
		
		for( std::vector<data>::iterator it = vec.begin(); it != vec.end(); it++ ){
			if( it->num <= p && !used[it->num] ){
			 	gx = it->x;
				gy = it->y;
				used[it->num] = true;
				break;
			}
		}
		
		ans += d[gx][gy];
		p++;
		
		sx = gx;
		sy = gy;
	}
	
	std::cout << ans << std::endl;

	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int w,h,n,x[10],y[10];
const int inf = 2000000;
char s[1000][1001];
int m[1000][1000];
int f(int c){
	int a=x[c],b=y[c];
	int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
	queue<pii> q;
	rep(i,h)rep(j,w)m[i][j]=inf;
	m[b][a]=0;
	q.push(pii(b,a));
	while(!q.empty()){
		int tx=q.front().second,ty=q.front().first;
		q.pop();
		rep(i,4){
			int cx=tx+dx[i],cy=ty+dy[i];
			if(in(cx,0,w-1) && in(cy,0,h-1) && s[cy][cx]!='X'){
				if(m[cy][cx]>m[ty][tx]+1){
					m[cy][cx]=m[ty][tx]+1;
					q.push(pii(cy,cx));
				}
			}
		}
	}
	return m[y[c+1]][x[c+1]];
}
int main(){
	int cnt;
	scanf("%d%d%d",&h,&w,&n);
	rep(i,h){
		scanf("%s",s[i]);
		rep(j,w){
			if(s[i][j]=='S'){
				x[0]=j;
				y[0]=i;
			}else if(in(s[i][j],'1','9')){
				int c=s[i][j]-'0';
				x[c]=j;
				y[c]=i;
			}
		}
	}
	cnt=0;
	rep(i,n)cnt+=f(i);
	printf("%d\n",cnt);
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<limits.h>
#include<queue>
char o[1002][1003],*p;
int h,w;
int f(int sx,int sy,int gx,int gy)
{
	struct S{int x,y,s;};
	S s={sx,sy,0},t;
	std::queue<S>q;
	static int d[1001][1001]={0},i,j,a[]={-1,0,1,0};
	for(i=1;i<=h;++i)for(j=1;j<=w;++j)d[i][j]=INT_MAX;
	q.push(s);
	while(!q.empty())
	{
		s=q.front();q.pop();
		if(o[s.y][s.x]=='X' || d[s.y][s.x]<=s.s)continue;
		d[s.y][s.x]=s.s;
		for(i=0;i<4;++i)
		{
			t=s;
			t.x+=a[i];
			t.y+=a[(i+1)%4];
			t.s++;
			q.push(t);
		}
	}
	return d[gy][gx];
}
int main()
{
	int n,i,j,x[10],y[10],z,s=0;
	scanf("%d%d%d",&h,&w,&n);
	for(i=0;i<h;++i)
	{
		scanf("%s",&o[i+1][1]);
		p=&o[i+1][1];
		while(p=strpbrk(p,"S123456789"))
			z=isdigit(*p)?*p-'0':0,y[z]=i+1,x[z]=p-o[i+1],++p;
	}
	for(j=0;j<w+2;++j)o[0][j]=o[h+1][j]='X';
	for(i=0;i<h+2;++i)o[i][0]=o[i][w+1]='X';
	for(i=0;i<n;++i)s+=f(x[i],y[i],x[i+1],y[i+1]);
	printf("%d\n",s);
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
int map[1002][1002]={0},myway[1002][1002]={0};
struct ALICE{
int t,h,x,y;
};
int main(){
int a,h,w,n,b,c,d,e,i,j,m=0,g[2],mark=0;
g[0]=1;
g[1]=-1;
char s[2002];
ALICE alice;
queue<ALICE> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
map[i][0]=-1;
map[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
map[0][i]=-1;
map[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",s);
	for(j=0;j<=w;j++){
		a=s[j];
		if(a>48&&a<58)
			map[i][j+1]=a-48;
		else if(s[j]=='S'){
			alice.x=i;
			alice.y=j+1;
			alice.t=0;
			alice.h=1;
			q.push(alice);
		}
		else if(s[j]=='X')
			map[i][j+1]=-1;
	}
}
while(!q.empty()){
	alice=q.front();
	q.pop();
	if(map[alice.x][alice.y]==alice.h){
		alice.h++;
		mark=0;
		while(!q.empty()){
			q.pop();
		}
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				myway[i][j]=0;
			}
		}
	}
	mark++;
	myway[alice.x][alice.y]=mark;
	if(alice.h==n+1){
		if(m==0||m>alice.t){
			m=alice.t;
			break;
		}
	}
	else{
	alice.t++;
		for(i=0;i<2;i++){
			if(map[alice.x+g[i]][alice.y]!=-1&&myway[alice.x+g[i]][alice.y]>mark||map[alice.x+g[i]][alice.y]!=-1&&myway[alice.x+g[i]][alice.y]==0){
				alice.x+=g[i];
				q.push(alice);
				alice.x-=g[i];
			}
			if(map[alice.x][alice.y+g[i]]!=-1&&myway[alice.x][alice.y+g[i]]>mark||map[alice.x][alice.y+g[i]]!=-1&&myway[alice.x][alice.y+g[i]]==0){
				alice.y+=g[i];
				q.push(alice);
				alice.y-=g[i];
			}
		}
	}
}
cout<<m<<endl;
return 0;
}