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++: /*
Á·Ï°Ìâ£ºCheese_AOJ 0558 
*/

#include <bits/stdc++.h>
using namespace std;

const int INF=100000000;
const int MAX=1000;

typedef pair<int, int> P;
int N, M;
int sx, sy;
int gx[10], gy[10];
char maze[MAX][MAX];
//ÉèÖÃÊý×é£¬±íÊ¾µ½ÆðµãµÄ×î½ü¾àÀë 
int d[MAX][MAX];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int bfs(int g)
{
	queue<P> que;
	for(int i=0; i<N; i++)
		for(int j=0; j<M; j++)
			d[i][j]=INF;
	//ÏÈ°ÑÆðµã·Å½øÈ¥£¬¾àÀëÉèÖÃÎªÁã 
	que.push(P(sx, sy));
	d[sx][sy]=0;
	
	//Ö±µ½¶ÓÁÐÃ»ÓÐÔªËØ£¬¾ÍÎÞÂ·¿É×ßÁË 
	while(que.size())
	{
		P p = que.front();
		que.pop();
		if(p.first==gx[g]&&p.second==gy[g])
			break;
		
		for(int i=0; i<4; i++)
		{
			int nx=p.first+dx[i];
			int ny=p.second+dy[i];
			if(nx>=0&&nx<N&&ny>=0&&ny<M&&maze[nx][ny]!='X'&&d[nx][ny]==INF)
			{
				que.push(P(nx,ny));
				d[nx][ny] = d[p.first][p.second]+1;
			}
		}	
	}
	return d[gx[g]][gy[g]];
}

int main()
{
	int gn;
	scanf("%d%d%d", &N, &M, &gn);
	getchar();
	for(int i=0; i<N; i++)
	{
		for(int j=0; j<M; j++)
			scanf("%c", &maze[i][j]);
		getchar();
	}
	for(int i=0; i<N; i++)
		for(int j=0; j<M; j++)
		{
			if(maze[i][j]=='S')
			{
				sx=i;
				sy=j;
			}
			if(maze[i][j]=='1')
			{
				gx[1]=i;
				gy[1]=j;
			}
			if(maze[i][j]=='2')
			{
				gx[2]=i;
				gy[2]=j;
			}
			if(maze[i][j]=='3')
			{
				gx[3]=i;
				gy[3]=j;
			}
			if(maze[i][j]=='4')
			{
				gx[4]=i;
				gy[4]=j;
			}
			if(maze[i][j]=='5')
			{
				gx[5]=i;
				gy[5]=j;
			}
			if(maze[i][j]=='6')
			{
				gx[6]=i;
				gy[6]=j;
			}
			if(maze[i][j]=='7')
			{
				gx[7]=i;
				gy[7]=j;
			}
			if(maze[i][j]=='8')
			{
				gx[8]=i;
				gy[8]=j;
			}
			if(maze[i][j]=='9')
			{
				gx[9]=i;
				gy[9]=j;
			}
		}
	
	int asw = bfs(1);
	for(int i=1; i<gn; i++)
	{
		sx=gx[i]; sy=gy[i];
		asw += bfs(i+1);
	}
	printf("%d\n", asw);
	
	
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

#define MAX 1000
int y[MAX][MAX];
int x[MAX][MAX];
int px[10];
int py[10];
int main(){
	memset(y,127,sizeof(y));
	char T[12]={'.','1','2','3','4','5','6','7','8','9','S','X'};
	int n,m,c,d,sum;
	string S;
	sum=0;
	cin>>n>>m>>c;
	for(int i=0;i<n;i++){
		cin>>S;
		for(int j=0;j<m;j++){
			for(int k=0;k<12;k++){
				if(S[j]==T[k]){
					x[i][j]=k;
					if(k==10){
						px[0]=i;
						py[0]=j;
					}
					if(k>=1 && k<=9){
						px[k]=i;
						py[k]=j;
					}
				}
			}
		}
	}
	for(int i=1;i<=c;i++){
		memset(y,127,sizeof(y));
		y[px[i-1]][py[i-1]]=0;
		for(int j=0;j<n*m/4+2;j++){
			for(int k=0;k<n;k++){
				for(int l=0;l<m;l++){
					if(k>=1){
						if(x[k-1][l]!=11){
							y[k-1][l]=min(y[k-1][l],y[k][l]+1);
						}
					}
					if(l>=1){
						if(x[k][l-1]!=11){
							y[k][l-1]=min(y[k][l-1],y[k][l]+1);
						}
					}
					if(x[k+1][l]!=11){
						y[k+1][l]=min(y[k+1][l],y[k][l]+1);
					}
					if(x[k][l+1]!=11){
						y[k][l+1]=min(y[k][l+1],y[k][l]+1);
					}
				}
			}
			if(y[px[i]][py[i]]<10000000){
				goto Exit;
			}
		}
		Exit:;
		sum += y[px[i]][py[i]];
	}
	cout<<sum<<endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int INF=1000000;
int d[1005][1005];
int dx[4]= {1,0,-1,0};
int dy[4]= {0,1,0,-1};
char s[1005][1005];
int m,n,k;
//int sx,sy,gx,gy;
typedef pair <int,int >P;
P aim[15];
int isd(char ch)
{
    return ch>='0'&&ch<='9'?1:0;
}
int bfs(int sx,int sy,int gx,int gy)
{
    queue<P> que;
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            d[i][j]=INF;
//            cout <<d[i][j]<<endl;
        }
    }
    que.push(P(sx,sy));
    d[sx][sy]=0;
    while(que.size())
    {

        P p=que.front();
        que.pop();
        if(p.first==gx&&p.second==gy)
        {
            break;
        }
        for(int i=0; i<4; i++)
        {
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
//            cout << nx << ":" << ny << endl;
//            cout << que.size()<<" "<<m<<" "<<n<<endl;
            if( nx>=0&&nx<m
                    &&ny>=0&&ny<n
                    &&s[nx][ny]!='X'
                    &&d[nx][ny]==INF)
            {

                que.push(P(nx,ny));

                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
    }
    return d[gx][gy];
}
int main()
{
    cin >> m >> n >> k;
    int sum = 0;
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin >> s[i][j];
            if(s[i][j]=='S')
            {
                aim[0].first=i;
                aim[0].second=j;
//                cout << i <<" "<< j <<endl;
            }
            if(isd(s[i][j]))
            {
                aim[s[i][j]-'0'].first=i;
                aim[s[i][j]-'0'].second=j;
//                cout << i << " "<<j <<endl;
            }

        }


    }
    for(int i=0; i<k; i++)
    {
        int steep=bfs(aim[i].first,aim[i].second,aim[i+1].first,aim[i+1].second);
        sum+=steep;
//            cout << steep <<endl;
    }
    cout << sum <<endl ;
}