Binbin's treasure map(bfs)

思路:

 

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

char map[505][505];
int book[505][505]={0};
struct note
{
	int x;
	int y;
};
bool cmp(int a,int b)
{
	return a>b;
}
struct note que[500*500+5];
int ans[500*500+5]={0};
int head,tail;
int main()
{
	int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
	int m,n;
	int cnt=0;
	cin >> n >> m;
	getchar();

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin >> map[i][j];
			
		}
		getchar();
		
	}
	for(int i=0;i<=m+1;i++)
	{
		
		map[0][i]='.';
		map[n+1][i]='.';
	}
	for(int j=0;j<=n+1;j++)
	{
		map[j][0]='.';
		map[j][m+1]='.';
	}
	//加上周围一圈,标记可读
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(book[i][j]==0&&(map[i][j]=='.'||map[i][j]=='$'))
			{
				cnt++;
				
				book[i][j]=1;
				int startx=i;
				int starty=j;
				head=1;
				tail=1;
				int tx;
				int ty;
				que[tail].x=startx;
				que[tail].y=starty;
				tail++;
				if(map[startx][starty]=='$')
				{
					ans[cnt]++;
				}
				while(head<tail)
				{
					for(int k=0;k<=3;k++)
					{
						tx=que[head].x+next[k][0];
						ty=que[head].y+next[k][1];
						if(tx<0||tx>n+1||ty<0||ty>m+1)
						{
							continue;
						}
						if((map[tx][ty]=='.'||map[tx][ty]=='$')&&book[tx][ty]==0)
						{
							book[tx][ty]=1;
							if(map[tx][ty]=='$')
							{
							
								ans[cnt]++;
							}
							que[tail].x=tx;
							que[tail].y=ty;
							tail++;
						}
					}
					head++;
				}
			}
		}
	}

	sort(ans+1,ans+cnt+1,cmp);
	int an=ans[1]+ans[2];
	cout << an;

	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值