Created
April 24, 2015 15:16
-
-
Save akahana-1/befab6755192296e9e1d to your computer and use it in GitHub Desktop.
解けないなあ……
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <queue> | |
#include <utility> | |
#include <map> | |
using namespace std; | |
const int Y = 50, X = 50, GUARD = 1; | |
typedef pair<int, int> P; | |
int bfs(const int (*board)[Y + 2], P t, P k){ | |
int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1}; | |
// pair<P, P> -> first = t, second = k | |
queue<pair<P, P> > q; | |
map<pair<P, P>, int> visited; | |
q.push(make_pair(t, k)); | |
visited[make_pair(t, k)] = 0; | |
while(!q.empty()){ | |
pair<P, P> picked = q.front(); | |
q.pop(); | |
if(visited[picked] >= 100) break; | |
P ct = picked.first, ck = picked.second; | |
if(ct == ck) return visited[picked]; | |
for(int i = 0;i < 4;++i){ | |
pair<P, P> nextp; | |
P nextt, nextk; | |
int tdx, tdy, kdx, kdy; | |
int ntx, nty, nkx, nky; | |
tdx = dx[i]; | |
tdy = dy[i]; | |
kdx = -tdx; | |
kdy = -tdy; | |
ntx = ct.first + tdx; | |
nty = ct.second + tdy; | |
nkx = ck.first + kdx; | |
nky = ck.second + kdy; | |
nextt = board[ntx][nty] != GUARD ? P(ntx, nty) : P(ct.first, ct.second); | |
nextk = board[nkx][nky] != GUARD ? P(nkx, nky) : P(ck.first, ck.second); | |
nextp = make_pair(nextt, nextk); | |
if(visited[nextp] == 0){ | |
q.push(nextp); | |
visited[nextp] = visited[picked] + 1; | |
} | |
} | |
} | |
return -1; | |
} | |
int main(){ | |
int board[X + 2][Y + 2]; | |
int x, y, a, res; | |
P t, k; | |
while(cin >> x >> y, x && y){ | |
for(int i = 0;i <= X + 1;++i){ | |
fill(board[i], board[i] + (Y + 2), GUARD); | |
} | |
cin >> t.first >> t.second; | |
cin >> k.first >> k.second; | |
for(int i = 1;i <= y;++i){ | |
for(int j = 1;j <= x;++j){ | |
cin >> a; | |
board[i][j] = a; | |
} | |
} | |
res = bfs(board, t, k); | |
if(res != -1) cout << res << endl; | |
else cout << "NA" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment