HDU-5025 2014广州网络赛 Saving Tang Monk 状压+BFS

给出一个N*N的矩阵,开启牢门需要收集齐m种钥匙,且必须收集了前i-1种钥匙才能收集第i种钥匙,最终收集齐了回到关押唐僧的房间拯救唐僧,经过一个‘S‘的房间时需要额外耗时把蛇打死,蛇最多5条,所以状压一下用优先队列BFS求最小时间即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
#define inf 1<<29
using namespace std;
const int maxn=111;
char str[maxn][maxn];
int n,m;
int ans;
int map[maxn][maxn];
int dp[maxn][maxn][11];//访问到坐标(x,y)身上有i个钥匙的步数
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
struct node
{
	int x,y;
	int step;
	int snake;
	int k;
 	friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
};
int go(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<n&&str[x][y]!='#')
	{
		return 1;
	}
	return 0;
}
void solve(int x,int y)
{
	priority_queue<node> q;
	node front,now;
	now.x=x;
	now.y=y;
	now.step=0;
	now.snake=0;
	now.k=0;
	q.push(now);
	while(!q.empty())
	{
		front=q.top();
		q.pop();
		x=front.x;
		y=front.y;
		if(str[x][y]=='T'&&front.k==m)
		{
			ans=min(ans,front.step);
		}
		for(int i=0;i<4;i++)
		{
			int fx=x+dir[i][0];
			int fy=y+dir[i][1];
			now.x=fx;
			now.y=fy;
			now.step=front.step+1;
			now.snake=front.snake;
			now.k=front.k;
			if(go(fx,fy))
			{
				if(str[fx][fy]=='S')
				{
					int k=map[fx][fy];
					if(((1<<k)&now.snake)==0)
                    {
                        now.step++;
                        now.snake|=(1<<k);
                    }
				}
				if(str[fx][fy]-'0'==now.k+1)
				{
					now.k+=1;
				}
				if(dp[fx][fy][now.k]>now.step&&now.step<ans)
				{
					dp[fx][fy][now.k]=now.step;
					q.push(now);
				}
			}
		}
	}
}
int main()
{
	int cnt;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0&&m==0)
		{
			break;
		}
		cnt=0;
		for(int i=0;i<n;i++)
		{
			scanf("%s",str[i]);
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(str[i][j]=='S')
				{
					map[i][j]=cnt++;
				}
				for(int k=0;k<11;k++)
				{
					dp[i][j][k]=inf;
				}
			}
		}
		ans=inf;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(str[i][j]=='K')
				{
					solve(i,j);
					break;
				}
			}
		}
		if(ans==inf)
		{
			printf("impossible\n");
		}
		else
		{
			printf("%d\n",ans);
		}
	}
	return 0;
}
时间: 2024-12-28 01:29:09

HDU-5025 2014广州网络赛 Saving Tang Monk 状压+BFS的相关文章

HDU 5023 (2014广州网络赛 线段树)

解题思路: 有两种操作,第一种是将区间 a 到 b 染成颜色 c ,第二种是询问区间 a  到 b 内的颜色,按照顺序输出. 注意到颜色只有三十种,因此我们可以用二进制数表示颜色,通过位运算来操作. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <vector> #include

HDU 5025 Saving Tang Monk(状压搜索)

钥匙是必须有序,蛇是不要求有序的.所以一个需要状压一个不用 因为时间计算和步数计算不同.所以要遍历整个空间,或者使用优先队列.优先时间短的. 风格就这样了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #define inf 0x3f3f3f3f #define maxn 110 using names

HDU 5024 Wang Xifeng&#39;s Little Plot(2014广州网络赛1003)

写了1h的DFS,简直被自己的代码吓哭了..不过起码还是思路清晰,QUQ~ 说一下题意吧: 题意是求一条最长路,最多能经过一次转弯,并且其角度只能为90度. 拿第一个样例来说:(0,1)->(1,2)->[转弯](2,1) ,所以答案是3. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 代码如下: #include<iostream> #include<cstdio> #include<cstring>

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

hihocoder 1828 Saving Tang Monk II (DP+BFS)

题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha

hihocoder #1828 : Saving Tang Monk II(BFS)

描述 <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang

[hdu 5032]2014北京网络赛Always Cook Mushroom 离散化+离线线段树/树状数组

Always Cook Mushroom Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 196    Accepted Submission(s): 54 Problem Description Matt has a company, Always Cook Mushroom (ACM), which produces high-q

2014牡丹江网络预选赛D题(状压DP)zoj3812

We Need Medicine Time Limit: 10 Seconds      Memory Limit: 65536 KB      Special Judge A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was named LL virus.

2018icpc南京网络赛-E AC Challenge(状压+dfs)

题意: n道题,每道题有ai和bi,完成这道题需要先完成若干道题,完成这道题可以得到分数t*ai+bi,其中t是时间 1s, n<=20 思路: 由n的范围状压,状态最多1e6 然后dfs,注意代码中dfs里的剪枝, 对一个状态statu,因为贪心的取最大值就行,所以及时剪枝 代码: 当时写不出来真是菜的活该 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath>