FZU 2092 记忆化搜索 || BFS

68 篇文章 0 订阅

BFS:

#include "queue"
#include "iostream"
#include "algorithm"
using namespace std;

int dir[5][2]={1,0,-1,0,0,1,0,-1,0,0};

int value[12][12][211];

int hash[11][11][11][11][210];

struct node
{
	int time;
	__int64 value;
	friend bool operator<(node n1,node n2)
	{
		if (n1.time==n2.time)
		return n2.value>n1.value;
		else 
			return n2.time<n1.time;
	}
	int x1,x2,y1,y2;
};

int maxx,n,m;
__int64 ans;
char map[11][11];

void bfs()
{
	priority_queue<node>q;
	node cur,next;
	int i,j,l;

	cur.x1=cur.y1=cur.x2=cur.y2=0;
	cur.value=cur.time=0;
	hash[0][0][0][0][0]=1;
	q.push(cur);
	while (!q.empty())
	{
		cur=q.top();
		q.pop();	
		for (i=0;i<5;i++)
			for (j=0;j<5;j++)
			{
				next.x1=cur.x1+dir[i][0];
				next.y1=cur.y1+dir[i][1];

				next.x2=cur.x2+dir[j][0];
				next.y2=cur.y2+dir[j][1];

				next.time=cur.time+1;

			
				if (next.time>maxx) continue;

				if (next.x1<0 || next.x1>=n || next.y1<0 || next.y1>=m) continue;
				if (next.x2<0 || next.x2>=n || next.y2<0 || next.y2>=m) continue;
				if (hash[next.x1] [next.y1] [next.x2] [next.y2] [next.time]==1) continue;
				if (map[next.x1][next.y1]=='#' || map[next.x2][next.y2]=='#') continue;


				next.value=cur.value;
			

				if (next.x1==next.x2 && next.y1==next.y2) next.value+=value[next.x1][next.y1][next.time];
				else next.value+=value[next.x1][next.y1][next.time]+value[next.x2][next.y2][next.time];

		
				if (next.value>ans) ans=next.value;
				q.push(next);
				hash[next.x1] [next.y1] [next.x2] [next.y2] [next.time]=1;
				hash[next.x2] [next.y2] [next.x1] [next.y1] [next.time]=1;
			}
	}
	return ;
}

int main()
{
	int i,Case,k,t,x,y;
	__int64 v;
	scanf("%d",&Case);
	while (Case--)
	{
		scanf("%d%d",&n,&m);
		getchar();
		for (i=0;i<n;i++)
			gets(map[i]);

		scanf("%d",&k);
		memset(value,0,sizeof(value));
		maxx=0;
		for (i=1;i<=k;i++)
		{
			scanf("%d%d%d%I64d",&t,&x,&y,&v);
			if (t>maxx) maxx=t;
			value[x-1][y-1][t]+=v;
		}

		ans=0;
		memset(hash,0,sizeof(hash));
		bfs();
		printf("%I64d\n",ans);
	}
	return 0;
}


记忆化搜索:

#include "stdio.h"
#include "string.h"
#include "math.h"

int dir[5][2]={1,0,-1,0,0,1,0,-1,0,0};
int n,m;
int dp[11][11][11][11][210];
int value[12][12][210];

char map[12][12];

int dfs(int x1,int y1,int x2,int y2,int t)
{
	int x3,y3,x4,y4;
	int i,j,l,temp,max;

	if (t>200) return 0;
    if (x1<0 || x1>=n || y1<0 || y1>=m) return 0;
	if (x2<0 || x2>=n || y2<0 || y2>=m) return 0;
	if (map[x1][y1]=='#' || map[x2][y2]=='#') return 0;

	if (dp[x1][y1][x2][y2][t]!=-1) return dp[x1][y1][x2][y2][t];

	temp=max=0;

	for (i=0;i<5;i++)
		for (j=0;j<5;j++)
		{
			x3=x1+dir[i][0]; y3=y1+dir[i][1];
			x4=x2+dir[j][0]; y4=y2+dir[j][1];
		
			temp=dfs(x3,y3,x4,y4,t+1);
			if (temp>max) max=temp;
		}

	dp[x1][y1][x2][y2][t]=max;

	if (x1==x2 && y1==y2) dp[x1][y1][x2][y2][t]+=value[x1][y1][t];
	else 
		dp[x1][y1][x2][y2][t]+=value[x1][y1][t]+value[x2][y2][t];
	return dp[x1][y1][x2][y2][t];
}


int main()
{
	int i,Case,k,x,y,v,t;
	scanf("%d",&Case);
	while (Case--)
	{
		scanf("%d%d",&n,&m);
		getchar();
		for (i=0;i<n;i++)
			gets(map[i]);

		scanf("%d",&k);

		memset(value,0,sizeof(value));

		while (k--)
		{
			scanf("%d%d%d%d",&t,&x,&y,&v);
			value[x-1][y-1][t]+=v;
		}

		memset(dp,-1,sizeof(dp));

		printf("%d\n",dfs(0,0,0,0,0));
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值