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<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int n,m,l,cnt,mx,my;
char a[1009][1009];
int vis[1009][1009];
pair<int,int> p[10],st;
int mov[4][2] = {1,0,0,-1,-1,0,0,1};
struct status
{
    int x,y;
    int ord;
    int fx;
    status(int a,int b,int c,int s)
    {
        x = a, y = b, ord = c, fx = s;
    }
    bool operator < (struct status t) const
    {
        if(ord != t.ord) return ord > t.ord;
        else return fx > t.fx;
    }
};

void bfs(int x,int y,int color)
{
    priority_queue <struct status> q;
    struct status temp(x,y,0,fabs(x-p[color].first) + fabs(y - p[color].second));
    q.push(temp);
    int flag = 0;
    while(!q.empty())
    {
        struct status nw = q.top();
        q.pop();
        if(nw.x == p[color].first && nw.y == p[color].second)
        {
            cnt += nw.ord;
            break;
        }
        vis[nw.x][nw.y] = 1;
        for(int i=0;i<4;i++)
        {
            int tx = mov[i][0] + nw.x, ty = nw.y + mov[i][1];
            if(tx>=0 && tx< n && ty>=0 && ty< m && !vis[tx][ty] && a[tx][ty] != 'X')
            {
                struct status tmp(tx,ty,nw.ord+1,fabs(tx-p[color].first) + fabs(ty - p[color].second));
                q.push(tmp);
                vis[tx][ty] = 1;
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&l))
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]>='0' && a[i][j]<='9')
                    p[a[i][j]-'0'].first = i, p[a[i][j]-'0'].second = j;
                if(a[i][j] == 'S') st.first = i, st.second = j;
            }
            getchar();
        }

        int sx = st.first, sy = st.second;
        cnt = 0;
        for(int i=1;i<=l;i++)
        {
            memset(vis,0,sizeof(vis));
            bfs(sx,sy,i);
            sx = p[i].first, sy = p[i].second;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

const int INF = 1<<29;
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int H, W, N;
int D[16][16];
string town[1024];
int dist[1024][1024];
P factory[16];
bool cheese[16];

bool in(int r, int c) {
  return 0 <= r && r < H && 0 <= c && c < W;
}

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  fill(cheese, cheese + 10, false);
  REP(i, 10) REP(j, 10) D[i][j] = INF;
  cin >> H >> W >> N;
  REP(i, H) cin >> town[i];
  REP(i, H) REP(j, W) {
    if (town[i][j] == 'S') {
      factory[0] = P(i, j);
      cheese[0] = true;
    } else if (town[i][j] != '.' && town[i][j] != 'X') {
      factory[town[i][j] - '0'] = P(i, j);
      cheese[town[i][j] - '0'] = true;
    }
  }
  assert(cheese[0] && cheese[1]);

  REP(i, 10) {
    if (!cheese[i]) continue;
    REP(r, H) REP(c, W) dist[r][c] = -1;
    int sr = factory[i].first, sc = factory[i].second;
    queue<T> que;
    que.push(T(sr, sc, 0));
    while (!que.empty()) {
      int r, c, d;
      tie(r, c, d) = que.front(); que.pop();
      if (dist[r][c] != -1) continue;
      dist[r][c] = d;
      if (town[r][c] == 'S') {
	D[i][0] = d;
      } else if (town[r][c] != '.' && town[r][c] != 'X') {
	D[i][town[r][c] - '0'] = d;
      }
      REP(j, 4) {
	int nr = r + dr[j], nc = c + dc[j];
	if (in(nr, nc) && town[nr][nc] != 'X' && dist[nr][nc] == -1) {
	  que.push(T(nr, nc, d+1));
	}
      }
    }
  }

  int ans = 0;
  REP(i, N) ans += D[i][i+1];
  cout << ans << endl;      
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;

class DAT{
public:
  int x;
  int y;
  int total;
  bool operator<(const DAT &A)const{
    return A.total < total;
  }
};

int main(void){
  int h,w,n;
  cin>>h>>w>>n;
  vector<string> field(h);
  map<int,pair<int,int> > ps;
  for(int i=0;i<h;++i){
    int j;
    cin>>field[i];
    if((j = field[i].find('S')) != string::npos)
      ps[0] = make_pair(j,i);
    for(char c='1';c<='9';++c)
      if((j = field[i].find(c)) != string::npos)
        ps[c-'0'] = make_pair(j,i);
  }
  int ans=0;
  for(int i=0;i<n;++i){
    int sx = ps[i].first, sy = ps[i].second;
    vector<vector<int> > dist(h);
    for(int j=0;j<h;++j){
    	dist[j]=vector<int>(w);
    	for(int k=0;k<w;++k){
    		dist[j][k]=1e9;
    	}
    }
    priority_queue<DAT> Q;
    DAT start;
    start.x = sx; start.y = sy; start.total = 0;
    Q.push(start);
    int sum = 0;
    while(!Q.empty()){
      DAT dat = Q.top();Q.pop();
      if(dist[dat.y][dat.x] < dat.total) continue;
      if(dat.x == ps[i+1].first && dat.y == ps[i+1].second){sum=dat.total;break;}
      if(field[dat.y][dat.x] == '.' || field[dat.y][dat.x] == 'S' || ('1'<=field[dat.y][dat.x] && field[dat.y][dat.x] <= '9')){
        dist[dat.y][dat.x] = dat.total;
        if(dat.x>0){
          DAT nxt;
          nxt.x=dat.x-1;nxt.y=dat.y;nxt.total=dat.total+1;
          Q.push(nxt);
        }
        if(dat.y>0){
          DAT nxt;
          nxt.x=dat.x;nxt.y=dat.y-1;nxt.total=dat.total+1;
          Q.push(nxt);
        }
        if(dat.x<w-1){
          DAT nxt;
          nxt.x=dat.x+1;nxt.y=dat.y;nxt.total=dat.total+1;
          Q.push(nxt);
        }
        if(dat.y<h-1){
          DAT nxt;
          nxt.x=dat.x;nxt.y=dat.y+1;nxt.total=dat.total+1;
          Q.push(nxt);
        }
      }
    }
    ans+=sum;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <stdio.h>

int main() {
    int i, j;
    int h, w, n;

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

            scanf(" %c", &c);
            if (c == 'S')