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<queue>
using namespace std;
int h,w,n,ans=0;
char c[1002][1002];
pair<int,int> a[12],pi;
bool f[1002][1002];

int dist(pair<int,int> s,pair<int,int> g){
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)f[i][j]=false;
	//cout<<s.first<<","<<s.second<<" "<<g.first<<","<<g.second<<endl;
	queue<pair<pair<int,int>,int>> que;
	pair<pair<int,int>,int> p;
	p.first=s; p.second=0;
	que.push(p);
	while(que.size()>0){
		p=que.front(); que.pop();
		if(p.first==g)return p.second;
		f[p.first.first][p.first.second]=true;
		//cout<<p.first.first<<","<<p.first.second<<endl;
		//if(p.first.first==g.first&&p.first.second==g.second)return p.second;
		if(c[p.first.first-1][p.first.second]!='X'&&!f[p.first.first-1][p.first.second]){p.first.first--; p.second++; que.push(p); p.first.first++; p.second--;}
		if(c[p.first.first+1][p.first.second]!='X'&&!f[p.first.first+1][p.first.second]){p.first.first++; p.second++; que.push(p); p.first.first--; p.second--;}
		if(c[p.first.first][p.first.second-1]!='X'&&!f[p.first.first][p.first.second-1]){p.first.second--; p.second++; que.push(p); p.first.second++; p.second--;}
		if(c[p.first.first][p.first.second+1]!='X'&&!f[p.first.first][p.first.second+1]){p.first.second++; p.second++; que.push(p); p.first.second--; p.second--;}
	}
	return -1;
}
	

int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)c[i][j]='X';
	for(int i=1;i<=h;i++){
		scanf("\n");
		for(int j=1;j<=w;j++){
			scanf("%c",&c[i][j]);
			if('1'<=c[i][j]&&c[i][j]<='9'){
				a[c[i][j]-48].first=i;
				a[c[i][j]-48].second=j;
			}
			if(c[i][j]=='S'){
				a[0].first=i; a[0].second=j;
			}
		}
	}
		/*for(int i=1;i<=h;i++){	
			for(int j=1;j<=w;j++){
				printf("%c",c[i][j]);
			}
			printf("\n");
		}*/
	for(int i=1;i<=n;i++){
		//cout<<i<<endl;
		ans+=dist(a[i-1],a[i]);
	}
	printf("%d",ans);
}

C++: #include<stdio.h>
#include<string.h>

#include<iostream>
#include<queue>


using namespace std ;


#define y .first
#define x .second


pair< int ,int > p ,cpy ;


char route[ 1000 + 1 ][ 1000 + 1 ] ;

int came[ 1000 + 1 ][ 1000 + 1 ] ;


bool check( pair< int ,int > now,int h,int w ) ;

#define size sizeof came


int mx[] = { 1 ,0 ,0 ,-1 } ;
int my[] = { 0 ,1 ,-1 ,0 } ;


int main()
{
	int h ,w ,n ;
	scanf( "%d %d %d" ,&h ,&w ,&n ) ;

	char end = '0' + n ,cheese = '1' ;

	scanf( "%c" ,&route[ 0 ][ 0 ] ) ;

	int i ,j ;
	for( i = 0 ; i < h ; ++i )
	{
		for( j = 0 ; j <= w ; ++j )
		{
			scanf( "%c" ,&route[ i ][ j ] ) ;

			if( route[ i ][ j ] == 'S' )
			{
				p y = i ;
				p x = j ;
			}
		}
	}

	int ans = 0 ;
	
	do
	{
		queue< pair< int ,int > > que ;

		memset( came ,0 ,size ) ;

		while( route[ p y ][ p x ] != cheese )
		{
			int i ;
			for( i = 0 ; i < 4 ; ++i )
			{
				cpy y = p y + my[ i ] ;
				cpy x = p x + mx[ i ] ;

				if( route[ cpy y ][ cpy x ] != 'X' && check( cpy ,h ,w ) == true )
				{
					came[ cpy y ][ cpy x ] = came[ p y ][ p x ] + 1 ;

					que.push( cpy ) ;
				}
			}

			came[ p y ][ p x ] = 1 ;

			p = que.front() ;

			que.pop() ;
		}

		++cheese ;

		ans += came[ p y ][ p x ] ;

	}while( cheese <= end ) ;

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


	return 0 ;
}


