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<cstring>
#include<cstdlib>
#include<queue>
using namespace std ;
struct ST
{
	int x ;
	int y ;
	int cnt = 0 ;
	bool came[ 1000 ][ 1000 ] = {{ 0 }} ;
} ;
char str[ 1000 ] ;
int main()
{
	ST m ,s ;
	queue< ST > que ;
	int n ;
	int w ,h ;
	int kyori = 0 ;
	int min = 1000 ;
	int map[ 1000 ][ 1000 ] = {{ 0 }} ;
	int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
	int my[ 4 ] = { 1 ,-1 ,0 ,0 } ;
	cin >> h >> w >> n ;
	int cheesuu = n ;
	int cheese ;
	int fx ,fy ;
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> str ;
		for( int j = 0 ; j < w ; j++ )
		{
			if( str[ j ] == 'X' )
				map[ i ][ j ] = -1 ;
			else if( str[ j ] == 'S' )
			{
				m.x = j ;
				m.y = i ;
			}
			else if( str[ j ] != '.' )
			{
				map[ i ][ j ] = str[ j ] - 48 ;
			}
		}
	}
	que.push( m ) ;
	while( n > 0 )
	{
		n-- ;
		cheese = cheesuu - n ;
		while( !que.empty() )
		{
			m = que.front() ;
			que.pop() ;
			m.came[ m.y ][ m.x ] = 1 ;
			if( map[ m.y ][ m.x ] == cheese )
			{
				fx = m.x ;
				fy = m.y ;
				min = min > m.cnt ? m.cnt : min ;
			}
			for( int i = 0 ; i < 4 ; i++ )
			{
				if( m.x + mx[ i ] > -1 && m.x + mx[ i ] < w && m.y +  my[ i ] > -1 && m.y + my[ i ] < h )
				{
					s = m ;
					s.x += mx[ i ] ;
					s.y += my[ i ] ;
					s.cnt++ ;
					if( map[ s.y ][ s.x ] != -1 && s.came[ s.y ][ s.x ] == 0 )
					{
						que.push( s ) ;
					}
				}
			}
		}
		memset( m.came ,0 ,sizeof( m.came ) ) ;
		kyori += min ;
		min = 1000 ;
		m.x = fx ;
		m.y = fy ;
		m.cnt = 0 ;
		que.push( m ) ;
	}
	cout << kyori << endl ;
	return 0 ;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <map> 
#include <iostream>
#include <cctype>
#include <utility> // 包含pair 
using namespace std;
typedef long long LL;
const int INF = 0x3fffffff;
const int MAXN = 1000 + 10;
char maze[MAXN][MAXN];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
struct Node {
	int x, y, t; // w为体力值, t为当前花费时间 
	Node (int _x, int _y, int _t) { x = _x; y = _y; t = _t;} 
};
int H, W, N; // H * W的地图上有N个奶酪工厂
int sx, sy; 
int ans;
bool loc_ok(int x, int y) {
	if (x < 0 || x >= H || y < 0 || y >= W || maze[x][y] == 'X') return false;
	return true;
}
bool inq[MAXN][MAXN];
int bfs(int wc) {
	memset(inq, false, sizeof(inq));
	queue<Node> que;
	que.push(Node(sx, sy, 0));
	inq[sx][sy] = true; // 标记为已经入队 
	while (!que.empty()) {
		Node u = que.front(); que.pop();
		int x = u.x, y = u.y, t = u.t;
		if (isdigit(maze[x][y])) {
			int temp = maze[x][y] - '0';
			if (temp == wc) { // 当前奶酪硬度与体力值相等 
				sx = x; // 去修改全局变量 
				sy = y;
				maze[x][y] = '.';
				return t;
			}  
		}
		for (int i = 0; i < 4; i++) {
			int ux = x + dx[i], uy = y + dy[i];
			if (loc_ok(ux, uy) && !inq[ux][uy]) {
				inq[ux][uy] = true;
				que.push(Node(ux, uy, t + 1));
			}
		}
	}
	// 队列不可能为空，所以此处不需要设出口 
}

int main() {
	while (scanf("%d %d %d\n", &H, &W, &N) != EOF) {
		ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				scanf("%c", &maze[i][j]);
				if (maze[i][j] == 'S') {
					sx = i; 
					sy = j;
					maze[i][j] = '.';
				}
			}
			getchar();
		}
		for (int i = 1; i <= N; i++) {
			ans += bfs(i); 
		}
		printf("%d\n", ans);
	}
    return 0;
}

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

typedef pair<int, int> P;

const int MAX_H = 1000, MAX_W = 1000;
const int INF = 100000000;
int H, W, N;
char field[MAX_H][MAX_W + 1];
int d[MAX_H][MAX_W + 1];
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
int result = 0;
P start, goal;

int bfs(P start, P goal);

int main(){
	cin >> H;
	cin >> W;
	cin >> N;
	for(int i=0; i<H; i++){
		cin >> field[i];
	}

	for(int i=0; i<H; i++){
		for(int j=0; j<W; j++){
			if(field[i][j] == 'S'){
				goal.first = i;
				goal.second = j;
			}
		}
	}

	for(int n=1; n<=N; n++){
		start.first = goal.first;
		start.second = goal.second;

		for(int i=0; i<H; i++){
			for(int j=0; j<W; j++){
				if(field[i][j] == n+'0'){
					goal.first = i;
					goal.second = j;
				}
			}
		}
		result += bfs(start, goal);
	}

	cout << result << endl;

	return 0;
}

int bfs(P start, P goal){
	for(int i=0; i<MAX_H; i++)
		for(int j=0; j<MAX_W; j++) d[i][j] = INF;

	queue<P> que;
	que.push(start);
	d[start.first][start.second] = 0;

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

		if(p.first==goal.first && p.second==goal.second) break;

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

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

	return d[goal.first][goal.second];
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;
mouse Q[10000];
int s,num;

mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n;
int cost;

main(){
  scanf("%d%d%d ",&h,&w,&n);
  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h)gets(ku[i]);


  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }


  Q[s+num++]=(mouse){cx,cy,0,0};
  maxch[cx][cy]=0;

  while(true){
    t=Q[s++];
    num--;
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;

    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
      if(ku[nx][ny]=='X')continue;
      nch=ch;
      if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
        nch=ch+1;
      }

      if(maxch[nx][ny]>=nch)continue;
      if(nch==n){
        printf("%d\n",cost+1);
        return 0;
      }
      maxch[nx][ny]=nch;
      Q[s+num++]=(mouse){nx,ny,cost+1,nch};
    }
  }
}