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 <utility>
#include <queue>
using namespace std;

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

int H, W;
char N;
const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
    queue<Pair> que;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }
    char muscle = '1';
    que.push(Pair(sx,sy));
    d[sx][sy] = 0;
    while (que.size()) {
        Pair p  = que.front(); que.pop();
        if (muscle-1 == N) 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 && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(Pair(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
            if (muscle == plant[nx][ny]) {
                while(que.size()) {que.pop();}
                que.push(Pair(nx, ny));
                muscle = muscle + 1;
                mytime += d[nx][ny];
                for (int i = 0; i < H; i++) {
                    for (int j = 0; j < W; j++) {
                        d[i][j] = INF;
                    }
                }
                d[nx][ny] = 0;
                break;
            }
        }
    }
}

void solve() {
    mytime = 0;
    bfs();
    cout << mytime << endl;
}

int main () {
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> plant[i][j];
            if(plant[i][j] == 'S') {sx = i; sy = j;}
        }
    }
    solve();
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <stdlib.h>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
using namespace std;
const int INF=1000000000;
const int max_n=1005;
typedef pair<int,int>P;
char maze[max_n][max_n+1];
int n,m,sx,sy;
int d[max_n][max_n];
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int bfs(char e)
{
    queue<P>que;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)d[i][j]=INF;
    que.push(P(sx,sy));
    d[sx][sy]=0;
    while(!que.empty())
    {
        P q=que.front();
        que.pop();
        if(maze[q.first][q.second]==e){sx=q.first;sy=q.second;break;}
        for(int i=0;i<4;i++)
        {
            int nx=q.first+dx[i],ny=q.second+dy[i];
            if(0<=nx&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='X'&&d[nx][ny]==INF)
            {
                que.push(P(nx,ny));
                d[nx][ny]=d[q.first][q.second]+1;
            }
        }
    }
return d[sx][sy];
}
int main()
{
    int t;
    while(cin>>n>>m>>t)
    {
       for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
       {
           cin>>maze[i][j];
           if(maze[i][j]=='S')sx=i,sy=j;
       }

       int res=0;
       for(int i=1;i<=t;i++){
        res+=bfs(i+'0');
       }
        cout<<res<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int D[1002][1002];
int A[1002][1002],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
//	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			A[S.y][S.x]=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();

		S.x+=x[l];
		S.y+=y[l];
		S.Z++;

		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
				A[S.y][S.x]=1;
		if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			//memset(A,0,sizeof(A));
			l=-1;

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

C++: #include <iostream>
#include <cassert>
#include <deque>
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
using namespace std;
int H,W,N;
int area[1000][1000];
int subGoal = 1;
deque<int> posX;
deque<int> posY;
deque<int> step;
void init();
void reset(int st);
int main() {
  cin >> H >> W >> N;
  init();
  int dx[4] = {1,0,-1,0};
  int dy[4] = {0,1,0,-1};
  int x,y,st;
  while(true){
    x = posX.front();
    posX.pop_front();
    y = posY.front();
    posY.pop_front();
    st = step.front();
    step.pop_front();
    rep(i,4){
      int nx = x+dx[i];
      int ny = y+dy[i];
      if(nx<0||nx>=W||ny<0||ny>=H) continue;
      if(area[nx][ny]==subGoal){
        if(subGoal==N){
          cout << st+1 << endl;
          return 0;
        }
        reset(st+1);
        break;
      }
      if(area[nx][ny]>0){
        area[nx][ny] -= 10;
        posX.push_back(nx);
        posY.push_back(ny);
        step.push_back(st+1);
      }
    }
  }
  return 0;
}

void init(){
  char input;
  rep(i,H){
    rep(j,W){
      cin >> input;
      if(input == '.'){
        area[j][i] = 10;
      }else if(input == 'X'){
        area[j][i] = -10;
      }else if(input == 'S'){
        posX.push_back(j);
        posY.push_back(i);
        step.push_back(0);
        area[j][i] = 0;
      }else{
        area[j][i] = (int)(input-'0');
      }
    }
  }
}

void reset(int st){
  posX.clear();
  posY.clear();
  step.clear();
  rep(i,H){
    rep(j,W){
      if(area[j][i] == subGoal){
        posX.push_back(j);
        posY.push_back(i);
        step.push_back(st);
      }else if(area[j][i] > -10&&area[j][i]<=0) area[j][i] += 10;
    }
  }
  subGoal++;
}