bool check( pair< int ,int > now ,int h ,int w )
{
	if( now y >= h || now x >= w || now y < 0 || now x < 0 )
		return false ;

	else if( came[ now y ][ now x ] > 0 )
		return false ;

	return true ;
}

C++: #include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;

//firstにh、secondにwを入れる
typedef pair<int, int> PII;

//S、1...Nまでの座標を格納する
PII cheese[10];

const int MAX_H = 1010, MAX_W = 1010;
int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];

int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

bool infield(int h, int w){
    return (0<=h && h<H && 0<=w && w<W);
}

//sからgまでの最短手数を返す
int bfs(PII s, PII g){
    queue<PII> que;
    //infで初期化
    memset(d, 0x3f, sizeof(d));
    que.push(s);
    d[s.first][s.second] = 0;
    
    while(!que.empty()){
        PII p = que.front(); que.pop();
        if(p == g)
            break;
        for(int i=0; i<4; i++){
            int nh = p.first+dh[i];
            int nw = p.second+dw[i];
            
            if(infield(nh, nw) && field[nh][nw]!='X' && d[nh][nw]==INF){
                que.push(PII(nh, nw));
                d[nh][nw] = d[p.first][p.second] + 1;
            }
        }
    }
    
    return d[g.first][g.second];
}

void solve(){
    int ans = 0;
    
    //cheeseリストを作る
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(field[i][j] == 'S')
                cheese[0] = PII(i, j);
            
            for(char c='1'; c<='9'; c++){
                if(field[i][j] == c)
                    cheese[c-'0'] = PII(i, j);
            }
        }
    }
    for(int i=0; i<N; i++)
        ans += bfs(cheese[i], cheese[i+1]);
    printf("%d\n", ans);
}

int main(){
    scanf("%d%d%d", &H, &W, &N);
    for(int i=0; i<H; i++)
        scanf("%s", field[i]);
    solve();
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<climits>
#include<cstdlib>
using namespace std;
int d[1000][1000];
char input[1000][1000];
bool visited[1000][1000];
int H,W,N,S_x,S_y,answer=0;
const int dirx[]={0,-1,0,1};
const int diry[]={1,0,-1,0};
int now='1';
int D[10];
queue<char> q;
void BFS();
void clean();
int main(){
	int i,j;
	char ch; 
	cin>>H>>W>>N;
	for(i=0;i<H*W;i++){
		cin>>ch;
		input[i%W][i/W]=ch;
		d[i%W][i/W]=0;
		if(ch=='S'){
			S_x=i%W;S_y=i/W;
		}
	}
	while(now<=N+48){
		BFS();
		now++;
	}
	for(i=1;i<=N;i++)
		answer+=D[i];
	cout<<answer<<endl;
}
void BFS(){
	int x,y,k,mx,my;
	q.push(S_x);
	q.push(S_y);
	visited[S_x][S_y]=true;
	while(!q.empty()){
		x=q.front();q.pop();
		y=q.front();q.pop();
		for(k=0;k<4;k++){
			mx=x+dirx[k];
			my=y+diry[k];
			if(mx<W&&mx>=0&&my<H&&my>=0){ 
			if(input[mx][my]!='X'&&!visited[mx][my]){
				q.push(mx);
				q.push(my);
				d[mx][my]=d[x][y]+1;
				if(input[mx][my]==(char)now){S_x=mx;S_y=my;D[now-48]=d[mx][my];}
				visited[mx][my]=true;
			}}	 
		}
	}
	clean();
}
void clean(){
	int i,j;
	for(i=0;i<=W;i++)
		for(j=0;j<=H;j++){
			visited[i][j]=false;
			d[i][j]=0;}
}