I. Invasion of Sjkmost(01bfs)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了I. Invasion of Sjkmost(01bfs)相关的知识,希望对你有一定的参考价值。

I. Invasion of Sjkmost(01bfs)

考虑转化为最短路问题。

然后从第一行开始bfs,同时用一个二维数组维护答案。

双端队列存结点,若当前为1加入队首,否则加入队尾。

最后扫一遍对答案取min 即可。

// Problem: Invasion of Sjkmost
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/26808/I
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Date: 2022-01-10 13:25:09
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = 402653189,805306457,1610612741,998244353;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ios ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n)
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 

template <typename T>		//x=max(x,y)  x=min(x,y)
void cmx(T &x,T y)
	if(x<y) x=y;

template <typename T>
void cmn(T &x,T y)
	if(x>y) x=y;

char a[N][N];
int n,m;
int d[4][2]=0,1,0,-1,1,0,-1,0;
int dis[N][N];
deque<PII>q;
int main()
	scanf("%d%d",&n,&m);
	rep(i,1,n) scanf("%s",a[i]+1);
	mst(dis,0x3f);
	rep(i,1,m)
		if(a[1][i]=='1') q.push_front(1,i),dis[1][i]=0;
		else q.push_back(1,i),dis[1][i]=1;
	
	while(!q.empty())
		auto u = q.front();q.pop_front();
		int x=u.x,y=u.y;
		for(int i=0;i<4;i++)
			int nx = x+d[i][0],ny=y+d[i][1];
			if(nx>0&&nx<=n&&ny>0&&ny<=m && 
			dis[nx][ny] > dis[x][y] +( a[nx][ny]=='0'))
				if(a[nx][ny]=='0') q.push_back(nx,ny);
				else q.push_front(nx,ny);
				dis[nx][ny] = dis[x][y] + (a[nx][ny]=='0');
			
		
	
	int ans = n;
	rep(i,1,m) cmn(ans,dis[n][i]);
	printf("%d\\n",ans);
	return 0;


以上是关于I. Invasion of Sjkmost(01bfs)的主要内容,如果未能解决你的问题,请参考以下文章

I. Rise of Shadows

The way of Webpack learning (I.) -- Configure Webpack from zero(从零开始配置webpack)

UVA 6480 Zombie Invasion(模拟退火)

alien_invasion

Multiclonal Invasion in Breast Tumors Identified by Topographic Single Cell Sequencing

[cf1184E]Daleks' Invasion