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<queue>
#include<stdio.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define SIZE 1010
using namespace std;

typedef pair<int,int> P;

char mass[SIZE][SIZE];
int  data[SIZE][SIZE];
int  dx[4]={1,0,-1,0};
int  dy[4]={0,1,0,-1};
int  sx,sy,gx,gy;
int  h,w,n;
int  k;
int  sum=0;

int bfs();

int main(){

  int i,j;

  cin>>h>>w>>n;

  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      cin>>mass[i][j];
    }
  }

  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      if(mass[i][j]=='S'){
	sx=j;
	sy=i;
	break;
      }
    }
  }

  for(k=1;k<=n;k++){
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(mass[i][j]-'0'==k){
	  gx=j;
	  gy=i;
	}
      }
    }
    sum+=bfs();
    sx=gx;
    sy=gy;
  }                      
  
  cout<<sum<<endl;
  
  return 0;
}
int bfs(){

  int i,j;
  queue<P> que;

  memset(data,-1,sizeof(data));

  que.push(P(sy,sx));
  data[sy][sx]=0;

  while(!que.empty()){

    P p=que.front();
    que.pop();

    if(p.first==gy && p.second==gx){
      return data[p.first][p.second];
    }

    for(i=0;i<4;i++){
      int nx=p.second+dx[i];
      int ny=p.first+dy[i];

      if(nx>=0 && nx<w && ny>=0 && ny<h && mass[ny][nx]!='X' && data[ny][nx]==-1){
	que.push(P(ny,nx));
	data[ny][nx]=data[p.first][p.second]+1;
    }
   }
  }
 return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct Dot{
	int x;int y;int t;
	Dot(int a,int b,int c):x(a),y(b),t(c) {}
};
int H,W,N;
char map[1000][1000];
int map_by[1000][1000];
int cheeses_x[10];
int cheeses_y[10];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int sx,sy;

void input(){
	cin >> H >> W >> N;
	for(int i = 0;i < H;++i){
		for(int j = 0;j < W;++j){
			cin >> map[i][j];
			if(map[i][j] == 'S'){
				sx = i;
				sy = j;
			}else if(map[i][j] >= '1' && map[i][j] <= '9'){
				int crt = map[i][j] - '0';
				cheeses_x[crt] = i;
				cheeses_y[crt] = j;
			}
		}
	}
}

int bfs(int s,int e){
	fill(map_by[0],map_by[0]+1000*1000,0);
	Dot st = Dot(cheeses_x[s],cheeses_y[s],0);
	queue<Dot> Q;
	Q.push(st);
	map_by[st.x][st.y] = 1;
	while(!Q.empty()){
		Dot d = Q.front();
		Q.pop();
		for(int i = 0;i < 4;++i){
			int cx = d.x+dx[i], cy = d.y+dy[i],ct = d.t+1;
			if(!(cx >= 0 && cx < H && cy >= 0 && cy < W && !map_by[cx][cy])) continue;
			if(map[cx][cy]-'0' == e) return ct;
			if(map[cx][cy] == '.' || (map[cx][cy] >= '1' && map[cx][cy] <= '9') || map[cx][cy] == 'S'){
				Q.push(Dot(cx,cy,ct));
				map_by[cx][cy] = 1;
			}
		}
	}
	return -1;
}

int main(){
	input();
	int res = 0;
	cheeses_x[0] = sx;cheeses_y[0] = sy;
	for(int i = 0;i < N;++i){
		int tmp = bfs(i,i+1);
	//	cout << tmp << endl;
		if(tmp == -1){
			res = -1;
			break;
		}
		res += tmp;
	}
	cout << res << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>

#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long

using namespace std;

typedef struct info{
	int nx;
	int ny;
	int cost;
	int target;
	info(int a,int b,int c,int d){
		nx = a;
		ny = b;
		cost = c;
		target = d;
	}
}info;

bool isNumChar(char c){
	if((0 <= c-48)&&(c-48 <= 9))return true;
	return false;
}

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

int W,H;
int N;

int sx[16] = {0};
int sy[16] = {0};

char field[1024][1024] = {""};
int record[1024][1024][12] = {0};
bool flag[16] = {false};

queue<info> que;


int ans;



int recordReset(){
	for(int i = 0;i < 1024;i++){
		for(int j = 0;j < 1024;j++){
			for(int k = 0;k < 16;k++){
				record[i][j][k] = INF;
			}
		}
	}
}

int bfs(){
	info now(0,0,0,0);
	info next(0,0,0,0);
	for(int i = 0;i < N;i++){
		next = info(sx[i],sy[i],0,i+1);
		que.push(next);
	}

	recordReset();
	while(!que.empty()){
		now = que.front();
		que.pop();
		if(!flag[now.target]){
			//printf("###(%d,%d):::cost<%d>:::target<%d>\n",now.nx,now.ny,now.cost,now.target);
			if(record[now.nx][now.ny][now.target] > now.cost){
				record[now.nx][now.ny][now.target] = now.cost;
				if((sx[now.target] == now.nx)&&(sy[now.target] == now.ny)){
					flag[now.target] = true;
					//printf("###target<%d> completed\n",now.target);
					ans += record[now.nx][now.ny][now.target];
				}
				else{
					switch(field[now.nx][now.ny]){
					case 'X':
						break;
					default:
						for(int i = 0;i < 4;i++){
							next = info(now.nx + dx[i],now.ny + dy[i],now.cost + 1,now.target);
							que.push(next);
						}
						break;
					}
				}
			}
		}
	}
}


int main (void){
	//while(true){
		scanf("%d%d",&H,&W);
		scanf("%d",&N);
		//if((W == 0)&&(H == 0))break;

		ans = 0;

		//Reset Field
		for(int i = 0;i < 1024;i++){
			for(int j = 0;j < 1024;j++){
				field[j][i] = 'X';
			}
		}

		//Reset Flag
		for(int i = 0;i < 16;i++){
			flag[i] = false;
		}

		//Reading Data
		for(int i = 1;i <= H;i++){
			scanf(" ");
			for(int j = 1;j <= W;j++){
				scanf("%c",&field[j][i]);
				if(isNumChar(field[j][i])){
					sx[field[j][i]-48] = j;
					sy[field[j][i]-48] = i;
					//printf("###<%d>(%d,%d)\n",field[j][i] - 48,sx[field[j][i] - 48],sy[field[j][i] - 48]);

				}
				else if(field[j][i] == 'S'){
					sx[0] = j;
					sy[0] = i;
					//printf("###<S>(%d,%d)\n",j,i);
				}
			}
		}

		//DBG
		/*for(int i = 0;i < N;i++){
			printf("<%d>(%d,%d)\n",i,sx[i],sy[i]);
		}*/

		//bfs();

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

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<cstdio>
#include<map>
#pragma warning(disable : 4996)
#define P pair<int,int>
using namespace std;

bool g[1000][1000]{};
int x[] = { -1,0,0,1 }, y[] = { 0,-1,1,0 };
map<int, P>j;
int a, b, c;
int meiro(int d) {
	queue<P> e;
	vector<vector<int>>f(a, vector<int>(b));
	for (int g = 0; g < a; g++) {
		for (int h = 0; h < b; h++) {
			f[g][h] = 1 << 29;
		}
	}
	e.push(j[d]);
	f[(j[d]).first][(j[d]).second] = 0;
	while(e.size()) {
		P p = e.front(); e.pop();
		if (p == j[d+1])break;
		for (int q = 0; q < 4; q++) {
			int l = p.first + x[q];
			int r = p.second + y[q];
			if (l >= 0 && l < a&&r >= 0 && r < b&&g[l][r] && f[l][r] == 1 << 29) {
				P w(l, r);
				e.push(w);
				f[l][r] = f[p.first][p.second] + 1;
			}
		}
	}
	return f[(j[d + 1]).first][(j[d + 1]).second];
}
int main() {
	scanf("%d%d%d", &a, &b, &c);
	for (int d = 0; d < a; d++) {
		for (int e = 0; e < b; e++) {
			char f;
			cin >> f;
			if (f == 'X')g[d][e] = false;
			else if (f == '.')g[d][e] = true;
			else if (f == 'S') {
				g[d][e] = true;
				P h(d, e);
				j[0] = h;
			}
			else {
				g[d][e] = true;
				P h(d, e);
				j[f - 48] = h;
			}
		}
	}
	int s = 0;
	for (int l = 0; l < c; l++) {
		s += meiro(l);
	}
	printf("%d\n", s);
}