跳到正文

题解

题解:P14469 [COCI 2025/2026 #1] 皇后 / Kraljica

L_J_R约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年11月27日
收录
2026年8月11日
更新
2026年11月9日
篇幅
约 3 分钟
获赞
5
题目
P14469

版本

  1. v1当前

题解:P14469[COCI 2025/2026 #1] 皇后 / Kraljica

思路

很容易想到 BFS,特殊点在于有 88 个方向,且每个方向要跑到边界或障碍物,遇到走过的格子(即 visi,j=1vis_{i,j} = 1)也不要 break!continue 就行了,具体见代码 4949 行。

代码如下↓

C++
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

int n, m, ans = 0x3f3f3f3f;
char c[1005][1005];
struct node
{
	int x, y, cnt;
}; queue<node> q;
bool vis[1005][1005];
vector<node> v[10];

bool chk(int x, int y)
{
	if(x > n || y > m || x < 1 || y < 1) return false;
	if(c[x][y] == '#') return false;
	return true;
}

int main()
{
	cin >> n >> m; cin.get();
	for(int i = 1; i <= n; i++) 
	{
		scanf("%s", c[i] + 1);
		for(int j = 1; j <= m; j++)
		{
			if(c[i][j] == 'S') q.push({i, j, 0}), vis[i][j] = true;
			if(c[i][j] >= '0' && c[i][j] <= '9')
				v[(c[i][j] - '0')].push_back({i, j, -1});
		}
	}
	while(!q.empty())
	{
		node u = q.front(); q.pop();
		if(c[u.x][u.y] == 'E')
		{
			ans = min(ans, u.cnt);
			break;
		}
		for(int i = 0; i < 8; i++)
		{
			int t = 1;
			while(true)
			{
				int nx = u.x + t * dx[i], ny = u.y + t * dy[i]; t++;
				if(vis[nx][ny]) continue; if(!chk(nx, ny)) break;
				vis[nx][ny] = true; q.push({nx, ny, u.cnt + 1});
				if(c[nx][ny] >= '1' && c[nx][ny] <= '9')
				{
					int tmp = c[nx][ny] - '0';
					for(auto k : v[tmp])
						if(k.x != nx || k.y != ny)
						{
							if(vis[k.x][k.y]) continue;
							q.push({k.x, k.y, u.cnt + 1});
							vis[k.x][k.y] = true;
						}
				}
			}
		}
	}
	if(ans != 0x3f3f3f3f) cout << ans << endl;
	else cout << -1 << endl;
	return 0;
}
原文
存档评论1 条抓取于