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++: //Cheese
#include<bits/stdc++.h>
using namespace std;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int H, W, N;
vector<string> mp;
int goal[10][2];

int solve(int si, int sj, int gi, int gj){
  int d[1000][1000];
  queue<pair<int, int> > que;
  memset(d, -1, sizeof(d));
  que.push(pair<int, int>(si, sj));
  d[si][sj]=0;
  while(que.size()){
    pair<int, int> now=que.front(); que.pop();
    if(now.first==gi && now.second==gj)break;
    for(int i=0; i<4; i++){
      int nx=now.first+dir[i][0], ny=now.second+dir[i][1];
      if(0<=nx && nx<H && 0<=ny && ny<W && mp[nx][ny]!='X' && d[nx][ny]==-1){
        que.push(pair<int, int>(nx, ny));
        d[nx][ny]=d[now.first][now.second]+1;
      }
    }
  }
  return d[gi][gj];
}

int main(){
  cin >> H >> W >> N;
  for(int i=0; i<H; i++){
    string s;
    cin >> s;
    mp.push_back(s);
  }
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      if(mp[i][j]=='S'){goal[0][0]=i; goal[0][1]=j;}
      if('1'<=mp[i][j] && mp[i][j]<='9'){goal[mp[i][j]-'0'][0]=i; goal[mp[i][j]-'0'][1]=j;}
    }
  }
  int ans=0;
  for(int i=0; i<N; i++){
    ans+=solve(goal[i][0], goal[i][1], goal[i+1][0], goal[i+1][1]);
  }
  cout << ans << endl;
  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[101][101];
int d[101][101];
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;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <vector>
#include <set>
#include <queue>

using namespace std;

int h,w,n;
char s[1252];
int mp[1252][1252];
int ys[11];
int xs[11];
bool used[1252][1252];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

int bfs(int sy,int sx,int gy,int gx){
	queue<int> qy,qx,qdist;
	for(int i=0;i<w;i++)for(int j=0;j<h;j++)
		used[i][j]=false;
	qy.push(sy);
	qx.push(sx);
	qdist.push(0);
	used[sy][sx] = true;
	while(!qy.empty()){
		int y,x,dist;
		y = qy.front(); qy.pop();
		x = qx.front(); qx.pop();
		dist = qdist.front(); qdist.pop();
		if(gy==y && gx==x)return dist;
		for(int i=0;i<4;i++){
			int ny,nx,ndist;
			ny = y+dy[i];
			nx = x+dx[i];
			ndist = dist+1;
			if(ny<0 || ny>=h || nx<0 || nx>=w)continue;
			if(used[ny][nx])continue;
			if(mp[ny][nx]==1)continue;
			used[ny][nx] = true;
			qy.push(ny);
			qx.push(nx);
			qdist.push(ndist);
		}
	}
	return -1;
}

int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<h;i++){
		scanf("%s",s);
		for(int j=0;j<w;j++){
			mp[i][j] = (s[j]=='X'?1:0);
			if(s[j]!='X' && s[j]!='.'){
				if(s[j]=='S'){
					ys[0]=i;
					xs[0]=j;
				}else{
					ys[s[j]-'0']=i;
					xs[s[j]-'0']=j;
				}
			}
		}
	}
	int ans = 0;
	for(int i=0;i<n;i++){
		int b = ans;
		ans += bfs(ys[i],xs[i],ys[i+1],xs[i+1]);
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


const int INF=1e9;

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

struct mouse{
	int y, x, eat, time;
	mouse(){};
	mouse(int y,int x, int eat, int time):y(y),x(x),eat(eat), time(time){};
};

int main(){
	int h, w, n;
	cin>>h>>w>>n;
	vector<string> s(h);
	rep(i, h) cin>>s[i];
	pii p;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(s[i][j]=='S'){
				p = pii(i, j);
			}
		}
		//cout<<s[i]<<endl;
	}
	queue<mouse> q;
	int eat = 0, cnt = 0;
	q.push(mouse(p.fi,p.se,eat, cnt));
	mouse m;
	while(!q.empty()){
		m=q.front();
		q.pop();
		if(m.eat==n) break;
		//cout<<"("<<m.y<<", "<<m.x<<", "<<m.eat<<", "<<m.time<<")"<<endl;
		for(int i = 0; i < 4; i++){
			int x = m.x+dx[i], y = m.y+dy[i];
			if(x < 0 || x >= w || y < 0 || y >= h) continue;
			if(s[y][x]=='.'){
				q.push(mouse(y, x, m.eat, m.time+1));
			}
			else if(s[y][x]=='X'){

			}else{
				if(s[y][x]-'1'==m.eat){
					q.push(mouse(y, x, m.eat+1, m.time+1));
				}
				else{
					q.push(mouse(y, x, m.eat, m.time+1));
				}
			}
		}
	}
	cout<<m.time<<endl;
}