Language: C++

//
//  0558.cpp
//  
//
//  Created by Yoshida Satoshi on 2017/11/16.
//
//

#include <iostream>
#include <queue>
#include <utility>
#include <stdlib.h>

#define MAX 1000
#define INF 100000000
#define FOR(index,max) for(index=0;index<max;index++)

using namespace std;

typedef pair<int, int> P;

int main(){
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    
    int nx,ny;
    
    queue<P> que;
    int H,W,N;
    int state[MAX][MAX];//0:????????°or????????? -1:????????¶???
    bool isVisited[MAX][MAX];
    int d[MAX][MAX];
    int ans=0;
    P start[9];
    P p;
    char in;
    cin>>H>>W>>N;
    FOR(i,H){
        FOR(j,W){
            cin>>in;
            if(in=='S'){
                start[0](i,j);
                state[i][j]=0;
            }
            else if(in=='X') state[i][j]=-1;
            else if(in=='.') state[i][j]=0;
            else{
                state[i][j]=0;
                start[atoi(in)](i,j);
                
            }
        }
    }
    
    FOR(k,N){
        FOR(i,H){
            FOR(j,W){
                isVisited[i][j]=false;
                d[i][j]=INF;
            }
        }
        que.push(start[k]);
        d[start[k].first][start[k].second]=0;
        while(que.size()){
            p=que.front();
            que.pop();
            
            if(p==start[k+1])   break;
            
            FOR(l,4){
                nx=p.first+dx[l];
                ny=p.seond+dy[i];
                
                if(!(nx<0||ny<0||nx>H-1||ny>W-1||state[nx][ny]==-1||d[nx][ny]==INF)){
                    que.push(P(nx,ny));
                    d[nx][ny]=d[p.first][p.second]+1;
                }
            }
        }
        
        ans += d[start[k+1].first][start[k+1].second];
    }
    
    cout<<ans<<endl;
    
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:38:9: error: 'i' was not declared in this scope; did you mean 'in'?
   38 |     FOR(i,H){
      |         ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:39:13: error: 'j' was not declared in this scope
   39 |         FOR(j,W){
      |             ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:49:28: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
   49 |                 start[atoi(in)](i,j);
      |                            ^~
      |                            |
      |                            char
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:9:
/usr/include/stdlib.h:105:30: note:   initializing argument 1 of 'int atoi(const char*)'
  105 | extern int atoi (const char *__nptr)
      |                  ~~~~~~~~~~~~^~~~~~
a.cc:55:9: error: 'k' was not declared in this scope
   55 |     FOR(k,N){
      |         ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:56:13: error: 'i' was not declared in this scope; did you mean 'in'?
   56 |         FOR(i,H){
      |             ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:57:17: error: 'j' was not declared in this scope
   57 |             FOR(j,W){
      |                 ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:70:17: error: 'l' was not declared in this scope
   70 |             FOR(l,4){
      |                 ^
a.cc:16:28: note: in definition of macro 'FOR'
   16 | #define FOR(index,max) for(index=0;index<max;index++)
      |                            ^~~~~
a.cc:72:22: error: 'P' {aka 'struct std::pair<int, int>'} has no member named 'seond'; did you mean 'second'?
   72 |                 ny=p.seond+dy[i];
      |                      ^~~~~
      |                      second
a.cc:72:31: error: 'i' was not declared in this scope; did you mean 'in'?
   72 |                 ny=p.seond+dy[i];
      |                               ^
      |                               in