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 <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <iterator>

using namespace std;

const int INF = 1000000;
typedef pair<int, int> P;
int w, h, n;
char f[1001][1001];
int d[1001][1001];
int ans;
int sx, sy;
int gx, gy;

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

void bfs()
{
   ans = 0;
   for (int i = 0; i < n; i++){
      queue<P> que;
      for (int y = 0; y < h; y++){
         for (int x = 0; x < w; x++){
            d[x][y] = INF;
         }
      }
      sx = chx[i];
      sy = chy[i];
      gx = chx[i+1];
      gy = chy[i+1];
      que.push(P(sx, sy));
      d[sx][sy] = 0;
      while (que.size()){
         P p = que.front(); que.pop();
         if (p.first == gx && p.second == gy) break;
         for (int j = 0; j < 4; j++){
            int nx = p.first + dx[j], ny = p.second + dy[j];
            if (0 <= nx && nx <= w && 0 <= ny && ny <= h && d[nx][ny] == INF && f[nx][ny] != 'X'){
               que.push(P(nx, ny));
               d[nx][ny] = d[p.first][p.second] + 1;
            }
         }
      }
      ans += d[gx][gy];
   }
   cout << ans << endl;
}


int main()
{
   cin >> h >> w >> n;
   for (int y = 0; y < h; y++){
      for (int x = 0; x < w; x++){
         cin >> f[x][y];
         if (f[x][y] == 'S'){
            chy[0] = y;
            chx[0] = x;
         } else if ('1' <= f[x][y] && f[x][y] <= '9'){
            chy[f[x][y] - '0'] = y;
            chx[f[x][y] - '0'] = x;
         }
      }
   }
   bfs();
   return 0;
}

C++: #include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
inline bool validate(int x, int y, int H, int W) {
  return 0 <= x && x < H && 0 <= y && y < W;
}
int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vector<string> v(H);
  for (int i = 0; i < H; i++)
    cin >> v[i];
  int sx = 0, sy = 0;
  for (int i = 0, f = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (v[i][j] == 'S') {
        f = 1;
        sx = i;
        sy = j;
        break;
      }
    }
    if (f)
      break;
  }
 
  int cheese = N;
  int dst = 0;
  int life = 1;
  while (cheese > 0) {
    vector<vector<int>> w(H, vector<int>(W, INT_MAX));
    queue<pair<int, int>> q;
    q.push(make_pair(sx, sy));
    w[sx][sy] = 0;
    while (!q.empty()) {
      const int tbl[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
      pair<int, int> p = q.front();
      q.pop();
      const int x = p.first, y = p.second;
      if ('1' <= v[x][y] && v[x][y] <= '9') {
        if (life >= (v[x][y] - '0')) {
          life++;
          cheese--;
          v[x][y] = '.';
          
          sx = x, sy = y;
          dst += w[x][y];
break;
        }
      }
      for (int i = 0; i < 4; i++) {
        int nx = x + tbl[i][0], ny = y + tbl[i][1];
        if (validate(nx, ny, H, W)) {
          if (v[nx][ny] != 'X' && w[nx][ny] == INT_MAX) {
            q.push(make_pair(nx, ny));
            w[nx][ny] = w[x][y] + 1;
          }
        }
      }
    }
  }
  cout << dst << endl;
 
  return 0;
}

C++: //14:54
#include <stdio.h>
#include <queue>

using namespace std;

typedef pair<int,int> P;

int h, w, n;
char field[1000][1001];
queue<P> qu;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int d[1000][1000];


void init(){
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      d[i][j] = -1;
    }
  }
  queue<P> empty;
  swap(qu,empty);
}

int solve(){
  int hp = 1;
  int cnt;
  init();
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(field[i][j] == 'S'){
        qu.push(P(i,j));
        d[i][j] = 0;
      }
    }
  }
  while(qu.size()){
    P p = qu.front();
    qu.pop();
    int y = p.first;
    int x = p.second;
    if(hp == n + 1)
      return d[y][x];
    if(hp == field[y][x] - '0'){
      hp++;
      cnt = d[y][x];
      init();
      d[y][x] = cnt;
    }
    for(int i = 0; i < 4; i++){
      int ny = y + dy[i];
      int nx = x + dx[i];
      if(ny >= 0 && ny < h && nx >= 0 && nx < w && field[ny][nx] != 'X' && d[ny][nx] == -1){
        d[ny][nx] = d[y][x] + 1;
        qu.push(P(ny,nx));
      }
    }
  }
  return 0;
}

int main(){
  scanf("%d %d %d", &h, &w, &n);
  for(int i = 0; i < h; i++){
    scanf("%s", field[i]);
  }
  printf("%d\n", solve() - 1);
  return 0;
}

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

  cin>>H>>W>>N;
  for(i=0;i<H;i++)cin>>field[i];
  for(i=0;i<H;i++){
    for(j=0;j<W;j++){
      if(field[i][j]=='S'){
	Point p={j,i,0};
	q.push(p);
      }
    }
  }
  for(i=0;i<H;i++)for(j=0;j<W;j++)visited[i][j]=false;

  while(q.size()){
    Point p=q.front();
    q.pop();
    visited[p.y][p.x]=true;
    if(field[p.y][p.x]==vital){
      if(vital-'0'==N){
	ans=p;
	break;
      }
      vital++;
      while(q.size())q.pop();
      for(i=0;i<H;i++)for(j=0;j<W;j++)visited[i][j]=false;
    }
    for(i=0;i<4;i++){
      Point p2;
      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;
      q.push(p2);
    }
  }
  cout<<ans.dist<<endl;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
int map[1002][1002]={0},r[1002][1002]={0};
struct A{
int t,h,x,y;
};
int main(){
int h,w,n,i,j,m=0,g[2],k=0,b;
g[0]=1;
g[1]=-1;
char s[1001];
A a;
queue<A> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
map[i][0]=-1;
map[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
map[0][i]=-1;
map[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",s);
	for(j=0;j<=w;j++){
		b=s[j];
		if(b>48&&b<58)
			map[i][j+1]=b-48;
		else if(s[j]=='S'){
			a.x=i;
			a.y=j+1;
			a.t=0;
			a.h=1;
			q.push(a);
		}
		else if(s[j]=='X')
			map[i][j+1]=-1;
	}
}
while(!q.empty()){
	a=q.front();
	q.pop();
	if(map[a.x][a.y]==a.h){
		a.h++;
		k=0;
		while(!q.empty()){
			q.pop();
		}
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				r[i][j]=0;
			}
		}
	}
	k++;
	r[a.x][a.y]=k;
	if(a.h==n+1){
		m=a.t;
		break;
	}
	a.t++;
	for(i=0;i<2;i++){
		if(map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]>k||map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]==0){
			a.x+=g[i];
			q.push(a);
			a.x-=g[i];
		}
		if(map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]>k||map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]==0){
			a.y+=g[i];
			q.push(a);
			a.y-=g[i];
		}
	}
}
cout<<m<<endl;
return 0;
}