GDUT_ winter training solution to a problem report _ topic I_G personal problem solution to a problem report

GDUT_ winter training solution to a problem report _ topic I_G personal problem solution to a problem report

topic:

You want to play a new role-playing. FIG world map by a n × m th cells form the mesh representation. Any role in a cell in a can move from the cell to the four directions - left, right, forward and backward movement of cells, but can not leave the world map.

In some monsters living cell. If at some point, you may be in a more small step or a step d number of monsters close to his cell, he immediately ran over to kill you.

You have to live from one unit to another unit of the game field. Determine the feasibility and, if applicable, minimum number of steps required to perform this operation number is found.

Entry

The first row contains three non-negative integer n, m, and d (0 <= n * m <= 200000,0 <= d <= 200000) - maximum distance and size of the monster dangerous FIG.

The following n rows each containing m characters. These characters may be equal to 'S', '.', 'M', 'F' 'start "and" done "cell is empty, and only appears once in the input.

Export

If starting from the end of the unit activates the unit, the output minimum number of steps required to perform this operation. Otherwise, output "-1."

Sample:
the INPUT

5 7 1
S.M…M


M…M…
…F

Output

12

Input

7 6 2
S…
…M…

…M

M…
…F

Output

11

Input

7 6 2
S…
…M…


…M
M…
…F

Output

-1

Input

4 4 2
M…
.S…

…F

Output

-1
subject maze bfs first shortest path obtained, but there are two problems:

1. The range of data, data are given n * m <= 2e5, it is possible to say n is 1, m is 2E5, then open a [2e5] [2e5] certainly not an array, would burst, so dynamic allocation memory, or keep two-dimensional array of one-dimensional array of information,

2.M this effect, if a point is simultaneously affect multiple M, will be repeated assignment problems, such complexity will go up, you can open a replica array to whether the record has been more recent M impact, but is reduced too most or use the queue: queues are put inside the impact, with the means to influence the coverage bfs Monster, and so can effectively prune, if all of the area of ​​influence is n, can be less than the first method step n .





Specifically: dynamic arrays easy to handle, the key is how to get the impact of M,

The first step, all the M are placed in queue inside, where I coordinate information stored in the form of structure

typedef struct coordinate
{int x_;int y_;}coor;

Queues into which such approach is to make the monster is d + 1, the remainder value is 0 then begin his diffusion:

else if(ch=='M')
			{
				all[time][time1]=d+1;
				coor co_;
				co_.x_=time;
				co_.y_=time1;
				que.push(co_);
			}

Can now begin, in a queue of all the monster inside, and then starts the surrounding every four lattice smaller than their 2, because if they are k, already the next k-1, already described to have monster which was affected, this time we will skip, not superfluous, if empathy is greater, indicating a more recent monster has exerted its influence, this time should be skipped.

while(!que.empty())
	{
		coor co=que.front();
		if((co.x_+1<n)&&all[co.x_+1][co.y_]<all[co.x_][co.y_]-1)
		{
			all[co.x_+1][co.y_]=all[co.x_][co.y_]-1;
			co.x_++;
			que.push(co);
			co.x_--;
		}
		if((co.x_-1>=0)&&all[co.x_-1][co.y_]<all[co.x_][co.y_]-1)
		{
			all[co.x_-1][co.y_]=all[co.x_][co.y_]-1;
			co.x_--;
			que.push(co);
			co.x_++;
		}
		if((co.y_+1<m)&&all[co.x_][co.y_+1]<all[co.x_][co.y_]-1)
		{
			all[co.x_][co.y_+1]=all[co.x_][co.y_]-1;
			co.y_++;
			que.push(co);
			co.y_--;
		}
		if((co.y_-1>=0)&&all[co.x_][co.y_-1]<all[co.x_][co.y_]-1)
		{
			all[co.x_][co.y_-1]=all[co.x_][co.y_]-1;
			co.y_--;
			que.push(co);
			co.y_++;
		}
		que.pop();
	}

