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>
#include<string.h>
using namespace std;
int  cheese[10001][10001];
int  check[10001][10001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1&&cheese[y.a+q[i]][y.b+p[i]]!=-1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define MAX 1000
struct Point{int x,y,dist;};
int main()
{
  int H,W,N;
  int vital;
  int i,j;
  char field[MAX][MAX];
  int dx[4]={0,1,0,-1};
  int dy[4]={-1,0,1,0};
  int sx,sy,ans;

  cin>>H>>W>>N;
  for(i=0;i<H;i++){
    for(j=0;j<W;j++){
      cin>>field[i][j];
      if(field[i][j]=='S'){
	sy=i;
	sx=j;
	ans=0;
      }
    }
  }

  for(vital=1;vital<=N;vital++){
    queue<Point>q;
    bool visited[MAX][MAX]={0};

    Point p={sx,sy,0},p2;
    q.push(p);
    while(1){
      p=q.front();
      q.pop();

      if(field[p.y][p.x]==(vital+'0')){
	sy=p.y;
	sx=p.x;
	ans+=p.dist;
	break;
      }

      for(i=0;i<4;i++){
	p2.y=p.y+dy[i];
	p2.x=p.x+dx[i];
	p2.dist=p.dist+1;
	if(p2.y<0||H<=p2.y)continue;
	if(p2.x<0||W<=p2.x)continue;
	if(field[p2.y][p2.x]=='X')continue;
	if(visited[p2.y][p2.x])continue;
	visited[p2.y][p2.x]=true;
	q.push(p2);
      }
    }
  }
  cout<<ans<<endl;
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
struct as
{
    int x,y;
}pos[10]; //用来存??次的起点和?点
struct node
{
    int x,y;
    int step;
};
bool vis[1010][1010];
char maze[1050][1050];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int H,W,N;

bool check(int x,int y)
{
    if (x<=0 || x>H) return 1;
    if (y<=0 || y>W) return 1;
    if (vis[x][y]) return 1;
    if (maze[x][y] =='X')return 1;
    return 0;
}
int bfs(int m,int n)
{
    memset(vis,0,sizeof vis);
    int sx = pos[m].x, sy = pos[m].y;
    int gx = pos[n].x, gy = pos[n].y;
   // printf("%d %d\n",sx,sy);
   // printf("%d %d\n",gx,gy);
    queue<node> Q;
    node a,next;
    a.step = 0;
    a.x = sx, a.y =sy;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if (a.x == gx && a.y == gy)
            return a.step;
        for(int i = 0; i < 4 ;i++)
        {
            int nx = a.x + dx[i];
            int ny = a.y + dy[i];
            if(check(nx,ny))
                continue;
            else
            {
                vis[nx][ny] =1;
                next.x = nx,next.y = ny,next.step = a.step+1;
                Q.push(next);
            }
        }
    }
    return 999999;
}

int main()
{
   // freopen("in.txt","r",stdin);
    while(cin>>H>>W>>N)
    {
        int sx,sy;
        for(int i = 1;i<=H;i++)
        {
            for(int j = 1; j <= W;j++)
            {
                cin>>maze[i][j];
                if (maze[i][j] >='1' && maze[i][j]<='9')
                {
                    pos[maze[i][j]-'0'].x = i;
                    pos[maze[i][j]-'0'].y = j;
                }
                if (maze[i][j]=='S')
                    sx=i,sy = j;
            }
        }

        pos[0].x = sx,pos[0].y = sy;
        int ans = 0;
        for(int i = 0;i<N;i++)
            ans += bfs(i,i+1);
        cout<<ans<<endl;
    }
    return 0;
}

/*
入力例 2
4 5 2
.X..1
....X
.XX.S
.2.X.

出力例 2
12
*/

C++: #include <bits/stdc++.h>
using namespace std;

queue<pair<int,int>> qu;
char memo[1001][1001];
char memod[1001][1001];
int d[1001][1001]={0};
pair<int,int> cheese[10],now,nextm;
int ans=0,h,w,n;
int x[4] = {1,-1,0,0};
int y[4] = {0,0,1,-1};

int main(){
  int i,j,a,b;
  cin >> h >> w >> n;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      cin >> memo[i][j];
      if(memo[i][j]=='S'){
	cheese[0] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='1'){
	cheese[1] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='2'){
	cheese[2] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='3'){
	cheese[3] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='4'){
	cheese[4] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='5'){
	cheese[5] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='6'){
	cheese[6] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='7'){
	cheese[7] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='8'){
	cheese[8] = make_pair(i,j);
	memo[i][j]='.';
      }
      if(memo[i][j]=='9'){
	cheese[9] = make_pair(i,j);
	memo[i][j]='.';
      }
    }
  }


    for(a=1;a<=h;a++){
    for(b=1;b<=w;b++){
      memod[a][b] = memo[a][b];
      d[a][b] = 0;
      //    cout << memo[a][b];
    }
    //   cout << endl;
  }


  for(j=0;j<n;j++){

        memod[cheese[j].first][cheese[j].second] = 'X';

  
  qu.push(cheese[j]);
  //  while(d[cheese[j+1].first][cheese[j+1].second]==0){
  while(qu.size()){
    now = qu.front();
    //   cout << now.first << now.second << endl;
    qu.pop();
    for(i=0;i<4;i++){
      nextm = make_pair(now.first+x[i],now.second+y[i]);
      //  cout << nextm.first << nextm.second << endl;
      if(nextm.first>=1&&nextm.first<=h&&nextm.second>=1&&nextm.second<=w&&memod[nextm.first][nextm.second]=='.'){
	//	cout << 'A' ;
	memod[nextm.first][nextm.second]='X';
	d[nextm.first][nextm.second] = d[now.first][now.second] + 1;
	qu.push(nextm);
      }
    }
  }

  ans += d[cheese[j+1].first][cheese[j+1].second];
  
  for(a=1;a<=h;a++){
    for(b=1;b<=w;b++){
      memod[a][b] = memo[a][b];
      d[a][b] = 0;
    }
  }
  while(qu.size()){
    qu.pop();
  }

  }

  
  cout << ans << endl;
  return 0;
}