题解区里都是一次性走两步
这里我就说一点
按普通(每次走一步)的来最后\(+1\)再除以\(2\)就行了
另外输入时加个时间限制 (\(q++ if(q>=w)break;\))
/* ID:death_r2 TASK:maze1 LANG:C++ */ #include <queue> #include <cstdio> #include <string> #include <iostream> #include <algorithm> using namespace std; #define reg register int #define isdigit(x) ('0' <= x&&x <= '9') template<typename T> inline T Read(T Type) { T x = 0,f = 1; char a = getchar(); while(!isdigit(a)) {if(a == '-') f = -1;a = getchar();} while(isdigit(a)) {x = (x << 1) + (x << 3) + (a ^ '0');a = getchar();} return x * f; } string mp[210]; bool vis[3][210][210]; int cnt,ans,d[3][210][210],dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}},w,h; pair<int,int> s[3]; #define pii_pii pair<int,pair<int,int> > inline void bfs(pair<int,int> s,int index) { priority_queue<pii_pii,vector<pii_pii >,greater<pii_pii > > q; q.push(make_pair(0,s)); vis[index][s.first][s.second] = 1; while(!q.empty()) { pii_pii it = q.top();q.pop(); for(reg i = 0;i < 4;i++) { int x = it.second.first + dir[i][0]; int y = it.second.second + dir[i][1]; if(x < 1||x > h||y < 0||y >= w||mp[x][y] != ' '||vis[index][x][y]) continue; d[index][x][y] = d[index][it.second.first][it.second.second] + 1; vis[index][x][y] = 1; q.push(make_pair(d[index][x][y],make_pair(x,y))); } } } int main() { // freopen("maze1.in","r",stdin); // freopen("maze1.out","w",stdout); w = Read(1) << 1 ^ 1,h = Read(1) << 1 ^ 1; for(reg i = 1;i <= h;i++) { char x = getchar(); while(x != ' '&&x != '|'&&x != '+'&&x != '-') x = getchar(); int q = 0; while(x != '\n') { ++q; mp[i] += x; x = getchar(); if(q >= w) break; } } for(reg i = 1;i <= h;i++) while(mp[i].size() < w) mp[i] += ' '; for(reg i = 0;i < w;i++) { if(cnt == 2) break; if(mp[1][i] == ' ') s[++cnt] = make_pair(1,i); if(mp[h][i] == ' ') s[++cnt] = make_pair(h,i); } for(reg i = 1;i <= h;i++) { if(cnt == 2) break; if(mp[i][0] == ' ') s[++cnt] = make_pair(i,0); if(mp[i][w - 1] == ' ') s[++cnt] = make_pair(i,w - 1); } bfs(s[1],1),bfs(s[2],2); for(reg i = 1;i <= h;i++) for(reg j = 0;j < w;j++) ans = max(ans,min((d[1][i][j] + 1) / 2,(d[2][i][j] + 1) / 2)); printf("%d\n",ans); return 0; }
来源:https://www.cnblogs.com/resftlmuttmotw/p/11965579.html