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 <queue>
#include <cstring>

using namespace std;

const int M=1000+5;
int m, n, w;

char mapp[M][M];
bool v[M][M];
int xx[10];
int yy[10];

struct node
{
    int x;
    int y;
    int s;
};


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

int bfs(int x,int y,node nd)
{
    queue<node> q;
    node t;
    t.x=x;
    t.y=y;
    t.s=0;
    q.push(t);
    while (!q.empty())
    {
        node t=q.front();
        q.pop();

        if(t.x==nd.x&&t.y==nd.y)
        {
            return t.s;
        }

        for(int i=0;i<4;i++)
        {
            int nx=t.x+dx[i];
            int ny=t.y+dy[i];
            if(1<=nx&&nx<=m&&1<=ny&&ny<=n&&!v[nx][ny])
            {
                if(mapp[nx][ny]!='X')
                {
                    node tt;
                    tt.x=nx;
                    tt.y=ny;
                    tt.s=t.s+1;
                    q.push(tt);
                }
            }
        }



    }
    return 0;


}

int main() {
    scanf("%d %d %d", &m, &n, &w);
    for(int i=1;i<=m;i++)
    {
        getchar();
        for(int j=1;j<=n;j++)
        {
            scanf("%c",&mapp[i][j]);
            if('1'<=mapp[i][j]&&mapp[i][j]<='9')
            {
                xx[mapp[i][j]-'0']=i;
                yy[mapp[i][j]-'0']=j;
            }
            else if(mapp[i][j]=='S')
            {
                xx[0]=i;
                yy[0]=j;
            }
        }
    }



    int step=0;
    for(int k=0;k<w;k++)
    {
        memset(v,0, sizeof(v));
        node nd;
        nd.x=xx[k+1];
        nd.y=yy[k+1];
        v[xx[k]][yy[k]]=1;
        step+=bfs(xx[k],yy[k],nd);
    }
    printf("%d\n",step);



    return 0;
}

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;
const int MAX=(1<<14)-1;
mouse Q[MAX+1];
int s,num;

mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n;
int cost;

char in[16];
int inum[3];
 
main(){
  int s=0;
  int nums=0;
  do{
    read(0,in+s,1);
    if(isdigit(in[s]))inum[nums]=inum[nums]*10+in[s]-'0';
    else nums++;
  }while(in[s++]!='\n');
  h=inum[0];
  w=inum[1];
  n=inum[2];

  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h){
    read(0,ku[i],w+1);
    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++&MAX];
    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++&MAX]=(mouse){nx,ny,cost+1,nch};
    }
  }
}

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

using namespace std;

const int INF = -1;
const int MAX_H = 1000;
const int MAX_W = 1001;
const int MAX_N = 9;

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
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\n", 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<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct mouse{
	int x;
	int y;
	int cnt;
};
int used[1000][1000]={0};
char map[1000][1000];
int maps[1000][1000]={0};
int main(){
	int h,w,n,p=1,i,j,fx,fy,k;
	int mx[4]={-1,0,1,0};
	int my[4]={0,-1,0,1};
	char num[11]="0123456789";
	queue<mouse> m;
	mouse v,u;
	scanf("%d %d %d",&h,&w,&n);
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				v.x=j;
				v.y=i;
			}
			for(k=0;k<10;k++){
				if(num[k]==map[i][j]){
					maps[i][j]=k;
				}
			}
		}
	}
	v.cnt=0;
	m.push(v);
	while(1){
		v=m.front();
		m.pop();
		if(used[v.y][v.x]==1){
			continue;
		}
		
		else{
			used[v.y][v.x]=1;
		}
		if(maps[v.y][v.x]==p){
			if(p==n){
				break;
			}
			memset(used,0,sizeof(used));
			while(!m.empty()){
				m.pop();
			}
			p++;
			m.push(v);
			continue;
		}
		for(i=0;i<4;i++){
			if((v.x+mx[i])>=0&&(v.x+mx[i])<w&&(v.y+my[i])>=0&&(v.y+my[i])<h){
				u=v;
				u.x=v.x+mx[i];
				u.y=v.y+my[i];
				if(map[u.y][u.x]!='X'){
					u.cnt++;
					m.push(u);
				}
			}
		}
	}
	printf("%d\n",v.cnt);
	return 0;
}