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<cstdio>
#include<vector>
#include<map>
#include<string>
#include<utility>
#include<algorithm>
#include<stack>
#include<queue>

#define numof(array) (sizeof (array) / sizeof *(array))
using namespace std;

#define INF (1 << 29)
static const int dx[] = {1, 0, -1, 0};
static const int dy[] = {0, 1, 0, -1};

#define MAX_H 1000
#define MAX_W 1000

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];//スタートからの距離


typedef pair<int, int> P;

int bfs(const P start, const P goal)
{
    queue<P> que;
    
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }

    que.push(start);
    d[start.second][start.first] = 0;


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

    
int main(void)
{
    cin  >> H >> W >> N;

    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            field[y][x] = 'X';
        }
    }

    P zahyo[10];//スタートと工場1,2,3...の座標
    for (int i = 0; i < 10; i++) {
        zahyo[i] = P(-1, -1);
    }


    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            cin >> field[y][x];
            
            //スタートと工場を見つけておく
            if ('0' < field[y][x] && field[y][x] <= '9') {
                zahyo[field[y][x] - '0'] = P(x, y);
            }
            if (field[y][x] == 'S') {
                zahyo[0] = P(x, y);
            }
            
        }
    }
        
    int cnt = 0;
    int answer = 0;
    while (cnt < N) {//-1でない間
        answer += bfs(zahyo[cnt], zahyo[cnt+1]);
        cnt++;
    }

    cout << answer << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
struct ma
{
	int x;
	int y;
};
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
int main()
{
	int ans = 0;
	int tate, yoko;
	int n;
	cin >> tate >> yoko >> n;
	char m[1100][1100];
	ma sta;
	for (int i = 0; i < tate; i++)
	{
		for (int k = 0; k < yoko; k++)
		{
			cin >> m[i][k];
			if (m[i][k] == 'S')
			{
				sta.x = k;
				sta.y = i;
			}
		}
	}
	int HP = 1;
	queue<ma> hoge;
	for (int i = 0; i < n; i++)
	{
		hoge.push(sta);
		int map[1100][1100];
		memset(map, -1, sizeof(map));
		map[sta.y][sta.x] = 0;
		while (!hoge.empty())
		{
			ma now;
			now = hoge.front();
			hoge.pop();
			for (int k = 0; k < 4; k++)
			{
				if (now.y + dy[k] < 0 || now.x + dx[k] < 0)continue;
				if (m[now.y + dy[k]][now.x + dx[k]] != 'X')
				{
					if (map[now.y + dy[k]][now.x + dx[k]] != -1)
					{
						continue;
					}
					map[now.y + dy[k]][now.x + dx[k]] = map[now.y][now.x] + 1;
					if (m[now.y + dy[k]][now.x + dx[k]] - '0' == HP)
					{
						m[now.y + dy[k]][now.x + dx[k]] = '.';
						ans += map[now.y + dy[k]][now.x + dx[k]];
						while (!hoge.empty())hoge.pop();
						HP++;
						sta.x = now.x + dx[k];
						sta.y = now.y + dy[k];
						break;
					}
					else
					{
						ma hage;
						hage.x = now.x + dx[k];
						hage.y = now.y + dy[k];
						hoge.push(hage);
					}
				}
			}

		/*	for (int i = 0; i < tate; i++)
			{
				for (int k = 0; k < yoko; k++)
				{
					cout << map[i][k] << " ";
				}
				cout << endl;
			}
			cout << endl;*/
		}
	}
	cout << ans << endl;

}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[1005][1005];int vis[1005][1005];
int w,h,n,xx,yy;int setp;

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

struct node
{
    int x,y,time;

};
node start;

void bfs(node st,char ed)
{
    queue<node> que;
    que.push(st);
    vis[st.x][st.y]=1;
    while(!que.empty())
    {
        node no=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=no.x+dx[i];
            int y=no.y+dy[i];
            if(vis[x][y]||x>=w||x<0||y>=h||y<0||ma[x][y]=='X')
                continue;
            if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                start.time=no.time+1;
                return ;
            }
            vis[x][y]=1;
            node no1;
            no1.x=x;no1.y=y;no1.time=no.time+1;
            que.push(no1);
        }
    }
}

int main()
{
    while(cin>>w>>h>>n,w>0)
    {
        for(int i=0;i<w;i++)
        {
            cin>>ma[i];
            for(int j=0;j<h;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;
                    ma[i][j]='0';
                }

        }
            for(int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(start,ma[start.x][start.y]+1);
            }

            printf("%d\n",start.time);

    }
}

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

  int used[1001][1001];
  char m[1001][1001];
  int H,W,N,sx,sy;
  
  const int dx[4]={1,0,-1,0};
  const int dy[4]={0,1,0,-1};

bool check(int x,int y){
  return x>=0&&y>=0&&x<H&&y<W&&m[x][y]!='X';
}

void debug()
{
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++){
      printf("%3d", used[i][j]);
    }
    puts("");
  }
  puts("");
} 
int bfs(int p){
  memset(used, 0, sizeof used);
  queue<pair<int,int> > que;
  used[sx][sy] = 1;
  que.push(make_pair(sx,sy));
  while(!que.empty()){
    pair<int,int> pos= que.front();
    que.pop();
    for(int i=0;i<4;i++){
      int x=pos.first+dx[i];
      int y=pos.second+dy[i];
      if(check(x,y) && used[x][y] == 0){
       que.push(make_pair(x,y));
       used[x][y] = used[pos.first][pos.second] + 1;
      }
      if(m[x][y]==p+'0'){
	
       sx=x;
       sy=y;
       return used[x][y] - 1;
      }
    }
  }
}



int main()
{
  cin>>H>>W>>N;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>m[i][j];
      if(m[i][j]=='S'){
       sx=i;
       sy=j;
      }	
    }
  }
  int res=0;
  for(int i=1;i<=N;i++){
    res+=bfs(i);
  }
  cout << res << endl;
}