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<iostream>
#include<cstring>
using namespace std;
int H,W,N;
char map[1001][1001];
int visit[1001][1001];
struct node{
	int x,y,ph,time;
}s;
void bfs(){
	queue<node>q;
	while(!q.empty())q.pop();
	q.push(s);
	int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
	while(!q.empty()){
		node ne=q.front();q.pop();
		for(int i=0;i<4;i++){
			node no;
			no.x=ne.x+dx[i];
			no.y=ne.y+dy[i];
			no.time=ne.time;
			no.ph=ne.ph;
			if(no.x>=0&&no.x<H&&no.y>=0&&no.y<W&&map[no.x][no.y]!='X'){
				no.time++;
				printf("map[%d][%d],time=%d\n",no.x,no.y,no.time);				
				if((int)map[no.x][no.y]==s.ph+48){
					s.x=no.x;
					s.y=no.y;
					s.ph++;
					s.time=no.time;
					return ;					
				}
				map[no.x][no.y]='X';
				visit[no.x][no.y]=1;
				q.push(no);
			}
		}
	}
}
int main(void){
	while(scanf("%d",&H)!=EOF&&scanf("%d%d",&W,&N)){
		for(int i=0;i<H;i++)
		for(int j=0;j<W;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				s.x=i,s.y=j,s.ph=1,s.time=0;
			}
		}
		for(int i=0;i<N;i++){
			memset(visit,0,sizeof(visit));
			bfs();
				for(int i=0;i<H;i++)
				for(int j=0;j<W;j++)
				if(visit[i][j])map[i][j]='.';	
		}
		printf("%d\n",s.time);
	}
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<utility>

using namespace std;

int solve(const vector<string> mp, vector<pair<int,int>> points, int h, int w, int s, int e) {
    int cx,cy;
    int dx[] = {-1,0,1,0};
    int dy[] = {0,1,0,-1};
    vector<vector<int>> memo(w,vector<int>(h,0)); //(x,y)???????????¢
    queue<pair<int,int>> Q;
    Q.push(points[s]);
    while (!Q.empty()) {
        pair<int,int> q;
        q = Q.front();Q.pop();
        if (q == points[e]) {
            return memo[q.first][q.second];
        }
        for (int dir=0; dir<4; dir++) {
            cx = q.first + dx[dir];
            cy = q.second + dy[dir];
            if (cx<0 || cx>w-1 || cy<0 || cy>h-1 || mp[cy][cx] == 'X' || memo[cx][cy] != 0) continue;
            Q.push(make_pair(cx,cy));
            memo[cx][cy] = memo[q.first][q.second] + 1;
        }
    }
    return -1;
}

int main(){
    int h,w,n,num;
    int dx[] = {-1,0,1,0,};
    int dy[] = {0,1,0,-1,};
    long long sum=0;
    cin >> h >> w >> n;
    vector<string> mp;
    string tmp;
    vector<pair<int,int>> points(n+1);
    for (int i= 0; i<h; i++) {
        cin >> tmp;
        for (int j=0; j<w; j++) {
            if (tmp[j] == 'S') {
                points[0] = make_pair(j,i);
            } else if(tmp[j] != '.' && tmp[j] != 'X') {
                num = atoi(&tmp[j]);
                points[num] = make_pair(j,i);
            } 
        }
        mp.push_back(tmp);
    }
    for(int i = 1; i<= n ; i++) {
        sum += solve(mp,points,h,w,i-1,i);
    }
    cout << sum << endl;
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> P;

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

char maze[MAX_H][MAX_W + 1];

int H, W, N;
int si, sj;
int gi[MAX_N + 1], gj[MAX_N + 1];
int d[MAX_H][MAX_W];

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

int bfs(int goalNum)
{
  queue<P> que;
  int cgi = gi[goalNum], cgj = gj[goalNum];

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

  que.push(P(si, sj));
  d[si][sj] = 0;

  while (que.size()) {
    P p = que.front(); que.pop();

    if (p.first == cgi && p.second == cgj) break;

    for (int i = 0; i < 4; i++) {
      int ni = p.first + dx[i], nj = p.second + dy[i];

      if (0 <= ni && ni < H && 0 <= nj && nj < W && maze[ni][nj] != 'X' && d[ni][nj] == INF) {
        que.push(P(ni, nj));
        d[ni][nj] = d[p.first][p.second] + 1;
      }
    }
  }

  return d[cgi][cgj];
}

int solve()
{
  int res = 0;
  for (int i = 1; i <= N; i++) {
    // printf("%d:(%d, %d) => ", i, si, sj);
    res += bfs(i);
    si = gi[i]; sj = gj[i];
    // printf("(%d, %d):%d\n", si, sj, res);
  }
  return res;
}

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

    for (int j = 0; j < W; j++) {
      if ('0' < maze[i][j] && maze[i][j] <= '9') {
        gi[maze[i][j] - '0'] = i;
        gj[maze[i][j] - '0'] = j;
      }
      if (maze[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }

  printf("%d\n", solve());
}

C++: #include <iostream>
#include <algorithm>
#include <queue>

using namespace std;
int inf=10000;
char c[1000][1000];
int d[1000][1000],n,h,w,ans=0,dx[]={1,0,-1,0},dy[]={0,1,0,-1},i,j,sx,sy,gx,gy;
int func(char s,char g){
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      d[i][j]=inf;
      if(s==c[i][j]){
	sx=i;
	sy=j;
      }
      if(g==c[i][j]){
	gx=i;
	gy=j;
      }
    }
  }
  //cout<<sx<<" "<<sy;
  //cout<<gx<<" "<<gy;
  queue< pair<int,int> > q;
  q.push(pair<int,int> (sx,sy));
  d[sx][sy]=0;
  while(q.size()){
    pair<int,int> p=q.front();
    q.pop();
    if(p.first==gx&&p.second==gy)
      break;
    for(i=0;i<4;i++){
      int nx=p.first+dx[i],ny=p.second+dy[i];
      // cout<<nx<<" "<<ny<<" "<<c[nx][ny]<<" "<<d[nx][ny]<<endl;
      if(nx<h&&nx>=0&&ny<w&&ny>=0&&c[nx][ny]!='X'&&d[nx][ny]==inf){
	
	q.push(pair<int,int>(nx,ny));
	d[nx][ny]=d[p.first][p.second]+1;
      }
    }
  }
  //cout<<d[gx][gy]<<endl;
  return d[gx][gy];
}
int main(){
  char sg[]="S123456789";
  cin>>h>>w>>n;
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      cin>>c[i][j];
    }
  }
  for(int k=0;k<n;k++){
    ans+=func(sg[k],sg[k+1]);
  }
  cout<<ans<<endl;
}