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 <cstring>
#include <queue>
using namespace std;
int h, w, n, s;
char g[1001][1001];
bool v[1001][1001];
int id[8] = {0, -1, 0, 1, -1, 0, 1, 0};
int main() {
    scanf("%d %d %d", &h, &w, &n);
    for (int i = 0; i < h; ++i) {
        getchar();
        for (int j = 0; j < w; ++j) {
            while (~scanf("%c", &g[i][j]) && g[i][j] == ' ');
            //printf("%c",g[i][j]);
            if (g[i][j] == 'S') s = i * w + j;
        }
    }
    int ans = 0;
    char c = '0';
    while (c != n + '0') {
        c += 1;
        queue<pair<int, int> > q;
        memset(v, 0, sizeof v);
        v[s / w][s % w] = true;
        q.push({s, 0});
        while (!q.empty()) {
            int i = q.front().first / w, j = q.front().first % w, step = q.front().second;
            //printf("123%d\n%c\n", step, g[i][j]);
            if (g[i][j] == c) {
                s = i * w + j;
                ans += step;
                //printf("%d %d %d\n", i, j, ans);
                break;
            }
            q.pop();
            for (int k = 0; k < 8; k += 2) {
                int ni = i + id[k], nj = j + id[k + 1];
                if (ni >= 0 && ni < h && nj >= 0 && nj < w && !v[ni][nj] && (g[ni][nj] == '.' || g[ni][nj] <= c)) {
                    v[ni][nj] = true;
                    q.push({ni * w + nj, step + 1});
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
const int INF=10000;
typedef pair<int,int> P;
char town[1001][1001];
int h,w,n;
int s;
int d[1001][1001];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char num='1';
int sx,sy,gx,gy;
int ans=0;

int bfs(){
queue<P> que;

 for(int i=0;i<h;i++){
   for(int j=0;j<w;j++){
     d[i][j]=INF;
   }
 }
 d[sy][sx]=0;
 que.push(P(sy,sx));

 while(que.size()){
   P p=que.front(); que.pop();
   
   if(town[p.first][p.second]==num){
     sy=p.first;
     sx=p.second;
     return d[p.first][p.second];
     break;
   }
   
   for(int i=0;i<4;i++){
     int ny=p.first + dy[i],nx=p.second+dx[i];
     
     if(0<=nx && nx<w && 0<=ny && ny<h && town[ny][nx]!='X' && d[ny][nx]==INF){
       que.push(P(ny,nx));
       d[ny][nx]=d[p.first][p.second]+1;
     }
   }
 }
}


int main(){
  cin>> h>> w>> n;
  
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>> town[i][j];
      if(town[i][j]=='S'){
	sy=i;
	sx=j;
      }
    }
  }

for(num='1';num<'1'+n;num++){
  ans+=bfs();
 }
  cout<< ans<< endl;
}

C++: #include<iostream>
#include<queue>
using namespace std;

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

//queue<pair<int,int>> que;
char a[1002][1002];
int cost[1002][1002];
bool used[1002][1002];

int return_cost(pair<int,int> S,pair<int,int> G){
	//while(!que.empty())que.pop();
	queue<pair<int,int>> que;
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++){ cost[i][j]=INF; used[i][j]=false; }
	que.push(S);
	cost[S.first][S.second]=0;
	while(!que.empty()){
		pair<int,int> p=que.front(); que.pop();
		if(p==G)break;
		int x=p.first,y=p.second;
		if(!used[x][y]){
			for(int i=0;i<4;i++){
				int nx=x+muki[i][0],ny=y+muki[i][1];
				if(a[nx][ny]!='X'&&cost[nx][ny]>cost[x][y]+1){
					cost[nx][ny]=cost[x][y]+1;
					que.push(pair<int,int>(nx,ny));
				}
			}
			used[x][y]=true;
		}
	}
	return cost[G.first][G.second];
}

int main(){
	int h,w,n;
	pair<int,int> p[12];
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)a[i][j]='X';

	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=h;i++){
scanf("\n");
	for(int j=1;j<=w;j++){
		scanf("%c",&a[i][j]);
		if(a[i][j]=='S'){
			p[0]=pair<int,int>(i,j);
		}
		else if('1'<=a[i][j]&&a[i][j]<='9'){
			p[a[i][j]-'1'+1]=pair<int,int>(i,j);
		}
	}
	}

	int ret=0;
	for(int i=0;i<n;i++){
		ret+=return_cost(p[i],p[i+1]);
	}

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

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int H,W,N;
    int sy,sx;
    int HP;
    int ans=0;
          cin>>H>>W>>N;
    char map[H][W];
    int step[H][W];
    for(int i = 0; i < H; i++)
        for(int j = 0; j < W; j++)
        {
            cin>>map[i][j];
            step[i][j]=-1;
            if(map[i][j]=='S') {
                sy=i;
                sx=j;
        }   
        }
    for(HP=1;HP<=N;HP++){
        queue<pair<int,int> > q;
        q.push(make_pair(sy,sx));
        step[sy][sx]=0;
        while(1){
            int y,x;
            pair<int,int> point=q.front();
            q.pop();
            y=point.first;
            x=point.second;
            if(map[y][x]=='0'+HP){
                ans+=step[y][x];
                for(int i = 0; i < H; i++){
                    for(int j = 0; j < W; j++){
                        step[i][j]=-1;
                    }
                    }
                sy=y;
                sx=x;
                break;
            }
            if(y>0 && map[y-1][x]!='X' && step[y-1][x]==-1) {
                step[y-1][x]=step[y][x]+1;
                q.push(make_pair(y-1,x));
            }
            if(y<H-1 && map[y+1][x]!='X' && step[y+1][x]==-1) {
                step[y+1][x]=step[y][x]+1;
               q.push(make_pair(y+1,x));
            }
            if(x>0 && map[y][x-1]!='X' && step[y][x-1]==-1) {
                step[y][x-1]=step[y][x]+1;
                q.push(make_pair(y,x-1));
            }
            if(x<W-1 && map[y][x+1]!='X' && step[y][x+1]==-1) {
                step[y][x+1]=step[y][x]+1;
                q.push(make_pair(y,x+1));
            }
        }
    }
    cout<<ans<<endl;
}