题目描述
不妨将超市的地面划分为
个区块,每个区块只有两种可能:货架 or 空地。(数据保证一定存在货架)
我们认为顾客只要经过货架旁边(指货架的上、下、左、右4格,不能直接从货架上跨过去),就会购买货架上的商品,但是同一区块只会购买一次。(如果在出入口不会试图购买周围的货架)
现在顾客们来到了超市的入口处,由于人流量太多,他们不希望重复经过多次同一个区块。
所以,现在你的任务是,为顾客们规划一条购买路线,让顾客们能够到达出口的同时,老板的单客利润得以最大化。
作为答案检验,你只需要输出每个客人消费的最大值是多少就可以了。
输入
第一行2个整数
,代表着超市地面的行列数。
接下来 行,每行
个数字,代表着每个区块当前的情况(A代表入口,B代表出口,0代表空地,其他数字代表着货架上物品的价格)。
输出
一个整数,代表着最大的销售额。
样例输入 Copy
5 5
A 0 0 5 0
0 0 0 0 0
1 0 3 2 0
0 0 0 0 0
1 1 0 0 B
样例输出 Copy
12
提示
。
#include<bits/stdc++.h>
using namespace std;
const int N=100;
char a[N][N];
int n,m;
int maxn=INT_MIN;
bool visited[N][N],visite1[N][N];
int x1=0,y1=0;
int x2=0,y2=0;
int next1[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int tx,int ty,int count1)
{
if(tx==x2&&ty==y2)
{
if(count1>maxn)
{
maxn=count1;
}
count1=0;
return;
}
else
{
for(int i=0;i<4;i++)
{
int x=tx+next1[i][0];
int y=ty+next1[i][1];
if(x>=1&&y>=1&&x<=n&&y<=m&&visited[x][y]==false&&(a[x][y]=='0'||a[x][y]=='B'))
{
visited[x][y]=true;
int d[4];
for(int i=0;i<4;i++)
{
if(isdigit(a[x+next1[i][0]][y+next1[i][1]]))
{
d[i]=a[x+next1[i][0]][y+next1[i][1]]-'0';
}
else
{
d[i]=0;
}
}
int max1=INT_MIN;
int x3,y3;
for(int i=0;i<4;i++)
{
if(max1<d[i]&&visite1[x+next1[i][0]][y+next1[i][1]]==false)
{
max1=d[i];
x3=x+next1[i][0];
y3=y+next1[i][1];
}
}
if(a[x][y]!='A'&&a[x][y]!='B')
{
count1+=max1;
visite1[x3][y3]=true;
}
dfs(x,y,count1);
count1-=max1;
visited[x][y]=false;
visite1[x3][y3]=false;
}
}
}
}
int main ()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')
{
x1=i;
y1=j;
}
else if(a[i][j]=='B')
{
x2=i;
y2=j;
}
}
}
visited[x1][y1]=true;
dfs(x1,y1,0);
cout<<maxn<<endl;
return 0;
}
//by crtzk7