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>
using namespace std;
int ans = 0;
int gh=0, gw=0;
const int max = 500000;
char c[1000][1000];
int d[1000][1000];
int mh[4] = { 1,0,-1,0 };
int mw[4] = { 0,1,0,-1 };
char hijou[9] = { '1','2','3','4','5','6','7','8','9' };
int  judge(int h, int w,int g) {
	if (c[h][w] ==hijou[g])
	{ 	gh = h;
	gw = w; 
	return d[h][w];
	}
	for (int i = 0; i < 4; i++) {
		int nh = h + mh[i];
		int nw = w + mh[i];
		if (nh >= 0 && nh < 1000 && nw>=0 && nh < 1000 && d[nh][nw] == max&&c[nh][nw] != 'X') {
			d[nh][nw] = d[h][w] + 1;
			judge(nh, nw,g);
		}
	}
}
int main() {
	int h, w, n,sh,sw;
	for (int i = 0; i < 1000;i++){
		for (int j = 0; j < 1000; j++)
		{
			c[i][j] = 'X'; 
		}
	}
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w;j++){
			cin >> c[i][j];
			if (c[i][j] == 'S') { sh = i; sw = j; }
		}
	}cout << sh << sw;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 1000; j++) {
			for (int k = 0; k < 1000; k++)
				d[j][k] = max;
		}
	
		d[sh][sw] = 0;
		ans+=judge(sh, sw, i);
		sh = gh; sw = gw;
}
	cout << ans << endl;
	
}

C++: #include <cstdio>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int INF = 100000000;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W]; // ?????????????????\??´?????§??????????????°, 1~N??§?????????????????§????????°??????+1
int sx, sy, gx, gy;
int power = 1; // ??????????????????
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

typedef pair<int, int> P;

void reset_d() {
  for (int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) d[i][j] = INF;
}

int bfs(int c) {
  queue<P> que;
  reset_d();
  d[sx][sy] = c;
  que.push(P(sx, sy));
  while(que.size()) {
    P p = que.front(); que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if ( 0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' && d[nx][ny] == INF) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
        if (field[nx][ny] - '0' == power) {
          sx = nx;
          sy = ny;
          power++;
          return d[nx][ny];
        }
      }
    }
  }
  return INF;
}

void solve() {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c = bfs(c);
  }
  printf("%d", c);
}


int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", field[i]);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (field[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }
  solve();
  return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
int main(void)
{
	char p[2];
	queue<pair<int,int> > q;
	pair<int,int> g,s,w;
	int x,y,n,m,a=0,f[1002][1002],l=1,l2;
	scanf("%d %d %d",&x,&y,&l2);
	for(n=0;n<=1002;n++){
		for(m=0;m<=1002;m++){
			if(n==0||n>x||m==0||m>y){
				f[n][m]=10;
			}
			else{
				scanf("%c",&p);
				if(p[1]=='S'){
					f[n][m]=0;
					s.first=n;
					s.second=m;
					q.push(s);
				}
				else if(p[1]=='X'){
					f[n][m]=10;
				}
				else if(p[1]=='.'){
					f[n][m]=0;
				}
				else{
					f[n][m]=p[1];
				}
			}
		}
	}
	while(!q.empty()){
		s=q.front();
		q.pop();
		if(f[s.first+1][s.second]<=l){
			w.first=s.first+1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second+1]<=l){
			w.first=s.first;
			w.second=s.second+1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first-1][s.second]<=l){
			w.first=s.first-1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second-1<=l]){
			w.first=s.first;
			w.second=s.second-1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int h,w,k;
pair<int,int> p[12];
bool t[1010][1010],vis[1010][1010];
queue<pair<int,pair<int,int> > >Q;
int main(){
	cin>>h>>w>>k;
	rep(i,0,h)rep(j,0,w){
		t[i][j]=true;
		char z;cin>>z;
		if(z=='X')t[i][j]=0;
		if(z=='S')p[0]=make_pair(i,j);
		if(z>'0'&&z<='9')p[z-'0']=make_pair(i,j);
	}
	int a=0;
	rep(r,0,k){
		Q.push(make_pair(0,p[r]));
		rep(i,0,h)rep(j,0,w)vis[i][j]=0;vis[p[r].x][p[r].y]=1;
		while(1){
			int L=Q.front().x,Y=Q.front().y.x,X=Q.front().y.y;
			if(Y==p[r+1].x&&X==p[r+1].y){
				a+=L;
				break;
			}
			int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
			rep(i,0,4){
				int f=Y+dy[i],g=X+dx[i];
				if(f>=0&&f<h&&g>=0&&g<w){
					if(t[f][g]==1&&vis[f][g]==0){
						vis[Y][X]=1;
						Q.push(make_pair(L+1,make_pair(f,g)));
					}
				}
			}
			Q.pop();
		}
		while(!Q.empty())Q.pop();
	}
	cout<<a<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
const int INF=100000000;

int ans=0,H,W,N,sx,sy,ssx,ssy;
char cheese[]={'0','1','2','3','4','5','6','7','8','9'},town[1000][1000];
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int d[1000][1000];

void bfs(){
  queue<P> que;
  sx=ssx; sy=ssy;
  for(int c=1;c<=N;c++){
    //??¬???1???cheese??????????????¢?´¢
    for(int i=0;i<H;i++)
      for(int j=0;j<W;j++) d[i][j]=INF;

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

    while(que.size()){
      P p=que.front(); que.pop();
      if(town[p.first][p.second]==cheese[c]) {
        sx=p.first; sy=p.second;
        ans+=d[p.first][p.second]; printf("%dyeah\n",ans);
        break;
      }
      for(int i=0;i<4;i++){
        int nx=p.first+dx[i],ny=p.second+dy[i];
        if(0<=nx && nx<H && 0<=ny && ny<W && town[nx][ny]!='X' && d[nx][ny]==INF){
          que.push(P(nx,ny));
          d[nx][ny]=d[p.first][p.second]+1;
        }
      }
    }
    while(!que.empty()) que.pop();

  }

  return ;
}

int main(){
  scanf("%d %d %d",&H,&W,&N);
  for(int i=0;i<H;i++) scanf("%s",&town[i]);

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(town[i][j]=='S'){
        ssx=i; ssy=j;
        goto OUT;
      }
    }
  }
  OUT:

  bfs();

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

  return 0;
}