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 <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int const INF = 1e9+7;

int h,w,ys,xs,yg,xg; char maze[1000][1000]; int d[1000][1000];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int bfs(){
    queue<P> que;
    for (int i = 0; i < h; i++){
        for (int j = 0; j < w; j++) d[i][j]=INF;
    }
    que.push(P(ys,xs)); d[ys][xs]=0;

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

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
   
    int n; cin>>h>>w>>n; P che[n+1];
    for (int i = 0; i < h; i++){
        for (int j = 0; j < w; j++){
            cin>>maze[i][j];
            if(maze[i][j]=='S')che[0]=P(i,j);
            else if(maze[i][j]!='.'&&maze[i][j]!='X')che[maze[i][j]-'0']=P(i,j);
        }
    }
    int ans=0;
    for (int i = 0; i < n; i++){
        ys=che[i].first; xs=che[i].second; yg=che[i+1].first; xg=che[i+1].second;
        ans+=bfs();
    }
    cout<<ans<<'\n';
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#define F first
#define S second
using namespace std;

int main(){

  int h,w,n;
  string s[1010];
  int z[1010][1010];
  int sx,sy;
  int ans = 0;
  int m;
  int dx[4] = {0,0,1,-1};
  int dy[4] = {1,-1,0,0};
  typedef pair < int,int > p;
  queue < p > que;

  cin >> h >> w >> n;

  for(int i=0;i<h;i++) cin >> s[i];

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(s[i][j] == 'S'){
	sx = j;
	sy = i;
	break;
      }
    }
  }

  for(int l=0;l<n;l++){

    memset(z,-1,sizeof(z));
    z[sy][sx] = 0;
    que.push( p(sy,sx) );
    char c = '1'+l;

    while( !que.empty() ){

      int x = que.front().S;
      int y = que.front().F;
      que.pop();

      for(int i=0;i<4;i++){

	int nx = x + dx[i];
	int ny = y + dy[i];

	if(nx < 0 || ny < 0 || nx == w || ny == h) continue;
	if(s[ny][nx] == 'X') continue;

	if(s[ny][nx] == c && (z[ny][nx] == -1 || z[y][x]+1 < z[ny][nx])){
	  m = z[ny][nx] = z[y][x]+1;
	  sy = ny;
	  sx = nx;
	  que.push( p(ny,nx) );
	  continue;
	}

	if(z[ny][nx] == -1 || z[y][x]+1 < z[ny][nx]){
	  z[ny][nx] = z[y][x]+1;
	  que.push( p(ny,nx) );
	}

      }
    }
    ans += m;
  }

  cout << ans << endl;

}

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

class Point{
public:
    int x,y;
    int number;
    Point(int X=0,int Y=0,int NUMBER=0):x(X),y(Y),number(NUMBER){};
};

bool map[1005][1005];
int condition[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
bool Judge[1005][1005]; //注意每次复位false
Point Cheeses[10];
int CheesesCount = 1;

int BFS(int x1,int y1,int x2,int y2){
    queue<Point> que;
    que.push(Point(x1,y1,0));
    while (que.size()) {
        Point p = que.front(); que.pop();
        if(p.x==x2&&p.y==y2){
            return p.number;
        }
        for(int i=0;i<4;i++){
            int tempX = p.x+condition[i][0],tempY = p.y+condition[i][1];
            if(map[tempX][tempY]&&!Judge[tempX][tempY]){
                Judge[tempX][tempY] = true;
                que.push(Point(tempX,tempY,p.number+1));
            }
        }
    }
    return 0;
}

int main(){
    int N,M,NUMBER;
    char temp;
    scanf("%d %d %d",&N,&M,&NUMBER);
    for(int i=1;i<=N;i++){
        getchar();
        for(int j=1;j<=M;j++){
            scanf("%c",&temp);
            if(temp!='X'){
                map[i][j] = true;
            }
            if(temp=='S'){
                Cheeses[0] = Point(i,j,0);
            }
            if(temp>='1'&&temp<='9'){
                Cheeses[temp-'0'] = Point(i,j,0);
            }
        }
    }
    int ans = 0;
    for(int i=0;i<NUMBER;i++){
        memset(Judge,false,sizeof(Judge));
        ans += BFS(Cheeses[i].x,Cheeses[i].y,Cheeses[i+1].x,Cheeses[i+1].y);
    }
    printf("%d\n",ans);

    return 0;

}

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


struct ST {
	int x;
	int y;
	int c;
};

char map[1000 + 10][1000 + 10];

bool wen[1000 + 10][1000 + 10] = { 0 };

int main()
{
	int ly, lx, n;

	ST m, tm;

	queue<ST> qu;

	int gy[] = {1,0,-1,0};
	int gx[] = {0,1,0,-1};

	char G[10 + 5] = { '0','1','2','3','4','5','6','7','8','9' };

	int sum = 0;
	int sy, sx;
	int i, j;

	cin >> ly >> lx >> n;

	for (i = 1; i <= ly; i++)
	{
		cin >> map[i] + 1;

		map[i][0] = 'X';
		map[i][lx + 1] = 'X';

		for (j = 1; j <= lx; j++)
		{
			if (map[i][j] == 'S')
			{
				sy = i;
				sx = j;
			}
		}
	}
	for (i = 0; i <= lx + 1; i++)
	{
		map[0][i] = 'X';
		map[ly + 1][i] = 'X';
	}

	m.y = sy;
	m.x = sx;
	m.c = 0;

	qu.push(m);

	for (i = 1; i <= n; i++)
	{
		while (!qu.empty())
		{
			m = qu.front();

			qu.pop();

			if (wen[m.y][m.x])
			{
				continue;
			}
			wen[m.y][m.x] = true;

			if (map[m.y][m.x] == G[i])
			{
				sum += m.c;

				while (!qu.empty())
				{
					qu.pop();
				}
				memset(wen, 0, sizeof(wen));

				m.c = 0;

				qu.push(m);

				break;
			}

			for (j = 0; j < 4; j++)
			{
				tm.y = m.y + gy[j];
				tm.x = m.x + gx[j];
				tm.c = m.c + 1;

				if (map[tm.y][tm.x] != 'X' && !wen[tm.y][tm.x] )
				{
					qu.push(tm);
				}
			}
		}
	}

	cout << sum << endl;

	return 0;
}