Skip to content

Instantly share code, notes, and snippets.

@IKKO-Ohta
Created June 30, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IKKO-Ohta/0cbd8e38dc8f8f2c16db51243886266c to your computer and use it in GitHub Desktop.
Save IKKO-Ohta/0cbd8e38dc8f8f2c16db51243886266c to your computer and use it in GitHub Desktop.
#define REP(i,n) for (int i=0;i<(n);i++)
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
const int MAX = 100;
const int INF = 10000;
int N,M;
int sx,sy;
int gx,gy;
char field[MAX][MAX+1];
int d[MAX][MAX+1];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
typedef pair<int,int> P;
int bfs(){
REP(i,N){
REP(j,M){
d[i][j] = INF;
}
}
queue <P> que;
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;
// 周辺4マスを調べる
for(int r = 0; r < 4; r++) {
int nx = p.first + dx[r], ny = p.second + dy[r];
if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' &&d[nx][ny] == INF){
d[nx][ny] = d[p.first][p.second] + 1;
que.push(P(nx,ny));
}
}
}
return d[gx][gy];
}
void solve(){
int ans = bfs();
cout << ans << endl;
}
int main(){
cin >> N >> M;
cin >> sx >> sy;
cin >> gx >> gy;
sx --; sy --; gx --; gy --;
REP(i,N){
REP(j,M){
cin >> field[i][j];
}
}
solve();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment