India and China Origins bfs+二分

 

A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died. 
 



Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain. 
And at each step people can go to 4 adjacent positions. 

Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.

题目大意,每年都会有一座山升起来问几年后中印的交流会断掉

我的ma,居然没有意识到年是单调的,既然意识到了,辣么就可以二分了

对于每一个mid年份 先绘制一下图tgap 然后从第一排的每一个位置开始搜索,如果能搜到n-1的位置,那么就可以交流,继续向上二分,否则向下二分。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

int n,m,year;
char gap[600][600];
char tgap[600][600];
int px[251000],py[251000];
bool visit[550][550];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
struct node
{
	int x,y;
	node(){}
	node(int a,int b)
	{
		x=a,y=b;
	}
};

bool bfs(int x,int y)
{
	memset(visit,0,sizeof(visit));
	visit[x][y]=1;
	queue<node> q;
	q.push(node(x,y));
	while(!q.empty())
	{
		node in,ne;
		in=q.front();
		q.pop();
		if(in.x>=n-1)
		{
			return 1;
		}
		for(int i=0;i<4;i++)
		{
			ne.x=in.x+dir[i][0];
			ne.y=in.y+dir[i][1];
			if(visit[ne.x][ne.y]==0&&tgap[ne.x][ne.y]=='0'&&ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m)
			{
				visit[ne.x][ne.y]=1;
				q.push(ne);
			}
		}
	}
	return false;
}

bool judge(int Y)
{
	for(int i=0;i<n;i++) strcpy(tgap[i],gap[i]);
	for(int i=1;i<=Y;i++)
	{
		tgap[px[i]][py[i]]='1';
	}
	for(int i=0;i<m;i++)
	{
		if(tgap[0][i]=='0') 
		{
			bool flag=bfs(0,i);
			if(flag) return true;
		}
	}
	return false;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++) scanf("%s",gap[i]);
		scanf("%d",&year);
		for(int i=1;i<=year;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			px[i]=x,py[i]=y;
		}
		int l=0,r=year,mid=0,ans=0;
		while(r>=l)
		{
			mid=(l+r)>>1;
			if(judge(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else r=mid-1;
		}
		printf("%d\n",ans+1);
	}
return 0;
} 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值