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;
string g[1000];

int bfs(int cheese);

int main(){
  cin >> h >> w >> n;
  for(int i=0;i<h;i++)cin >> g[i];

  int ans = 0;
  for(int i=0;i<n;i++){
    ans += bfs(i);
  }
  cout << ans << endl;
}

int dis[1000][1000];
int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};

int bfs(int cheese){
  char start = (cheese==0)?'S':('0'+cheese), goal = '1'+cheese;

  queue<int> qy,qx;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      dis[i][j] = -1;
      if(g[i][j] == start){
	dis[i][j] = 0;
	qy.push(i); qx.push(j);
      }
    }
  }

  int res = 0;
  while(qy.size()){
    int y = qy.front(), x = qx.front();
    qy.pop(); qx.pop();
    if(g[y][x] == goal)return dis[y][x];

    for(int i=0;i<4;i++){
      int ny = y+dy[i], nx = x+dx[i];
      if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
      if(g[ny][nx] == 'X')continue;
      
      if(dis[ny][nx]<0){
	dis[ny][nx] = dis[y][x] + 1;
	qy.push(ny); qx.push(nx);
      }
    }
  }
  return -1;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX = 1200;
int H,W,N;
int sx,sy,res;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};//4个?找方向
char map[MAX][MAX];
int d[MAX][MAX];//??最短路的数?
typedef pair<int,int> P;
void BFS(int x,int y,char m)
{
    queue<P>  que;
    //初始状?入?
    que.push(P(x,y));
    d[x][y]=0;
    while(que.size())
    {
        //从?列最前端取出状?
        P p = que.front();
        que.pop();
        //若到?m点?束?找
        if(map[p.first][p.second]==m){
            sx=p.first;
            sy=p.second;
            return;
        }

        //四个方向循?
        for(int i=0;i<4;i++)
        {
            //?算移?后的位置
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
            if(nx>=0&&nx<H&&ny>=0&&ny<W&&map[nx][ny]!='X'&&d[nx][ny]==-1)
            {
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
    }

}
void solve()
{
    for(int i=1;i<=N;i++)
    {
        memset(d,-1,sizeof(d));
        BFS(sx,sy,i+48);
        res+=d[sx][sy];
    }
    printf("%d\n",res);
}
int main()
{
    scanf("%d%d%d",&H,&W,&N);
    for(int i=0;i<H;i++)
        scanf("%s",map[i]);

    for(int i=0;i<H;i++)
    for(int j=0;j<W;j++)
    {
        if(map[i][j]=='S')
        {
            sx=i;sy=j;
            i=H;j=W;
        }
    }
    solve();
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define scai(x) scanf("%d", &x)
#define scac(x) scanf("%c", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define pri(x) printf("%d", x)
#define prc(x) printf("%c", x)
#define pri2(x,y) printf("%d %d", x, y);
#define prif(x) printf("%lf", x)
#define prif2(x, y) printf("%lf %lf", x, y)
#define mset(x, y) memset(x, y, sizeof(x))
#define fori(i, x, y) for(int i = x; i < y; i++)
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
typedef long long ll;
#define INF 0x3f3f3f3f
#define MAX_N  1003
char m[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
//int dp[MAX_N];

int H, W, N;

int hm[4] = {-1, 1, 0, 0};
int wm[4] = {0, 0, -1, 1};
int ans = 0;

void solve(int h, int w)
{
    for(int i = 1; i <= H; i++)
        for(int j = 1; j <= W; j++)
            dp[i][j] = INF;
    dp[h][w] = 0;
    bool flag = 0;
    m[h][w] = '.';
    int t = 0;
    pii sx;
    sx.first = h;
    sx.second = w;

    ans = 0;
    queue<pii> que;
    que.push(sx);
    int n = 0;
    int NN = 1;
    while(!que.empty())
    {

        flag = 0;

       // printf("que is front is %d %d\n", que.front().first, que.front().second);
        for(int i = 0; i < 4; i++)
        {

                pii tmp;
                tmp.first = que.front().first + hm[i];
                tmp.second = que.front().second + wm[i];

                //printf("BFS is at %d %d %c\n", tmp.first, tmp.second, m[tmp.first][tmp.second]);

                if(1 <= tmp.first && tmp.first <= H && 0 < tmp.second && tmp.second <= W && m[tmp.first][tmp.second] != 'X' && dp[tmp.first][tmp.second] == INF)
                {
                    //----------------------------------------------------------
                    //printf("进入第一层if判断，基本条件成立\n");

                    if(m[tmp.first][tmp.second] == '.')
                    {
                        que.push(tmp);
                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        //printf("BFS is at %d %d\n", tmp.first, tmp.second);
                        //t++;
                    }
                    else if(m[tmp.first][tmp.second] - '0' <= NN)
                    {
                        NN++;

                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        ans = dp[tmp.first][tmp.second];
                        //printf("这次的步数是: %d 到的位置是%d %d 里面是%c NN 是%d\n", ans, tmp.first, tmp.second,m[tmp.first][tmp.second], NN);
                        m[tmp.first][tmp.second] = '.';
                        for(int ii = 1; ii <= H; ii++)
                            for(int jj = 1; jj <= W; jj++)
                                    dp[ii][jj] = INF;
                        dp[tmp.first][tmp.second] = ans;
                        while(!que.empty())
                        {
                            que.pop();
                            //t++;

                        }
                        flag = 1;
                        que.push(tmp);
                    }
                    else
                    {
                        que.push(tmp);
                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        //t++;
                    }
                }
        }
        if(!flag)
            que.pop();
    }
    /*for(int i = 1; i <= H; i++){
            puts("");
        for(int j = 1; j <= W; j++)
            {
                printf("%c", m[i][j]);
            }
    }*/
    pri(ans);
    puts("");
}
int main()
{
    scanf("%d %d %d", &H, &W, &N);
    for(int i = 1; i <= H; i++)
    {
        getchar();
        for (int j = 1; j <= W; j++)
        {
            scac(m[i][j]);
        }
    }
    for(int i = 1; i <= H; i++)
    {
        //getchar();
        for (int j = 1; j <= W; j++)
        {
            if (m[i][j] == 'S')
            {
                solve(i, j);
                break;
            }
        }
    }
    //system("pause");
    return 0;

}