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<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> P;

char d[1000][1000];
int data[1000][1000];
int h,w,n;
int sx[4]={1,0,-1,0};
int sy[4]={0,-1,0,1};

int check(int x1,int y1,int x2,int y2){
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)data[i][j]=-1;
	data[y1][x1]=0;
	queue<P> q;
	q.push(P(x1,y1));
	while(q.size()){
		P p=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=p.first+sx[i],y=p.second+sy[i];
			if(x<0||x>=w||y<0||y>=h)continue;
			if(d[y][x]=='X'||data[y][x]>=0)continue;
			q.push(P(x,y));
			data[y][x]=data[p.second][p.first]+1;
			if(x==x2&&y==y2)return data[y][x];
		}
	}
}

int main(){
	int x[10],y[10];
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<h;i++){
		scanf("%s",d[i]);
		for(int j=0;j<w;j++){
			if(d[i][j]=='S'){ x[0]=j,y[0]=i;}
			if(48<d[i][j]&&d[i][j]<58){ x[d[i][j]-48]=j,y[d[i][j]-48]=i;}
		}
	}
	int res=0;
	for(int i=0;i<n;i++) res+=check(x[i],y[i],x[i+1],y[i+1]);
	printf("%d\n",res);
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int h,w,n;
char route[1050][1050];
int dirx[4]={0,-1,0,1};
int diry[4]={-1,0,1,0};
int vis[1005][1005];
struct maze
{
    int x,y;
    int time;
};
maze BFS(int dx,int dy,char ch)
{
    int xx,yy;
    queue<maze>q;
    memset(vis,0,sizeof(vis));
    maze a,b;
    a.x=dx;
    a.y=dy;
    a.time=0;
    q.push(a);
    vis[dx][dy]=1;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(route[a.x][a.y]==ch)
            return a;
        for(int i=0;i<4;i++)
        {
            xx=a.x+dirx[i];
            yy=a.y+diry[i];
            if(xx>=0&&xx<h&&yy>=0&&yy<w&&route[xx][yy]!='X')
            {
                b.x=xx;
                b.y=yy;
                b.time=a.time+1;
                if(!vis[xx][yy])
                {
                    q.push(b);
                    vis[xx][yy]=1;
                }
            }
        }
    }
}
int main()
{
    int dex=0,dey=0,tim;
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        getchar();
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                scanf("%c",&route[i][j]);
                if(route[i][j]=='S')
                {
                    dex=i;
                    dey=j;
                    route[i][j]='0';//0起点开始
                }
            }
            getchar();
        }
        maze st;
        tim=0;
        for(int i=0;i<n;i++)//分段搜索最优路径，每段最优路径相加即为最终的最优路径
        {
            st=BFS(dex,dey,route[dex][dey]+1);
            tim+=st.time;
            dex=st.x;
            dey=st.y;
        }
        printf("%d\n",tim);
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#define MAX 1002
using namespace std;

char xy[MAX][MAX];
int cost[MAX][MAX];
int hp[MAX][MAX];

struct state{
  int x;
  int y;
  int co;
  int hp;
  state(){}
  state(int x,int y,int co,int hp):
    x(x),y(y),co(co),hp(hp){}
};

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

int H,W,N;

int solve(int x,int y){
  int i,j;
  queue<state> q;
  q.push( state(x,y,0,1) );
  cost[x][y]=0;
  hp[x][y]=1;

  while( !(q.empty()) ){

    state p = q.front();
    q.pop();

    x=p.x;
    y=p.y;

    for(i=0;i<4;i++){

      if( x+dx[i] < 0 || x+dx[i] >= W || y+dy[i] < 0 || y+dy[i] >= H )
	continue;

      if(xy[y+dy[i]][x+dx[i]]=='X') continue;

      if(xy[y+dy[i]][x+dx[i]]-'0' == p.hp){
	
	if(p.hp >= N){
	  return p.co+1;
	}
	
	while( !(q.empty()) ){
	  q.pop();
	}

	//printf("%d\n",p.hp+1);
	
	q.push( state(x+dx[i],y+dy[i],p.co+1,p.hp+1) );
	
	break;
	
      }


      if(cost[x+dx[i]][y+dy[i]]<=p.co+1){
	if(hp[x+dx[i]][y+dy[i]]>=p.hp){
	  continue;
	}
      }
      
      
      cost[x+dx[i]][y+dy[i]]=p.co;
      hp[x+dx[i]][y+dy[i]]=p.hp;

      q.push( state(x+dx[i],y+dy[i],p.co+1,p.hp) );


    }

  }

  return -1;
}

main(){
  int i,j,sx,sy;

  cin >> H >> W >> N;

  for(i=0;i<H;i++){
    scanf("%s",xy[i]);
    for(j=0;j<W;j++){
      if(xy[i][j]=='S'){
	sx=j;
	sy=i;
      }
    }
  }

  int n=solve(sx,sy);

  cout << n << endl; 
  
  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;

int used[1000][1000];
int field[1000][1000];
int mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0};
int main(){
  int h, w, n, sx, sy, ans = 0;
  scanf("%d%d%d", &h, &w, &n);
  for(int i = 0;i < h;i++){
    for(int j = 0;j < w;j++){
      char c;
      scanf(" %c", &c);
      if(c =='.' || c == 'S')field[i][j] = 0;
      else if(c == 'X')field[i][j] = -1;
      else field[i][j] = c - '0';
      if(c == 'S')sx = i, sy = j;
    }
  }
  for(int goal = 1;goal <= n;goal++){
    memset(used, 0, sizeof(used));
    queue< pair<int, int> > qu;
    qu.push(make_pair(sx, sy));
    bool fin = false;
    while(!fin){
      ans++;
      for(int i = qu.size();i > 0 && !fin;i--){
	int tx = qu.front().first;
	int ty = qu.front().second;
	qu.pop();
	for(int j = 0;j < 4 && !fin;j++){
	  int nx = tx + mx[j];
	  int ny = ty + my[j];
	  if(nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
	  if(field[nx][ny] == -1 || used[nx][ny] != 0)continue;
	  used[nx][ny] = 1;
	  if(field[nx][ny] == goal){
	    fin = true;
	    sx = nx;
	    sy = ny;
	  }
	  qu.push(make_pair(nx, ny));
	}
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}