This step is done, the rest is an ordinary two-dimensional space bfs find the shortest path, but my approach is to let the affected point 1 - d + 1, 0 point is not affected, how to represent the path the length of it, I used the negative, represents the shortest path to this starting point with a negative number.
Then: The complete code is as follows:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>

#define ULL unsigned long long
using namespace std;

typedef struct coordinate
{int x_;int y_;}coor;
queue<coor>que;
int n,m,d;
int start_x,start_y,end_x,end_y;

int main()
{
	scanf("%d %d %d",&n,&m,&d);
	int all[n+1][m+1];
	memset(all,0,sizeof(all));
	getchar();
	for(int time=0;time<n;time++)
	{
		for(int time1=0;time1<m;time1++)
		{
			char ch;
			ch=getchar();
			if(ch=='S'){all[time][time1]=0;start_x=time;start_y=time1;}
			else if(ch=='F'){all[time][time1]=0;end_x=time;end_y=time1;}
			else if(ch=='.'){all[time][time1]=0;}
			else if(ch=='M')
			{
				all[time][time1]=d+1;
				coor co_;
				co_.x_=time;
				co_.y_=time1;
				que.push(co_);
			}
		}
		getchar();
	}
	//input finished;
	while(!que.empty())
	{
		coor co=que.front();
		if((co.x_+1<n)&&all[co.x_+1][co.y_]<all[co.x_][co.y_]-1)
		{
			all[co.x_+1][co.y_]=all[co.x_][co.y_]-1;
			co.x_++;
			que.push(co);
			co.x_--;
		}
		if((co.x_-1>=0)&&all[co.x_-1][co.y_]<all[co.x_][co.y_]-1)
		{
			all[co.x_-1][co.y_]=all[co.x_][co.y_]-1;
			co.x_--;
			que.push(co);
			co.x_++;
		}
		if((co.y_+1<m)&&all[co.x_][co.y_+1]<all[co.x_][co.y_]-1)
		{
			all[co.x_][co.y_+1]=all[co.x_][co.y_]-1;
			co.y_++;
			que.push(co);
			co.y_--;
		}
		if((co.y_-1>=0)&&all[co.x_][co.y_-1]<all[co.x_][co.y_]-1)
		{
			all[co.x_][co.y_-1]=all[co.x_][co.y_]-1;
			co.y_--;
			que.push(co);
			co.y_++;
		}
		que.pop();
	}
	//完成赋值,接下来该走普普通通板子bfs了
	if(all[start_x][start_y]!=0){printf("-1\n");}
	else
	{
		queue<coor>bfs_;
		coor start;
		start.x_=start_x;
		start.y_=start_y;
		bfs_.push(start);
		all[start_x][start_y]=-1;
		while(!bfs_.empty())
		{
			coor next=bfs_.front();
			bfs_.pop();

			if((next.x_+1<n)&& all[next.x_+1][next.y_]==0 )
			{
				all[next.x_+1][next.y_]=all[next.x_][next.y_]-1;
				next.x_++;
				bfs_.push(next);
				next.x_--;
			}
			if((next.x_-1>=0)&& all[next.x_-1][next.y_]==0 )
			{
				all[next.x_-1][next.y_]=all[next.x_][next.y_]-1;
				next.x_--;
				bfs_.push(next);
				next.x_++;
			}
			if((next.y_+1<m)&& all[next.x_][next.y_+1]==0 )
			{
				all[next.x_][next.y_+1]=all[next.x_][next.y_]-1;
				next.y_++;
				bfs_.push(next);
				next.y_--;
			}
			if((next.y_-1>=0)&& all[next.x_][next.y_-1]==0 )
			{
				all[next.x_][next.y_-1]=all[next.x_][next.y_]-1;
				next.y_--;
				bfs_.push(next);
				next.y_++;
			}
		}
		if(all[end_x][end_y]>=0)printf("-1");
		else printf("%d\n",-1-all[end_x][end_y]);
	}


    return 0;
}
Released eight original articles · won praise 0 · Views 115

Guess you like

Origin blog.csdn.net/DevourPower/article/details/103962920