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>

using namespace std;

char data[1000][1000];
bool visited[1000][1000];
int h, w, n;
class State{
public:
  int x;
  int y;
  int hp;
  int min;
};

int bfs(int, int);
void syokika();
main(){
  while(cin >> h){
    cin >> w >> n;

    int sx, sy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> data[i][j];
	if(data[i][j]=='S'){
	  sx=j;
	  sy=i;
	  data[i][j]='.';
	}
      }
    }

    cout << bfs(sy, sx) << endl;
  }
  return 0;
}


int bfs(int sy, int sx){
  State u, v;
  queue<State> Q;
  int dy[4]={0, -1, 0, 1};
  int dx[4]={1, 0, -1, 0};
  int ny, nx;

  syokika();

  u.y=sy;
  u.x=sx;
  u.hp=1;
  u.min=0;
  visited[u.y][u.x]=true;
  Q.push(u);

  while(!Q.empty()){
    u=Q.front(); Q.pop();
    //    printf("%d %d %d %d\n", u.x, u.y, u.hp, u.min); 
    if(data[u.y][u.x]==u.hp+'0'){
      if(u.hp==n) return u.min;
      u.hp++;
      while(!Q.empty()){
	Q.pop();
      }
      syokika();
      visited[u.y][u.x]=true;
    }
    for(int i=0;i<4;i++){
      nx=u.x+dx[i];
      ny=u.y+dy[i];
      if(nx>=0 && nx<w){
	if(ny>=0 && ny<h){
	  if(data[ny][nx]=='X') continue;
	  if(!visited[ny][nx]){
	    v.y=ny;
	    v.x=nx;
	    v.hp=u.hp;
	    v.min=u.min+1;
	    visited[ny][nx]=true;
	    Q.push(v);
	  }
	}
      }
    }
  }
  return -1;
}

void syokika(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      visited[i][j]=false;
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int H, W;
char N;
vector<vector<char> > _map(H, vector<char>(W));
int num = 1;

int solve(int i, int j, int c){
  if(_map[i][j] == N)return c;
  for(int dx = -1; dx <= 1; dx++){
    for(int dy = -1; dy <= 1; dy++){
      if(dx != dy && dx != -1 * dy && (j + dx < W && i + dy < H)){
        int tmp;
        tmp = static_cast<char>(num);
        if(_map[i + dy][j + dx] == tmp || _map[i + dy][j + dx] == '.'){
          num++;
          solve(i + dy, j + dx, c + 1);
        }
      }
    }
  }
  return 0;
}

int main(){
  cin >> H >> W >> N;
  int x, y;
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      cin >> _map[i][j];
      if(_map[i][j] == 'S'){
        x = j; y = i;
      }
    }
  }
  cout << solve(y, x, 0) << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>

//ios::sync_with_stdio(false);

using namespace std;

const int INF = (1<<30) - 1;

int dh[] = {0, 0, 1, -1};
int dw[] = {1, -1, 0, 0};

int bfs(vector<string>& field, pair<int, int> start, pair<int, int> end){
    int H = field.size();
    int W = field[0].size();
    vector<vector<int> > d(H, vector<int>(W, INF));
    queue<pair<int, int> > nodes;
    
    d[start.first][start.second] = 0;
    nodes.push(start);
    
    while(!nodes.empty()){
        auto node = nodes.front(); nodes.pop();
        for(int i=0; i<4; i++){
            int nh = dh[i] + node.first;
            int nw = dw[i] + node.second;
            
            if(0<=nh&&nh<H && 0<=nw&&nw<W && field[nh][nw] != 'X' && d[nh][nw] == INF){
                d[nh][nw] = d[node.first][node.second] + 1;
                
                if(nh == end.first && nw == end.second)
                    return d[nh][nw];
                
                nodes.push(make_pair(nh, nw));
            }
        }
    }
    return -1;
}

int main(){
    int H, W, N;
    cin >> H >> W >> N;
    
    vector<string> field(H);
    for(int i=0; i<H; i++)
        cin >> field[i];
    
    vector<pair<int, int> > cheeses(N+1);
    
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            char& c = field[i][j];
            if(c == 'S'){
                cheeses[0] = make_pair(i, j);
            }else if(c != '.' && c != 'X'){
                cheeses[c-'0'] = make_pair(i, j);
            }
        }
    }
    
    
    int ans = 0;
    for(int i=0; i<N; i++){
        ans += bfs(field, cheeses[i], cheeses[i+1]);
    }
    
	return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<map>
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
bool pos[1000][1000];
int d[1000][1000];
int cx[10];
int cy[10];
int dx[4]={-1,0,0,1};
int dy[4]={0,1,-1,0};
int h,w,n;
int bfs(int sx,int sy,int gx,int gy){
	int i,j;
	queue<P> que;
	for(i=0;i<h;i++)for(j=0;j<w;j++)d[i][j]=INF;
	d[sx][sy]=0;
	que.push(P(sx,sy));
	while(!que.empty()){
		P p=que.front();
		que.pop();
		int x=p.first;
		int y=p.second;
		for(i=0;i<4;i++){
			if(x+dx[i]>=0&&x+dx[i]<h&&y+dy[i]>=0&&y+dy[i]<w&&pos[x+dx[i]][y+dy[i]]&&d[x+dx[i]][y+dy[i]]==INF){
				d[x+dx[i]][y+dy[i]]=d[x][y]+1;
				que.push(P(x+dx[i],y+dy[i]));
			}
		}
	}
	return d[gx][gy];
}
		
int main(){
	int i,j;
	char c;
	scanf("%d %d %d",&h,&w,&n);
	for(i=0;i<h;i++)for(j=0;j<w;j++)pos[i][j]=true;
	for(i=0;i<h;i++)for(j=0;j<w;j++){
		scanf(" %c",&c);
		if(c=='S'){cx[0]=i;cy[0]=j;}
		else if(c>='1'&&c<='9'){cx[c-'0']=i;cy[c-'0']=j;}
		else if(c=='X')pos[i][j]=false;
	}
	int ans=0;
	for(i=1;i<=n;i++)ans+=bfs(cx[i-1],cy[i-1],cx[i],cy[i]);
	printf("%d\n",ans);
	return 0;
}

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

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define INF 1e+8

struct P{
    int x;
    int y;
};

int w,h,n;
char feld[1010][1010];
int d[1010][1010];
int sx,sy,gx,gy;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans=0;
P che[15];


int bfs(){
    REP(i,n){
        P sp=che[i];
        sx=che[i].x;
        sy=che[i].y;
        gx=che[i+1].x;
        gy=che[i+1].y;
        queue<P> que;
        REP(y,h)REP(x,w)d[y][x]=INF;
        que.push(sp);
        d[sy][sx]=0;
        while(!que.empty()){
            P p=que.front();
            que.pop();
            int x=p.x;
            int y=p.y;
            if(x==gx&&y==gy){
                ans+=d[gy][gx];
                break;
            }
            REP(i,4){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||ny<0&&nx>=w&&ny>=h)continue;
                if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                    P next;
                    next.x=nx;
                    next.y=ny;
                    que.push(next);
                    d[ny][nx]=d[y][x]+1;
                }
            }
        }
    }
    return 0;
}

int main(){
    cin>>h>>w>>n;
    REP(y,h){
        REP(x,w){
            scanf(" %c",&feld[y][x]);
            if(feld[y][x]=='S'){
                che[0].x=x;
                che[0].y=y;
            }else if(feld[y][x]>='1'&&feld[y][x]<='9'){
                char c=feld[y][x];
                che[c-'0'].x=x;
                che[c-'0'].y=y;
            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}