hdu-3468 Treasure Hunting(BFS+二分匹配)

Treasure Hunting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2198    Accepted Submission(s): 598


Problem Description
Do you like treasure hunting? Today, with one of his friend, iSea is on a venture trip again. As most movie said, they find so many gold hiding in their trip.
Now iSea’s clever friend has already got the map of the place they are going to hunt, simplify the map, there are three ground types:

● '.' means blank ground, they can get through it
● '#' means block, they can’t get through it
● '*' means gold hiding under ground, also they can just get through it (but you won’t, right?)

What makes iSea very delighted is the friend with him is extraordinary justice, he would not take away things which doesn’t belong to him, so all the treasure belong to iSea oneself! 
But his friend has a request, he will set up a number of rally points on the map, namely 'A', 'B' ... 'Z', 'a', 'b' ... 'z' (in that order, but may be less than 52), they start in 'A', each time friend reaches to the next rally point in the shortest way, they have to meet here (i.e. iSea reaches there earlier than or same as his friend), then start together, but you can choose different paths. Initially, iSea’s speed is the same with his friend, but to grab treasures, he save one time unit among each part of road, he use the only one unit to get a treasure, after being picked, the treasure’s point change into blank ground.
Under the premise of his friend’s rule, how much treasure iSea can get at most?

 

Input
There are several test cases in the input.

Each test case begin with two integers R, C (2 ≤ R, C ≤ 100), indicating the row number and the column number.
Then R strings follow, each string has C characters (must be ‘A’ – ‘Z’ or ‘a’ – ‘z’ or ‘.’ or ‘#’ or ‘*’), indicating the type in the coordinate.

The input terminates by end of file marker.
 

Output
For each test case, output one integer, indicating maximum gold number iSea can get, if they can’t meet at one or more rally points, just output -1.

 

Sample Input
2 4 A.B. ***C 2 4 A#B. ***C
 

Sample Output
1 2
 

一、原题地址

        点我传送


二、大致题意

    有一个n*m的矩阵,

    字母 表示目标点,

    *  表示一枚金币, 

    .  表示空地可前进

    # 表示障碍步可前进

    现在主人公从A点出发,前往下一个顺序的目标点,(目标点的排序为A……B……C…………a……z,必须依次经过)。他会以最快的方式前往下一个目标点,因为他与同伴有过约定,每一次在前往下一个目标点的最短路中,他可以带走路途中的其中一枚金币。现在询问在到达最后一个目标点时,他最多可以拿到多少金币。


三、大致思路

    在读入地图的过程中我们可以得到目标点的个数,这样就能判断地图上是不是缺少了某些目标点。

    从每个目标点开始跑一次BFS,可以判断是否能达到下一个目标点,若不能达到则输出-1,同时也得到了当前目标点到达下一个目标点的1条或多条最短路。利用DFS往前搜索,从下一个目标点往当前目标点沿着最短路搜回来,若这些路上存在金币,则将当前目标点与这个金币在二分关系中相连,这样表示可以在这条路上取走这枚金币。这样遍历全部的目标点后,就建立了一个完整的二分图。


四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int 
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }


int n, m;
char mmp[105][105];
int vis[105][105];
int neednum;

const int maxn = 110;
vector<int> vec[maxn];
int match[maxn*maxn];
int visit[maxn*maxn];

int x[] = { 1,-1,0,0 };
int y[] = { 0,0,1,-1 };
struct Node
{
	int x, y;
}need[55];
int uN;
int dis;
int find(int u)
{
	for (int i = 0; i < vec[u].size(); i++)
	{
		int v = vec[u][i];
		if (!visit[v])
		{
			visit[v] = 1;
			if (match[v] == -1 || find(match[v]))
			{
				match[v] = u;
				return true;
			}
		}
	}
	return false;
}
int Match()
{
	int ans = 0;
	memset(match, -1, sizeof(match));
	for (int i = 0; i < uN - 1; i++)
	{
		memset(visit, 0, sizeof(visit));
		if (find(i))
			ans++;
	}
	return ans;
}
void Pre_DFS(int nx, int ny, int id)
{
	if (vis[nx][ny] == 1)
	{
		return;
	}
	if (mmp[nx][ny] == '*')
	{
		vec[id].push_back(nx * m + ny);
		//printf("add    from: %d    to: %d \n", id , nx * m + ny);
		/*DC.AddEdge(nx * 1000 + ny, 100200, 1);
		printf("add    from: %d    to: %d \n", nx * 1000 + ny, 100200);*/
	}
	for (int i = 0; i < 4; i++)
	{
		int xx = nx + x[i], yy = ny + y[i];
		if (xx >= 0 && xx < n&&yy >= 0 && yy < m)
		{
			if (vis[xx][yy] == vis[nx][ny] - 1)
			{
				Pre_DFS(xx, yy, id);
			}
		}
	}
	vis[nx][ny] = 0;
}
bool Pre_BFS(int id)
{
	memset(vis, 0, sizeof(vis));
	int ex = need[id + 1].x, ey = need[id + 1].y;
	queue<Node>q;
	Node start; start.x = need[id].x, start.y = need[id].y;
	vis[start.x][start.y] = 1;
	q.push(start);
	while (!q.empty())
	{
		Node t = q.front();
		q.pop();
		if (t.x == ex&&t.y == ey)
		{
			Pre_DFS(t.x, t.y, id);
			return true;
		}
		for (int i = 0; i < 4; i++)
		{
			int xx = t.x + x[i], yy = t.y + y[i];
			if (xx >= 0 && xx < n&&yy >= 0 && yy < m&&mmp[xx][yy] != '#')
			{
				if (vis[xx][yy] == 0)
				{
					vis[xx][yy] = vis[t.x][t.y] + 1;
					Node next;
					next.x = xx, next.y = yy;
					q.push(next);
				}
			}
		}
	}
	return false;
}
void read()
{
	memset(need, -1, sizeof(need));
	neednum = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%s", mmp[i]);
		for (int j = 0; j < m; j++)
		{
			if (mmp[i][j] >= 'A'&&mmp[i][j] <= 'Z')
			{
				int id = mmp[i][j] - 'A';
				need[id].x = i, need[id].y = j;
				neednum++;
			}
			else if (mmp[i][j] >= 'a'&&mmp[i][j] <= 'z')
			{
				int id = mmp[i][j] - 'a' + 26;
				need[id].x = i, need[id].y = j;
				neednum++;
			}
		}
	}
}
void solve()
{
	read();
	bool flag = false;
	for (int i = 0; i < neednum; i++)
	{
		if (need[i].x == -1)
		{
			flag = true;
			break;
		}
	}
	if (flag || neednum<2)
	{
		printf("-1\n");
		return;
	}
	uN = neednum;
	for (int i = 0; i < uN - 1; i++)
	{
		vec[i].clear();
		//printf("add    from: %d    to: %d \n", 0, 100000 + i + 1);
		if (!Pre_BFS(i))
		{
			printf("-1\n");
			return;
		}
	}
	printf("%d\n", Match());
	return;
}
int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		
		solve();
	}
	getchar();
	getchar();
}

五、反思

    

 对于DFS的回溯没有清空vis,导致超时,这是严重的错误,对于重合的最短路上的搜索若不清空vis,复杂度爆表要牢记。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页