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 <iostream>
#include <queue>
#define min(x,y) ((x)<(y)?(x):(y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
int Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1,0,-1,0};
int ver[] = {0,-1,0,1};
int ans;


void clear();
void show();
P bfs(int cheese,P n);

int main(void) {
    scanf("%d %d %d\n", &H, &W, &N);
    P s,e;
    for (int i = 0; i<H; i++) {
        for (int j = 0; j<W; j++) {
            Maps[i][j] = getchar();
            if (Maps[i][j]=='S') {s.first = i; s.second = j;}
        }
        getchar();
    }

    
    P tmp;
    tmp.first = s.first; tmp.second = s.second;
    
    for (int i = 1; i<=N; i++) {
        clear();
        tmp = bfs(i, tmp);
    }
    
    
    
    printf("%d\n", ans);
    return 0;
}

P bfs(int cheese,P n) {
    P end;
    queue<P> que;
    que.push(n);
    dist[n.first][n.second] = 0;
    
    while (que.size()) {
        P top = que.front();
        
        que.pop();
        for (int i = 0; i<4; i++) {
            int nx = top.first+ver[i], ny = top.second+hor[i];
            if (nx>=0 && nx<H && ny>=0 && ny<W) {
                if (dist[nx][ny]==INFI) {
                    if (Maps[nx][ny]=='.' || (isdigit(Maps[nx][ny]) && cheese!=Maps[nx][ny]-'0'))  {
                        que.push(P(nx,ny));
                        dist[nx][ny] = min(dist[nx][ny], dist[top.first][top.second]+1);
                    } else if (isdigit(Maps[nx][ny]) && cheese==Maps[nx][ny]-'0') {
                        que.push(P(nx,ny));
                        dist[nx][ny] = min(dist[nx][ny], dist[top.first][top.second]+1);
                        end.first = nx; end.second = ny;
                        break;
                    }
                }
            }
        }
    }
    ans += dist[end.first][end.second];
    return end;
}

void clear() {
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            dist[i][j] = INFI;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<cstdio> 
#include<limits.h>
using namespace std;
typedef pair<int,int>	P;
//int sx,sy;
//int gx,gy;
int	h,w,n;		//h行w列 
int factory[16][2];
char arr[1000][1000];
int movx[4]={-1,0,0,1};
int movy[4]={0,1,-1,0};
int d[1000][1000];
int bfs(int x1,int y1,int x2,int y2){
	queue<P>	myQ;
	myQ.push(P(x1,y1));
	fill_n(&d[0][0],1000*1000,-1);
	d[x1][y1]=0;
	while(!myQ.empty()){
		P pos=myQ.front();
		if(pos.first==x2&&pos.second==y2){
			arr[pos.first][pos.second]='.';
			break;
		}
		myQ.pop();
		for(int i=0;i<4;i++){
			int xx=pos.first+movx[i];
			int yy=pos.second+movy[i];
			if(d[xx][yy]==-1&&xx>=0&&xx<h&&yy>=0&&yy<w&&arr[xx][yy]!='X'){	//(arr[xx][yy]=='.'||arr[xx][yy]==arr[x2][y2])
				d[xx][yy]=d[pos.first][pos.second]+1;
				myQ.push(P(xx,yy));
			}
		}
	}
	return d[x2][y2];
}
int main(){
	while(scanf("%d%d%d",&h,&w,&n)!=EOF){
		for(int i=0;i<h;i++){
			getchar();
			for(int j=0;j<w;j++){
				scanf("%c",&arr[i][j]);
				if(arr[i][j]=='S'){
					arr[i][j]='.';
					factory[0][0]=i;
					factory[0][1]=j;
				}
				else if(isdigit(arr[i][j])){
					int index=arr[i][j]-'0';
					factory[index][0]=i;
					factory[index][1]=j;
				}
			}
		}
		int res=0;
		for(int i=0;i<n;i++){
			res+=bfs(factory[i][0],factory[i][1],factory[i+1][0],factory[i+1][1]);
		}
		printf("%d\n",res);
	}
	return 0;
}

C++: #include<queue>
#include<cctype>
#include<cstdio>

#define mp           make_pair
#define rep(i,n)     for(int i = 0;i<n;i++)

using namespace std;

typedef pair<int , int > pii;


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

int h,w;
char grid[1010][1010];

int sortestDestance(int sx,int sy,int gx,int gy)
{
	static bool visited[1010][1010];
	rep(i,h)rep(j,w)  visited[i][j] = false;
	visited[sy][sx] = true;
	
	queue< pair< int ,pii > > qu;
	qu.push(mp(0,mp(sy,sx)));
	while(1)
	{
		pair<int ,pii> a = qu.front();
		qu.pop();
		int d = a.first,x = a.second.second,y = a.second.first;
		if(x == gx && y == gy) return d;
		
		rep(i,4)
		{
			int xx = x + dx[i], yy = y + dy[i];
			 if(0<=xx && xx<w && 0<=yy && yy<h && grid[yy][xx]!='X' && !visited[yy][xx])
			{
                visited[yy][xx]=true;
                qu.push(mp(d+1,mp(yy,xx)));
            }

		}
	}
}



int main(){
    int n;  scanf("%d%d%d",&h,&w,&n);
    int cx[10],cy[10];
    rep(i,h){
        scanf("%s",grid[i]);
        rep(j,w){
            if(grid[i][j]=='S')     cx[0]=j,cy[0]=i;
            if(isdigit(grid[i][j])) cx[grid[i][j]-'0']=j,cy[grid[i][j]-'0']=i;
        }
    }
 
    int cost=0;
    rep(i,n)    cost+=shortestDistance(cx[i],cy[i],cx[i+1],cy[i+1]);
    printf("%d\n",cost);
 
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
#define INF 1000000
typedef pair<int,int> P;

int H,W,N,i,j,minute;
char field[1000][1000];
P factory[10];

int solve(int i){
	queue<P> s;
	P now;
	int dist[H][W];
	for(int k=0;k<H;k++){
		for(int j=0;j<W;j++){
			dist[i][j]=INF;
		}
	}
	int x[4]={1,0,-1,0},y[4]={0,1,0,-1};
	dist[factory[i].first][factory[i].second]=0;
	s.push(factory[i]);
	while(!s.empty()){
		now=s.front(); s.pop();
		if(now == factory[i+1]) break;
		for(int k=0;k<4;k++){
			int nx=now.first+x[k],ny=now.second+y[k];
			if(0<=nx && nx<H && 0<=ny && ny<W && field[nx][ny]!='X' && dist[nx][ny] == INF){
				s.push(P(nx,ny));
				dist[nx][ny] = dist[now.first][now.second]+1;
			}
		}
	}
	return dist[now.first][now.second];
}

int main(){
	cin>>H>>W>>N;
	for(i=0;i<H;i++){
		for(j=0;j<W;j++){
			cin>>field[i][j];
			if(1<=field[i][j]-'0' && field[i][j]-'0'<=N){factory[field[i][j]-'0']=P(i,j);}
			if(field[i][j]=='S'){factory[0].first=i;factory[0].second=j;}
		}
	}
	for(i=0;i<N;i++){
		minute+=solve(i);//i??????????????????????£?????????¢?´¢
	}
	cout<<minute<<endl;
	return 0;
}