2020 camp-day-
bfs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <cstdio> #include <utility> #include <queue> #define RE register #define FOR(i,a,b) for(RE int i=a;i<=b;++i) #define ROF(i,a,b) for(RE int i=a;i>=b;--i) #define P pair<int,int> #define sc(n) scanf("%d",&n) using namespace std; const int maxn = 102; struct Horse { P p; int c = 0; }horse; int mp[maxn][maxn], ans[maxn][maxn], n, m; int d[][2] = { {1,2},{-1,2},{1,-2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1} }; char s[maxn]; queue<Horse> qu; inline bool check( int k) { P p = horse.p; p.first += d[k][0], p.second += d[k][1]; if (p.first<1 || p.first>n || p.second<1 || p.second>m || mp[p.first][p.second]) return false ; if (k < 2) { if (mp[horse.p.first][horse.p.second + 1] == 2) return false ; } else if (k < 4) { if (mp[horse.p.first][horse.p.second - 1] == 2) return false ; } else if (k < 6) { if (mp[horse.p.first + 1][horse.p.second] == 2) return false ; } else if (mp[horse.p.first - 1][horse.p.second] == 2) return false ; return true ; } int main() { sc(n); sc(m); FOR(i, 1, n) { scanf ( "%s" , s + 1); FOR(j, 1, m) { ans[i][j] = -1; if (s[j] == '.' ); else if (s[j] == 'M' )horse.p = { i,j }, mp[i][j] = 1, qu.push(horse), ans[i][j] = 0; else mp[i][j] = 2; } } while (!qu.empty()) { horse = qu.front(); qu.pop(); FOR(i, 0, 7) if (check(i)) { Horse h = horse; h.p.first += d[i][0], h.p.second += d[i][1]; ++h.c; qu.push(h); mp[h.p.first][h.p.second] = 1; ans[h.p.first][h.p.second] = h.c; } } FOR(i, 1, n - 1) { FOR(j, 1, m - 1) printf ( "%d " , ans[i][j]); printf ( "%d\n" , ans[i][m]); } FOR(j, 1, m - 1) printf ( "%d " , ans[n][j]); printf ( "%d" , ans[n][m]); return 0; } |
本文作者:洛绫璃
本文链接:https://www.cnblogs.com/2aptx4869/p/12216739.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步