题目描述
注意行和列 行是C,列是R,坑死了。
样例
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 155;
char a[N][N];
int R, C;
int ans = 0;
pair<int, int> cao;
pair<int, int> ma;
void bfs()
{
queue<pair<int, int>> q;
q.push(ma);
while (!q.empty())
{
bool flag = false;
int sz = q.size();
while (sz--)
{
auto tmp = q.front();
q.pop();
if (tmp == cao)
{
flag = true;
break;
}
int x = tmp.first;
int y = tmp.second;
if (x < 0 || x >= C || y < 0 || y >= R || a[x][y] == '*')
continue;
a[x][y] = '*';
//cout << x << " " << y <<endl;
static int pos[8][2] = { { -2, 1 }, { 1, 2 }, { 1, -2 }, { -1, 2 }, { -1, -2 }, { -2, -1 }, { 2, 1 }, { 2, -1 } };
for (int i = 0; i < 8; i++)
{
int nx = x + pos[i][0];
int ny = y + pos[i][1];
q.push({ nx, ny });
}
}
if (flag)
break;
ans++;
}
}
int main()
{
cin >> R >> C;
for (int i = 0; i < C; i++)
{
for (int j = 0; j < R; j++)
{
cin >> a[i][j];
if (a[i][j] == 'H')
{
cao = { i, j };
}
if (a[i][j] == 'K')
{
ma = { i, j };
}
}
}
bfs();
// cout << cao.first << " " << cao.second <<endl;;
// cout << ma.first << " " << ma.second << endl;;
cout << ans << endl;
// system("pause");