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<cstdlib>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
int h,w,n;//
typedef pair<int,int> P;
queue <P> q;
char g[1002][1002];
int x,y;//起点的横纵坐标
int nx,ny;//下一个点
int d[1002][1002];
int _x[4]={-1,0,0,1},_y[4]={0,-1,1,0};
bool is_legal(int x,int y)//格子是否越界
{
	if(x>=1&&x<=h&&y>=1&&y<=w) return true;
	else return false;
		
}
//队列初始要有起点
//从起点出发，目的地：power的奶酪
//power奶酪的最小步数
int bfs(int power)
{
	while(!q.empty()) q.pop();
	q.push(P(x,y));
	for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
			{
				d[i][j]=INF;
			}
	d[x][y]=0;
	while(!q.empty())
	{
		P p=q.front();
		q.pop();
		int row=p.first;
		int column=p.second;
		
		for(int i=0;i<4;i++)
		{
			nx=row+_x[i];
			ny=column+_y[i];
			if(!is_legal(nx,ny)) continue;
			
			//d[nx][ny]=d[row][column]+1;障碍物或越界，d数组不变
			if(g[nx][ny]=='0'+power) 
			{
				d[nx][ny]=d[row][column]+1;
				//下一次bfs的起点
				x=nx;
				y=ny;
				return d[nx][ny];
			}
			if(g[nx][ny]!='X') 
			{
				//更新d的条件没判，w了好多次
				if(d[row][column]+1<d[nx][ny])
				{
					d[nx][ny]=d[row][column]+1;
				q.push(P(nx,ny));
				}
				
			}
		}
	}
	return INF;
}

int main()
{
	while(scanf("%d %d %d",&h,&w,&n)!=EOF)
	{
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
			{
				cin>>g[i][j];
				if(g[i][j]=='S') {x=i;y=j;}
			}
		
		int ans=0;
		
		
		//1～n奶酪的最小步数之和
		
		for(int k=1;k<=n;k++) {
			ans+=bfs(k);
		}
		
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
    int Y, X, n;
    int x[10];
    int y[10];
    int ans = 0;
    cin >> Y >> X >> n;
    string a[Y];
    for(int i = 0; i < Y; i++) {
        cin >> a[i];
    }
    int step[Y + 2][X + 2];

    for(int i = 0; i < X + 2; i++) {
        step[0][i] = -2;
    }
    for(int i = 1; i <= Y; i++) {
        step[i][0] = -2;
        for(int j = 1; j <= X; j++) {
            if(a[i - 1][j - 1] == 'S') {
                x[0] = j;
                y[0] = i;
                step[i][j] = 0;
            }
            if(a[i - 1][j - 1] == '.') {
                step[i][j] = -1;
            }
            if(a[i - 1][j - 1] == 'X') {
                step[i][j] = -2;
            }
            if(a[i - 1][j - 1] == '1') {
                x[1] = j;
                y[1] = i;
                step[i][j] = 1;
            }
            if(a[i - 1][j - 1] == '2') {
                x[2] = j;
                y[2] = i;
                step[i][j] = 2;
            }
            if(a[i - 1][j - 1] == '3') {
                x[3] = j;
                y[3] = i;
                step[i][j] = 3;
            }
            if(a[i - 1][j - 1] == '4') {
                x[4] = j;
                y[4] = i;
                step[i][j] = 4;
            }
            if(a[i - 1][j - 1] == '5') {
                x[5] = j;
                y[5] = i;
                step[i][j] = 5;
            }
            if(a[i - 1][j - 1] == '6') {
                x[6] = j;
                y[6] = i;
                step[i][j] = 6;
            }
            if(a[i - 1][j - 1] == '7') {
                x[7] = j;
                y[7] = i;
                step[i][j] = 7;
            }
            if(a[i - 1][j - 1] == '8') {
                x[8] = j;
                y[8] = i;
                step[i][j] = 8;
            }
            if(a[i - 1][j - 1] == '9') {
                x[9] = j;
                y[9] = i;
                step[i][j] = 9;
            }
        }
        step[i][X + 1] = -2;
    }
    for(int i = 0; i < X + 2; i++) {
        step[Y + 1][i] = -2;
    }

    queue<pair<int, int> > q;

    q.push(make_pair(y[0], x[0]));

    for(int i = 0; i < n; i++) {
        q.push(make_pair(y[i], x[i]));
        while(q.empty() == false) {
            if(q.front().first == y[i + 1] && q.front().second == x[i + 1]) {
                ans += step[y[i + 1]][x[i + 1]];
                break;
            }
            if(step[q.front().first + 1][q.front().second] != -2) {
                step[q.front().first + 1][q.front().second] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first + 1, q.front().second));
            }
            if(step[q.front().first - 1][q.front().second] != -2) {
                step[q.front().first - 1][q.front().second] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first - 1, q.front().second));
            }
            if(step[q.front().first][q.front().second + 1] != -2) {
                step[q.front().first][q.front().second + 1] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first, q.front().second + 1));
            }
            if(step[q.front().first][q.front().second - 1] != -2) {
                step[q.front().first][q.front().second - 1] =
                    step[q.front().first][q.front().second] + 1;
                q.push(make_pair(q.front().first, q.front().second - 1));
            }
            q.pop();
            for(int i = 0; i < X + 2; i++) {
                step[0][i] = -2;
            }
            for(int i = 1; i <= Y; i++) {
                step[i][0] = -2;
                for(int j = 1; j <= X; j++) {
                    step[i][j] = -1;
                }
                step[i][X + 1] = -2;
            }
            for(int i = 0; i < X + 2; i++) {
                step[Y + 1][i] = -2;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
#define INF 1000000000
using namespace std;
typedef pair<int,int> P;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
int h,w,n;
char s[1000][1000];
bool inf(int y,int x){return(y>=0&&y<h&&x>=0&&x<w)?1:0;}
int bfs(P sp,P sg){
	queue<P> que;
	que.push(sp);
	vector<vector<int> > d(h,vector<int>(w,INF));
	d[sp.first][sp.second]=0;
	while(!que.empty()){
		P t=que.front();
		que.pop();
		int y=t.first,x=t.second;
		if(t==sg){
			return d[sg.first][sg.second];
		}
		for(int i=0;i<4;i++){
			int ny=dy[i]+y,nx=dx[i]+x;
			if(inf(ny,nx)&&d[ny][nx]>d[y][x]+1&&s[ny][nx]!='X'){
				que.push(P(ny,nx));
				d[ny][nx]=d[y][x]+1;
			}
		}
	}
	return -1;
}
int main(){
	while(cin>>h>>w>>n,h||w||n){
		//s=vector<string>(h);
		vector<P> pm(n+1);
		for(int i=0;i<h;i++){
			cin>>s[i];
			for(int j=0;j<w;j++){
				if(s[i][j]=='S'){
					pm[0]=P(i,j);
				}else if(isdigit(s[i][j])){
					pm[s[i][j]-'0']=P(i,j);
				}
			}
		}
		int ans=0;
		for(int i=0;i<n;i++){
			ans+=bfs(pm[i],pm[i+1]);
		}
		cout<<ans<<endl;
	}
	return 0;
}