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>

using namespace std;

void recur(int i,int j,vector<vector<char> > &rect,vector<vector<int> > &dp){
	try{
		if (rect.at(i - 1).at(j) != 'X' and dp[i-1][j]>dp[i][j]+1){
			dp[i - 1][j] = dp[i][j]+1;
			recur(i-1,j,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i + 1).at(j) != 'X' and dp[i+1][j]>dp[i][j]+1){
			dp[i + 1][j] = dp[i][j]+1;
			recur(i+1,j,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i).at(j-1) != 'X' and dp[i][j-1]>dp[i][j]+1){
			dp[i][j-1] = dp[i][j]+1;
			recur(i,j-1,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i).at(j+1) != 'X' and dp[i][j+1]>dp[i][j]+1){
			dp[i][j+1] = dp[i][j]+1;
			recur(i,j+1,rect,dp);
		}
	}
	catch (...){}
	return;
}

int trvrs(char S,char G,vector<vector<char> > R){
	int count;
	//cout << S << G << endl;
	vector<vector<int> > dp(R.size(), vector<int>(R.front().size()));
	for (int i = 0; i<R.size(); i++){
		for (int j = 0; j<R.front().size(); j++){
			dp[i][j]=1000000; 
		}
	}
	for (int i = 0; i<R.size(); i++){
		for (int j = 0; j<R.front().size(); j++){
			if(R[i][j] == S) {
				dp[i][j]=0;
				recur(i,j,R,dp);
			}; 
		}
	}
	for (int i = 0; i<R.size(); i++){
		for (int j = 0; j<R.front().size(); j++){
			//cout << dp[i][j];
			if(R[i][j] == G) count=dp[i][j]; 
		}
		//cout << endl;
	}
	return count;
}

