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 <queue>
#define INF 300000

using namespace std;

typedef pair<int, int> p;

int w, h, n, result;
int visited[1000][1000];
bool map[1000][1000];
p start, last_start, goal, cur, cheese[10];
queue<p> myqueue;

void init(int in){
    start = last_start;

    while(!myqueue.empty()) myqueue.pop();

    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            if(map[i][j] == false){
                visited[i][j] = -1;
            }else{
                visited[i][j] = INF;
            }
            goal = cheese[in];
        }
    }
    return ;
}

int main(){
    cin >> w >> h >> n;

    char input;
    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            cin >> input;
            if(input == 'X') map[i][j] = false;
            else             map[i][j] = true;
            if(input == 'S')
                last_start = make_pair(i, j);
            if(input != 'X' && input != 'S' && input != '.'){
                cheese[input-'0'] = make_pair(i, j);
            }
        }
    }


    result = 0;
    for(int i=1; i<=n; ++i){
        init(i);
        myqueue.push(start);
        visited[start.first][start.second] = 0;

        while(!myqueue.empty()){

            cur = myqueue.front();
            myqueue.pop();

            if(cur == goal)break;

            int x = cur.first, y = cur.second;
            if(x-1 >= 0 && visited[x-1][y]==INF){
                myqueue.push(make_pair(x-1, y));
                visited[x-1][y] = visited[x][y]+1;
            }
            if(y-1 >= 0 && visited[x][y-1]==INF){
                myqueue.push(make_pair(x, y-1));
                visited[x][y-1] = visited[x][y]+1;
            }
            if(x+1 < w && visited[x+1][y]==INF){
                myqueue.push(make_pair(x+1, y));
                visited[x+1][y] = visited[x][y]+1;
            }
            if(y+1 < h && visited[x][y+1]==INF){
                myqueue.push(make_pair(x, y+1));
                visited[x][y+1] = visited[x][y]+1;
            }
        }

        result += visited[goal.first][goal.second];
        last_start = goal;
    }
    cout << result << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
/* 変数名を被らせない
   デバッグコードを取り除く 
　　提出する問題を間違えない */
#define ANSWER(x) cout<<x<<endl
#define debug(x) cout<<#x<<": "<<x
typedef pair<int,int> P;
void bfs(int sx,int sy);
int H,W,N,a;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int cheese_x[10];
int cheese_y[10];
char ch[1001][1001];
int al[1001][1001];
int S_x,S_y,hp = 1,dis;
int main(){
  cin >> H >> W >> N;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> ch[i][j];
      if(ch[i][j] == 'S'){
	S_x = j;
	S_y = i;
      }
    }
  }
  for(int i=0;i<N;i++){
    bfs(S_x,S_y);
  }
  
  cout << a << endl;
}
void bfs(int sx,int sy){
  queue<P> que;
  que.push(P(sx,sy));
  al[sy][sx] = a;
  while(!que.empty()){
    P now = que.front();que.pop();
    int x = now.first;
    int y = now.second;
    if(ch[y][x] - '0' == hp){
      hp++;
      S_x = x;
      S_y = y;
      a = al[y][x];
      
      memset(al,0,sizeof(al));
      
      break;
    }
    for(int i=0;i<4;i++){
      int mx = x + dx[i];
      int my = y + dy[i];
      if(mx < 0 || my < 0 || mx >= W || my >= H)continue;
      if(ch[my][mx] != 'X' && al[my][mx] == false){
	que.push(P(mx,my));
	al[my][mx] = al[y][x] + 1;
      }
    }
  }
}

C++: #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
using namespace std;
int n,m,p;
int sx,sy;
char s[1505][1505];
struct node{
	int x,y,step;
	node(int sx,int sy,int step)
	{
		this->x=sx;this->y=sy;this->step=step;
	}
};
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int vis[1505][1500];
node bfs(node u,int pos)
{
	queue<node> q;
	while(!q.empty())q.pop();
	q.push(u);
	
	while(!q.empty())
	{
		node v=q.front();q.pop();
		if(s[v.x][v.y]==pos+'0') return v;
		for(int i=0;i<4;i++)
		{
			int x=v.x+dx[i];
			int y=v.y+dy[i];
			if(x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]!='X'&&vis[x][y]==0)
			{
				vis[x][y]=1;
				node now=node(x,y,v.step+1);
				q.push(now);
			}
		}
	}
}
int main()
{
	while(cin>>n>>m>>p)
	{
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>s[i]+1;
			for(int j=1;j<=m;j++)
			{
				if(s[i][j]=='S') sx=i,sy=j;
			}
		}
		node u=node(sx,sy,0);	
		for(int i=1;i<=p;i++)
		{		
			memset(vis,0,sizeof(vis));
			vis[u.x][u.y]=1;
			u=bfs(u,i);
			ans+=u.step;
			u.step=0;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <queue>
using namespace std;
int INF= 1001001001;
typedef pair<int,int> pp;
int H, W, N;
int A[1001][1001];
pp F[11];
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
int d[1001][1001];

int bfs(int c) {
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) d[i][j] = INF;
    }
    int fx = F[c].first, fy = F[c].second, tx = F[c+1].first, ty = F[c+1].second;
    d[fx][fy] = 0;
    queue<pp> Q;
    Q.push(pp(fx,fy));
    while(!Q.empty()) {
        pp cur = Q.front(); Q.pop();
        int x = cur.first, y = cur.second;
        for(int i=0; i<4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if(nx >= 0 && nx < H && ny >= 0 && ny < W && A[nx][ny] == 0 && d[nx][ny] == INF) {
                d[nx][ny] = d[x][y] + 1;
                if(nx == tx && ny == ty) return d[nx][ny];
                Q.push(pp(nx,ny));
            }
        }
    }
    return -1;
}

int main() {
    cin >> H >> W >> N;
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            char tmp; cin >> tmp;
            switch(tmp) {
                case 'X': {A[i][j] = -1; break;}
                case 'S': {F[0].first = i; F[0].second = j; A[i][j] = 0; break;}
                case '.': {A[i][j] = 0; break;}
                default: {int n = tmp - '0'; F[n].first = i; F[n].second = j; A[i][j] = 0;}
            }
        }
    }
    int res = 0;
    for(int i=0; i<N; i++) res += bfs(i);
    cout << res << endl;
}