跳到正文

题解

题解:P4668 [BalticOI 2011] Treasures and Vikings (Day1)

easy42约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
获赞
1
题目
P4668

版本

  1. v1当前

注意到维京船如果和我们同一行或同一列并且中间没有岛,那么我们就死了。

其实我们通过仔细观察可以发现,其实维京船追踪我们这个操作很难实现,其实我们走到一个点时,可以判断维京船能不能过来击杀我们,这样子就很好实现。

注意到这样子我们需要判断维京船到底能不能击杀我们,所以需要维京船到每个点的最短路径,这样子如果我们每走到一个点,就可以直接判断维京船可不可以花费的时间比我们少到达这一行或者一列并且中间没有岛。

好的,可能讲的比较抽象,来梳理一下:

  1. bfs 维京船到每个点的最短路径。
  2. bfs 我们到宝藏点的路径,途中需要判断大量合法性操作。

我们大概是以我们为视角,转变为了维京船为视角,在一些题中这种方法大概是经常出现的。

最后放一下代码,细节过多。

C++
#include<bits/stdc++.h>
using namespace std;
int n,m;
char c[1005][1005];
int t[1005][1005];
bool b1[1005][1005],b2[1005][1005];
struct edge{
	int x,y,cnt;
};
queue<edge>q;
int vx,vy,yx,yy,tx,ty;
int f[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void print(int x,int y){
	cout<<x<<" "<<y<<endl;
}
bool check(int x,int y){
	return x>=1&&x<=n&&y>=1&&y<=m&&c[x][y]!='I';
}
bool tcheck(int x,int y,int k){
	for(int i=y;i>=1;i--){
		if(c[x][i]!='I'){
			if(k>=t[x][i]) return false;
		}
		else break;
	}
	for(int i=y+1;i<=m;i++){
		if(c[x][i]!='I'){
			if(k>=t[x][i]) return false;
		}
		else break;
	}
	for(int i=x;i>=1;i--){
		if(c[i][y]!='I'){
			if(k>=t[i][y]) return false;
		}
		else break;
	}
	for(int i=x+1;i<=n;i++){
		if(c[i][y]!='I'){
			if(k>=t[i][y]) return false;
		}
		else break;
	}
	return true;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>c[i][j];
			if(c[i][j]=='V') vx=i,vy=j;
			if(c[i][j]=='Y') yx=i,yy=j;
			if(c[i][j]=='T') tx=i,ty=j;
		}
	}
	t[vx][vy]=0;
	b1[vx][vy]=1;
	q.push({vx,vy,0});
	while(!q.empty()){
		int x=q.front().x,y=q.front().y,cnt=q.front().cnt;
		q.pop();
		t[x][y]=cnt;
		for(int i=0;i<4;i++){
			int hx=x+f[i][0];
			int hy=y+f[i][1];
			if(!b1[hx][hy]&&check(hx,hy)) q.push({hx,hy,cnt+1}),b1[hx][hy]=1;
		}
	}
	while(!q.empty()) q.pop();
	b2[yx][yy]=1;
	q.push({yx,yy,0});
	while(!q.empty()){
		int x=q.front().x,y=q.front().y,cnt=q.front().cnt;
		q.pop();
		if(x==tx&&y==ty) cout<<"YES",exit(0);
		for(int i=0;i<4;i++){
			int hx=x+f[i][0];
			int hy=y+f[i][1];
			if(!b2[hx][hy]&&check(hx,hy)&&tcheck(hx,hy,cnt+1)) q.push({hx,hy,cnt+1}),b2[hx][hy]=1;
		}
	}
	cout<<"NO";
	return 0;
}
原文