[NOIP simulation test]: go lattice (analog + BFS + Dijkstra)

Title Description

$ CYJ $ wanted to find his little friends $ FPJ $, $ CYJ $ and $ FPJ $ now located in a room, the room layout can be viewed as a matrix $ N $ lines $ M $ columns within each matrix element would be one of the following conditions:
$ 1 $ obstruction area - where there is a wall ( '\ #' $ is represented by $).
2. What is $ $ $ $ CYJ beginning in the region (indicated by $ $ 'C').
3. This is $ $ $ $ FPJ in area ( 'F' $ is represented by $).
$ 4 $ space area (by $ '.' $ Shown).
$ CYJ $ carries a so-called transfer gun thing, it is possible to create a portal of firearms, in every action, he can choose one of the following operations:
$ 1 $ moves to a neighboring grid (up, down, left, right, you can not move walls of the grid). This operation consumes one unit of time.
$ 2 $ steering a wall (need not be contiguous, only to face), launching its portal, the portal will stay in the wall facing your place (there is at most only two portals at the same time), when the wall there are already two on the portal, and you launch the third fan, so that an initial launch will disappear. At the same time, you can not be in a position to manufacture two portal (this operation will not be time-consuming).
$ 3 $ if he is adjacent to a front wall and have a portal, then he can move to another fan portal in front of the grid. This operation will be a time-consuming units.
$ CYJ $ want to know how much time is required to be able to at least reach the finish ($ 'F' $) from the starting point ($ 'C' $).
Please note: We guarantee edge of the map will be around the walls and there must be $ 'C' $, $ ' F' $.


Input Format

The first line of the input two positive integers $ N $ and $ M $, represents the size of the map.
The next $ N $ lines each with a length of $ M $ string representing the terrain.


Output Format

You need a minimum of time to reach the output end, if you can not reach your output $ "no" $.


Sample

Sample input 1:

4 4
####
#.F#
#C.#
####

Sample output 1:

2

Sample input 2:

6 8
########
#.##..F#
#C.##..#
#..#...#
#.....##
########

Sample input 2:

4

Sample input 3:

4 5
#####
#C#.#
###F#
#####

Sample output 3:

no


Data range and tips

$ $ 50% of the data satisfies: $. 4 \ N leqslant, M $ \ leqslant 15;
$% $ 100 of data satisfies: $ 4 \ leqslant N, M \ leqslant 500 $;


answer

This question is out of the coach a little embarrassed, I said I will stay there in multiple channels competing teams, then there's the story of this question in his profile.

About six months ago, $ Akoasm $ Great God out of this question, he wants to sell the copyright, I helped him to trial this question, but just six months after ......

Incidentally $ Akoasm attached a link to the god of $: $ \ mathcal {} $ Akoasm .

Nonsense is over, words Reformed turn.

This question is should I refereed $ Akoasm $ Great God out of the best a question.

The subject is very new, hard to think positive solution, a lot of people think of search + burst pruning, as few people think of the positive solution.

First of all, we have $ pretreatment distance from the nearest wall by $ BFS, in the pre-grid before the first wall down about where to build map, you can run $ Dijkstra $ on the map.

It's that simple, mostly hard to think of.


Code time

#include<bits/stdc++.h>
using namespace std;
struct rec
{
	int nxt;
	int to;
	int w;
}e[2222222];
int head[300000],cnt;
int n,m;
char ch[600];
int st,ed;
bool Map[600][600];
int bs[600][600],u[600][600],d[600][600],l[600][600],r[600][600];
bool vis[300000];
int dis[300000];
queue<pair<int,int> > que;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
void add(int x,int y,int w)
{
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	e[cnt].w=w;
	head[x]=cnt;
}
void insert(int x,int y,int w)
{
	if(Map[x][y]&&!bs[x][y])
	{
		bs[x][y]=w;
		que.push(make_pair(x,y));
	}
}
void pre_bfs()
{
	while(!que.empty())
	{
		pair<int,int> flag=que.front();
		que.pop();
		insert(flag.first-1,flag.second,bs[flag.first][flag.second]+1);
		insert(flag.first,flag.second-1,bs[flag.first][flag.second]+1);
		insert(flag.first+1,flag.second,bs[flag.first][flag.second]+1);
		insert(flag.first,flag.second+1,bs[flag.first][flag.second]+1);
	}
}
void pre_work()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(Map[i][j])
			{
				if(!Map[i][j-1])l[i][j]=(i-1)*m+j;
				else l[i][j]=l[i][j-1];
				if(!Map[i-1][j])u[i][j]=(i-1)*m+j;
				else u[i][j]=u[i-1][j];
			}
	for(int i=n;i;i--)
		for(int j=m;j;j--)
			if(Map[i][j])
			{
				if(!Map[i][j+1])r[i][j]=(i-1)*m+j;
				else r[i][j]=r[i][j+1];
				if(!Map[i+1][j])d[i][j]=(i-1)*m+j;
				else d[i][j]=d[i+1][j];
			}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(Map[i][j])
			{
				if(Map[i][j+1]){add((i-1)*m+j,(i-1)*m+j+1,1);add((i-1)*m+j+1,(i-1)*m+j,1);}
				if(Map[i+1][j]){add((i-1)*m+j,i*m+j,1);add(i*m+j,(i-1)*m+j,1);}
				if(u[i][j]!=(i-1)*m+j)add((i-1)*m+j,u[i][j],bs[i][j]);
				if(d[i][j]!=(i-1)*m+j)add((i-1)*m+j,d[i][j],bs[i][j]);
				if(l[i][j]!=(i-1)*m+j)add((i-1)*m+j,l[i][j],bs[i][j]);
				if(r[i][j]!=(i-1)*m+j)add((i-1)*m+j,r[i][j],bs[i][j]);
			}
}
void Dij(int x)
{
	memset(dis,0x3f,sizeof(dis));
	q.push(make_pair(0,x));
	dis[x]=0;
	while(!q.empty())
	{
		int flag=q.top().second;
		q.pop();
		if(vis[flag])continue;
		vis[flag]=1;
		for(int i=head[flag];i;i=e[i].nxt)
			if(dis[e[i].to]>dis[flag]+e[i].w)
			{
				dis[e[i].to]=dis[flag]+e[i].w;
				q.push(make_pair(dis[e[i].to],e[i].to));
			}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",ch+1);
		for(int j=1;j<=m;j++)
		{
			if(ch[j]=='C')st=(i-1)*m+j;
			if(ch[j]=='F')ed=(i-1)*m+j;
			if(ch[j]=='#')que.push(make_pair(i,j));
			if(ch[j]=='C'||ch[j]=='F'||ch[j]=='.')Map[i][j]=1;
		}
	}
	pre_bfs();
	pre_work();
	Dij(st);
	printf("%d",dis[ed]);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11320354.html