大美丽回家

image

解题思路

  • 走过的路可以再走
    1.投机取巧:挑选一个合适的值x(1~200*200,我随便选了200),判断访问该点的次数小于x就可以继续访问,大于x就表示访问了太多次,不能访问了
    2.好好做:就算一个点被访问了多次,但只要每次访问的刀不一样就行了
    (这里我更倾向于第一种)
  • 请使用bfs
  • 由于走过的路可以再走,所以我们可以把vis改成计数器数组(记录访问次数)

code

#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[205][205];
struct node {
int x, y;
int dao, cnt;
node(int a, int b, int c, int d): x(a), y(b), dao(c), cnt(d) {};
};
int vis[205][205];
int sx, sy;
int ex, ey;
queue<node> q;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
getchar();
for (int j = 1; j <= n; j++) {
s[i][j] = getchar();
if (s[i][j] == '@')
sx = i, sy = j;
if (s[i][j] == '*')
ex = i, ey = j;
}
}
vis[sx][sy] = true;
q.push(node(sx, sy, k, 0));
while (q.size()) {
node t = q.front();
q.pop();
if (t.x == ex && t.y == ey) {
cout << t.cnt;
return 0;
}
for (int i = 0; i < 4; i++) {
int xx = t.x + dx[i];
int yy = t.y + dy[i];
if (xx == ex && yy == ey) {
cout << t.cnt + 1;
return 0;
}
if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && vis[xx][yy] <= 200) {
if (s[xx][yy] == 'x') {
if (t.dao == 0) {
continue;
}
q.push(node(xx, yy, t.dao - 1, t.cnt + 1));
vis[xx][yy]++;
} else {
q.push(node(xx, yy, t.dao, t.cnt + 1));
vis[xx][yy]++;
}
}
}
}
cout << -1;
return 0;
}

posted on   可爱楷玩算法  阅读(13)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示