int main(){
	int W, H ,N, count=0;
	//Input
	cin >> H >> W >> N;
	vector<vector<char> > rect(H, vector<char>(W));
	for (int i = 0; i<H; i++){
		for (int j = 0; j<W; j++){
			cin >> rect[i][j];
		}
	}
	//Processing
	count+=trvrs('S','1',rect);
	for(int i=1;i<N;i++){
		count+=trvrs(i+'0',i+1+'0',rect);
	}
	//Output
	for (int i = 0; i<H; i++){
		for (int j = 0; j<W; j++){
			//cout << rect[i][j];
		}
		//cout << endl;
	}
	cout << count << endl;
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

struct OZAWA
{
	int x;
	int y;
	int c;
};
int Flag[1000][1000];
char MAP[1000][1000];
queue<OZAWA> Q;
int main(void)
{
	int H,W,N,Nc=0,X[10],Y[10];
	OZAWA ba,v;
	int mv[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
	char Cheese[10] ={'1','2','3','4','5','6','7','8','9'};
	memset(MAP,'X',sizeof(MAP));
	memset(Flag,0,sizeof(Flag));
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			//printf("%d %d ",i,j);
			cin >> MAP[i][j];
			//printf("Q");
			if(MAP[i][j] == 'S'){
				ba.x = j;
				ba.y = i;
				ba.c = 0;
				Flag[i][j] = 1;
				Q.push(ba);
				//printf("Sin\n");
			}
			for(int ii=0;ii<9;ii++){
				if(MAP[i][j] == Cheese[ii]){

					X[ii] = j;
					Y[ii] = i;
					//printf("\n@@@\n");
				}
			}
		}
	}
	//printf("S %d %d\n",ba.x,ba.y);
	//for(int ii=0;ii<2;ii++)
	//printf("%d %d\n",X[0],Y[0]);
	while(!Q.empty()){
		//printf("QA\n");
		ba = Q.front();
		Q.pop();
		if(ba.x==X[Nc] && ba.y==Y[Nc]){
			//printf("OZA");
			Nc++;
			memset(Flag,0,sizeof(Flag));
			while(!Q.empty()){
				Q.pop();
			}
			Q.push(ba);
		}
		//printf("aaa\n");
		if(Nc == N) break;
		for(int i=0;i<4;i++){
			if(MAP[ba.y+mv[i][1]][ba.x+mv[i][0]]!='X' && Flag[ba.y+mv[i][1]][ba.x+mv[i][0]]==0 && (ba.x+mv[i][0])>=0&&(ba.x+mv[i][0])<W&&(ba.y+mv[i][1])>=0&&(ba.y+mv[i][1])<H){
				v.x=ba.x+mv[i][0];
				v.y=ba.y+mv[i][1];
				v.c=ba.c+1;
				Q.push(v);
				Flag[v.y][v.x] = 1;
				//printf("%d %d\n",v.x,v.y);
			}
		}
	}
	cout <<ba.c<<endl
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
const int INF = 100000000;
typedef pair<int,int> Point;
Point AllPoint[1000];
int W,H;
int x[4] = {0,1,0,-1};
int y[4] = {1,0,-1,0};
char map[1000][1000];
int deep[1000][1000];
int N;
void BFS(Point Start,Point Over){
    queue<Point> que;
    int FirstDeep = deep[Start.first][Start.second];
    for (int i = 0;i<H;i++){
        for (int j = 0;j<W;j++){
            deep[i][j] = INF;
        }
    }
    que.push(Start);
    deep[Start.first][Start.second] = FirstDeep;
    while (que.size()){
        Point p = que.front();
        que.pop();
        if (p.first== Over.first&&p.second == Over.second)
            break;
        for (int i = 0;i<4;i++){
            int ix = p.first+x[i];
            int iy = p.second+y[i];
            if (ix>=0&&ix<=H
                &&iy>=0&&iy<=W
                &&deep[ix][iy] == INF
                &&map[ix][iy]!='X'){
                que.push(Point(ix,iy));
                deep[ix][iy] = deep[p.first][p.second]+1;
            }
        }
    }
}



int main(){
    cin >> H >> W;
    cin >> N;
    for (int i = 0;i<H;i++){
        for (int j = 0;j<W;j++){
            cin >> map[i][j];
            if ((int)map[i][j]-48>=1&&(int)map[i][j]-48<=N){
                AllPoint[(int)map[i][j]-48].first = i;
                AllPoint[(int)map[i][j]-48].second = j;

            }
            if (map[i][j] == 'S'){
                AllPoint[0].first = i;
                AllPoint[0].second = j;
            }
        }
    }
    deep[AllPoint[0].first][AllPoint[0].second] = 0;
    for (int i = 0;i<N;i++){
        BFS(AllPoint[i],AllPoint[i+1]);
    }
    cout << deep[AllPoint[N].first][AllPoint[N].second]<<endl;

    return 0;
}
//#....
//S.##.
//###G.
//#####
//#####

//#S######.#
//......#..#
//.#.##.##.#
//.#........
//##.##.####
//....#....#
//.#######.#
//....#.....
//.####.###.
//....#...G#

C++: #include<iostream>
#include<queue>
#include<stdlib.h>
#include<pair.h>
using namespace std;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
const int INF=-1;
char plat[1005][1005];
int d[1005][1005];
typedef pair<int,int> p;
int H,W,N;
queue<p> cheese;
int bfs(int& sx,int& sy,char energy)
{
    d[sx][sy]=0;
    cheese.push(p(sx,sy));
    while(cheese.size()){
        p test=cheese.front();
        cheese.pop();
        if(plat[test.first][test.second]==energy){
            sx=test.first;sy=test.second;
            return d[test.first][test.second];
        }
        for(int i=0;i<4;++i){
            int nx=test.first+dx[i];int ny=test.second+dy[i];
            if(nx>=0&&nx<W&&ny>=0&&ny<H&&plat[nx][ny]!='X'&&d[nx][ny]==INF){
                cheese.push(p(nx,ny));
                d[nx][ny]=d[test.first][test.second]+1;
            }
        }

    }
}

int main()
{

    cin>>W>>H>>N;
    int sx,sy;
    for(int i=0;i<W;++i){
        for(int j=0;j<H;++j){
            char u;
            cin>>u;
            if(u=='S'){
                sx=i;sy=j;
            }
            plat[i][j]=u;
        }
    }
    char e='1';
    int ans=0;
    while(N--){
        memset(d,-1,sizeof(d));
        ans+=bfs(sx,sy,e++);
        while(cheese.size())cheese.pop();

    }
    cout<<ans<<endl;

}