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 <iostream>
#include <vector>
#include <math.h>
using namespace std;

int H, M, N;

int go[4][2];

bool K[1002][1002];

int score[1002][1002];

int C[11][2];

int ans = 0;

void saiki(int sx, int sy, int s){
  if(K[sx][sy]){
    if(score[sx][sy] > s){
      score[sx][sy] = s;
      for(int i = 0; i < 4; i++){
        saiki(sx + go[i][0], sy + go[i][1], s + 1);
      }
    }
  }
}

int main(){
  
  go[0][0] = 1;
  go[0][1] = 0;
  go[1][0] = 0;
  go[1][1] = -1;
  go[2][0] = -1;
  go[2][1] = 0;
  go[3][0] = 0;
  go[3][1] = 1;
  
  for(int i = 0; i <= H + 1; i++){
    for(int j = 0; j <= M + 1; j++){
      score[i][j] = 2147483640;
      K[i][j] = false;
    }
  }
  
  scanf("%d%d%d", &H, &M, &N);
  
  for(int i = 0; i < H; i++){
    char str[M + 1];
    scanf("%s", str);
    for(int j = 0; j < M; j++){
      if(str[j] == '.'){
        K[i + 1][j + 1] = true;
      }else if(str[j] == 'X'){
        K[i + 1][j + 1] = false;
      }else if(str[j] == 'S'){
        C[0][0] = i + 1;
        C[0][1] = j + 1;
        K[i + 1][j + 1] = true;
      }else{
        C[str[j] - '0'][0] = i + 1;
        C[str[j] - '0'][1] = j + 1;
        K[i + 1][j + 1] = true;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    for(int j = 1; j <= H; j++){
      for(int k = 1; k <= M; k++){
        if(K[j][k]){
          score[j][k] = 2147483640;
        }else{
          score[j][k] = -2147483640;
        }
      }
    }
    saiki(C[i + 1][0], C[i + 1][1], 0);
    int aa = score[C[i][0]][C[i][1]];
    //printf("%d\n", aa);
    ans += aa;
  }
  
  printf("%d\n", ans);
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const int MAXN=1000+20;
int dp[MAXN][MAXN];
int maze[MAXN][MAXN];
int h,w,n,sx[10],sy[10],temp;
void input(){
	memset(maze,-1,sizeof(maze));
	cin>>h>>w>>n;
	cin.get();
	for(int i=1;i<=h;++i){
		for(int j=1;j<=w;++j){
			char x;
			x=cin.get();
			if(x=='.') maze[i][j]=0;
			if(x=='X') maze[i][j]=-1;
			if(x>='0' && x<='9'){
				maze[i][j]=x-'0';
				sx[x-'0']=i;
				sy[x-'0']=j;
			}
			if(x=='S'){
				maze[i][j]=0;
				sx[0]=i;
				sy[0]=j;
			}
		}
		cin.get();
	}
//	for(int i=0;i<10;++i) cout<<sx[i]<<' '<<sy[i]<<endl;
/*	for(int i=1;i<=h;++i){
		for(int j=1;j<=w;++j){
			cout<<setw(4)<<maze[i][j];
		}
		cout<<endl;
	}*/
}
void bfs(int xx,int yy,int nn){
	queue <int> tx;
	queue <int> ty;
	tx.push(xx);
	ty.push(yy);
	while(tx.size() && ty.size()){
		int qx=tx.front(),qy=ty.front();
		tx.pop();
		ty.pop();
		for(int i=0;i<4;++i){
			int x=qx+dx[i],y=qy+dy[i];
			if(maze[x][y]==-1 || dp[x][y]>=0) continue;
			tx.push(x);
			ty.push(y);
			dp[x][y]=dp[qx][qy]+1;
			if(x==sx[nn] && y==sy[nn]){
				temp=dp[qx][qy];
//				cout<<temp<<endl;
				break;
			}
//			cout<<x<<' '<<y<<' '<<dp[x][y]<<endl;
		}
	}
//	cout<<endl;
}
void solve(){
	for(int i=0;i<n;++i){
//		cout<<temp<<endl;
		memset(dp,-1,sizeof(dp));
//		cout<<sx[i+1]<<' '<<sy[i+1]<<endl;
		dp[sx[i]][sy[i]]=temp;
		bfs(sx[i],sy[i],i+1);
	}
	cout<<dp[sx[n]][sy[n]]+n-1<<endl;
}
int main(){
	input();
	solve();
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 1005
using namespace std;

struct node {
	int i;
	int j;
	char val;
};

int H, W, N;//H(1 <= H <= 1000)、W(1 <= W <=1000)、N(1 <= N <= 9)
char m[MAXN][MAXN];
queue<node> q;

bool flag = 0;
int t1 = 0;
char flag_m[MAXN][MAXN];
void search(int i, int j, char val)
{
	if (flag_m[i][j]<val) {
		if (m[i][j] == 'X')
		{
			return;
		}
		if (m[i][j] == '.' || m[i][j] != val)
		{
			q.push({ i, j, val });
			//flag_m[i][j] = true;
			t1++;
		}
		if (m[i][j] == val)
		{
			if (val ==(char)('0' + N))
			{
				flag = 1;
			}
			else
			{
				q.push({ i, j, val + 1 });
				//flag_m[i][j] = true;
				m[i][j] = '.';
				t1++;
			}
		}
	}
	return;
}
int main()
{
	int si, sj;
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 1; i <= H; i++)
	{
		getchar();
		for (int j = 1; j <= W; j++)
		{
			scanf("%c", &(m[i][j]));
			if (m[i][j] == 'S')
			{
				si = i;
				sj = j;
			}
		}
	}

	//

	q.push({ si, sj, '1' });
	memset(flag_m, '0', sizeof(flag_m));
	int t = 1;
	int time = 0;

	while (!q.empty() && (!flag))
	{
		time++;
		t1 = 0;
		while (t-- && (!flag))
		{
			node n1 = q.front(); q.pop();
			si = n1.i; sj = n1.j;
			flag_m[si][sj] = n1.val;
			if (si > 1)
			{
				search(si - 1, sj, n1.val);
			}
			if (si < H)
			{
				search(si + 1, sj, n1.val);
			}
			if (sj > 1)
			{
				search(si, sj - 1, n1.val);
			}
			if (sj < W)
			{
				search(si, sj + 1, n1.val);
			}

		}
		t = t1;
	}
	printf("%d\n", time);
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int,int> P;

int bfs();
int INF=1000000000;
int h,w,n;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int sx,sy;
int good[1111][1111];
int ans=0;
char life='0';
char mapp[1111][1111];

int main(){
  cin >> h >> w >> n;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mapp[i][j];
      if(mapp[i][j] == 'S'){
	sy = i;
	sx = j;
      }
    }
  }
  for(int i=1;i<n+1;i++){
    life++;
    ans += bfs();
  }
  cout << ans << endl;
}


int bfs(){
  queue<P> que;
  que.push(P(sy,sx));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      good[i][j] = INF;
    }
  }
  good[sy][sx] = 0;
  
  while(que.size()){
    P p=que.front(); que.pop();
    if(mapp[p.first][p.second] == life){
      sy = p.first;
      sx = p.second;
      break;
    }
    for(int i=0;i<4;i++){
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if(0 <= nx && nx < w && 0 <= ny && ny < h && mapp[ny][nx] != 'X' && good[ny][nx] == INF){
	que.push(P(ny,nx));
	good[ny][nx] = good[p.first][p.second] + 1;
      }
    }
  }
  return good[sy][sx];
}