Acstorm @ 2024-02-20 11:55:29
#include<bits/stdc++.h>
using namespace std;
#define int long long
bool vis[101][101];
struct node
{
int x , y;
}answer[10001];
int dire[101][101];
int endx , endy;
int n;
int dx[] = {0 , 0 , -1 , 1};
int dy[] = {1 , -1 , 0 , 0};
void bfs()
{
int st = 1 , op = 1;
while(st <= op)
{
for(int i = 0;i < 4;++ i)
{
for(int j = 1;true;++ j)
{
int tx = answer[st].x + dx[i] * j;
int ty = answer[st].y + dy[i] * j;
if(vis[tx][ty])
break;
if(tx < 1 || ty < 1 || tx > n || ty > n)
break;
vis[tx][ty] = true;
dire[tx][ty] = dire[answer[st].x][answer[st].y] + 1;
answer[++ op].x = tx;
answer[op].y = ty;
}
}
++ st;
}
}
signed main()
{
cin >> n;
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= n;++ j)
{
char f;
cin >> f;
if(f == 'A')
{
answer[1].x = i;
answer[1].y = j;
vis[i][j] = true;
}
else if(f == 'B')
{
endx = i;
endy = j;
}
else if(f == 'x')
{
vis[i][j] = true;
}
}
bfs();
if(!vis[endx][endy])
{
printf("-1");
return 0;
}
printf("%d" , dire[endx][endy] - 1);
return 0;
}
机房有人基本相同代码AC
by Brad_Z @ 2024-04-13 10:02:19
int dx[] = {1 , 0 , 0 , -1};
int dy[] = {0 , 1 , -1 , 0};
改一下顺序,就过了