#23547: 為何RE @@


s10806226@smail.ycsh.tp.edu.tw (Eri)

學校 : 臺北市立永春高級中學
編號 : 108040
來源 : [101.3.116.101]
最後登入時間 :
2022-05-11 14:14:13
b059. 4. 靈犬尋寶 -- 95學年度全國資訊學科能力競賽 | From: [219.91.14.225] | 發表日期 : 2020-11-26 23:39

實在想不透 為何會RE

 

程式碼如下:(-1為未經過,-2為障礙)

#include <bits/stdc++.h>

using namespace std;

 

#define FAST ios::sync_with_stdio(false);cin.tie(0)

#define Fr(i,s,e) for(auto i = s ; i < e ; i++)

#define endl '\n'

#define Ft first

#define Sd second

 

typedef pair<int, int> pii;

 

const int dx[8] = {3, 3, 1, -1, -3, -3, 1, -1};

const int dy[8] = {1, -1, -3, -3, 1, -1, 3, 3};

const int obstx[8] = {1, 1, 0, 0, -1, -1, 0, 0};

const int obsty[8] = {0, 0, -1, -1, 0, 0, 1, 1};

 

const int MAX = 100;

 

int main(){

FAST;

 

int n, sx, sy, ex, ey, mp[MAX][MAX];

while(cin >> n){

Fr(i,0,MAX)Fr(j,0,MAX)mp[i][j] = -1 ;

Fr(i,0,n){

int x, y; cin >> x >> y;

mp[y][x] = -2;

}

cin >> sx >> sy >> ex >> ey;

auto bfs = [&](int isx, int isy, int iex, int iey){

auto valid = [&](int x, int y){

return x >= 0 and y >= 0 and x < MAX and y < MAX;

};

queue<pii> qu{};

qu.push({isx, isy});

mp[isy][isx] = 0;

bool flag = true;

while(qu.size() and flag){

int x = qu.front().Ft, y = qu.front().Sd; qu.pop();

Fr(i,0,8){

int nx = x + dx[i], ny = y + dy[i];

int ox = obstx[i], oy = obsty[i];

if(valid(nx, ny) and mp[oy][ox] == -1){

qu.push({nx, ny});

mp[ny][nx] = mp[y][x] + 1;

}

if(nx == iex and ny == iey){

flag = false;

break;

}

}

}

};

bfs(sx, sy, ex, ey);

if(mp[ey][ex] == -1)cout << "impossible" << endl;

else cout << mp[ey][ex] << endl; 

}

return 0;

}

 
ZeroJudge Forum