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 <string>
#include <cstdlib>
#include <algorithm>
//#include <functional>
#include <cstdio>
#include <queue>
#include <fstream>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point
{
	int x;
	int y;
};

int dx[] = {0,-1,0,1};//ã©çvñè
int dy[] = {1,0,-1,0};
string field[1000];
int mfield[1001][1001] = {};
int life=1;
int csum = 0;
int H,W,N;


int open(Point gool)
{
	int counter=0;
	queue<Point> tp;
	tp.push(gool);
	int i,j;
	Point inp;
	Point p;
	while(1)
	{
		counter++;
		int size = tp.size();
		for(i=0;i<size;i++)
		{
			p = tp.front();
			tp.pop();
			for(j=0;j<4;j++)
			{
				inp.x = p.x+dx[j];
				inp.y = p.y+dy[j];
				if(inp.x<0 || inp.y<0 || inp.y>=H || inp.x>=W)
					continue;
				if(field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life)
				{
					//v·éÉÅZ£Ú®Â\][
					if(field[inp.y][inp.x] == life + '0' ) //©ªÉ©Á½`[YÌ­©
					{
						return counter;
					}
					else
					{
						tp.push(inp);
						mfield[inp.y][inp.x] = life;
					}
				}
			}
		}
		/*for(i=0;i<H;i++)
		{
			for(j=0;j<W;j++)
			{
				cout << mfield[i][j];
			}
			cout << endl;
		}*/
	}
	//copy(tp.begin() , tp.end() , tp.begin());
	return 0;
}

int main(void)
{
	int i,j;
	queue<Point> tp;
	Point top[10];
	Point inp;
	int count = 0;
	int counter=0;
	cin >> H >> W >> N;

	for(i=0;i<H;i++)//üÍ
	{
		cin >> field[i];
		for(j=0;j<W;j++)
		{
			if(field[i][j] == 'S')
			{
				top[0].y = i;
				top[0].x = j;
				mfield[i][j] = life;
			}
			if(field[i][j] >= '0' && field[i][j] <= '9')
			{
				top[field[i][j] - '0'].y = i;
				top[field[i][j] - '0'].x = j;
			}
		}
	}//üÍIíè

	while(1)
	{
		if((counter = open(top[life-1])) != 0)
		{
			life++;
			csum += counter;
		}
		if(life > N)
			break;
	}
		cout << csum << endl;

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define sor(v) sort(v.begin(),v.end())
#define min_3(a,b,c) min(a,min(b,c))
#define max_3(a,b,c) max(a,max(b,c))
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dre_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dre_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

char c[1010][1010];

int bfs(P s,P g){
	queue<P1> que;
	bool used[1010][1010]; rep(i,1010)rep(j,1010)used[i][j]=false;
	
	que.push(P1(0,s));
	while(!que.empty()){
		P1 p=que.front(); que.pop();
		if(p.sc==g)return p.fr;
		if(!used[p.sc.fr][p.sc.sc]){
			rep(i,4){
				int nx=p.sc.fr+dre_4[i][0];
				int ny=p.sc.sc+dre_4[i][1];
				
				if(c[nx][ny]!='X'){
					que.push(mp1(p.fr+1,nx,ny));
				}
			}
			used[p.sc.fr][p.sc.sc]=true;
		}
	}
	
	return INF;
}

int main(){
	int h,w,n;
	cin>>h>>w>>n;
	
	rep(i,1010)rep(j,1010)c[i][j]='X';
	rep1(i,h){
		scanf("\n");
		rep1(j,w){
			cin>>c[i][j];
		}
	}
	
	int x[12],y[12];
	
	rep1(i,h)rep1(j,w){
		if(c[i][j]=='S'){
			x[0]=i;
			y[0]=j;
		}
		rep1(k,n){
			if(c[i][j]=='0'+k){
				x[k]=i;
				y[k]=j;
			}
		}
	}
	
	int ret=0;
	rep1(i,n){
		ret+=bfs(P(x[i-1],y[i-1]),P(x[i],y[i]));
	}
	
	cout<<ret<<endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
 
typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;
mouse Q[10000];
int s,num;
 
mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n;
int cost;
 
main(){
  scanf("%d%d%d ",&h,&w,&n);
  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h)fread(ku[i],1,w+1,stdin);
 
 
  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }
 
 
  Q[s+num++]=(mouse){cx,cy,0,0};
  maxch[cx][cy]=0;
 
  while(true){
    t=Q[s++];
    num--;
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;
 
    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
      if(ku[nx][ny]=='X')continue;
      nch=ch;
      if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
        nch=ch+1;
      }
 
      if(maxch[nx][ny]>=nch)continue;
      if(nch==n){
        printf("%d\n",cost+1);
        return 0;
      }
      maxch[nx][ny]=nch;
      Q[s+num++]=(mouse){nx,ny,cost+1,nch};
    }
  }
}