首页 > 其他分享 >G.Snake Move

G.Snake Move

时间:2024-01-24 19:44:56浏览次数:43  
标签:q1 int Move 3005 cc Snake && empty

  • 赛后独立思考了不算长的一段时间,然后就通过了;赛时没看这道题有些可惜,算是个教训吧
  • 发现两个性质之后,这道题就非常简单了:
    1.只有初始位置的贪吃蛇才可能会对最优路径产生影响
    2.令贪吃蛇长度-1的操作等价于它停在原地不动
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int a[3005][3005];
int f[3005][3005];
int read1()
{
	char cc=getchar();
	while(!(cc>=48&&cc<=57))
	{
		if(cc=='-')
		{
			break;
		}
		cc=getchar();
	}
	bool f=false;
	int s=0;
	if(cc=='-')
	{
		f=true;
	}
	else
	{
		s=cc-48;
	}
	while(1)
	{
		cc=getchar();
		if(cc>=48&&cc<=57)
		{
			s=s*10+cc-48;
		}
		else
		{
			break;
		}
	}
	if(f==true)
	{
		s=-s;
	}
	return s;
}
struct t1
{
	int x,y,w;
};
bool operator <(t1 a,t1 b)
{
	if(a.w!=b.w)
	{
		return a.w<b.w;
	}
	else if(a.x!=b.x)
	{
		return a.x<b.x;
	}
	return a.y<b.y;
}
priority_queue<t1>q;
deque<pair<int,int> >q1;
deque<int>q2;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
void bfs(int s,int t)
{
	f[s][t]=0;
	q1.push_back(make_pair(s,t));
	q2.push_back(0);
	while(!(q1.empty()&&q.empty()))
	{
		if(q1.empty()||!q.empty()&&-q.top().w<=q2.front())
		{
			t1 tmp=q.top();
			q.pop();
			q1.push_front(make_pair(tmp.x,tmp.y));
			q2.push_front(-tmp.w);
		}
		int x=q1.front().first,y=q1.front().second;
		q1.pop_front();
		int w=q2.front();
		q2.pop_front();
		for(int i=0;i<4;i++)
		{
			if(w>=a[x+dx[i]][y+dy[i]]&&w+1<f[x+dx[i]][y+dy[i]])
			{
				f[x+dx[i]][y+dy[i]]=w+1;
				q1.push_back(make_pair(x+dx[i],y+dy[i]));
				q2.push_back(w+1);
			}
			else if(a[x+dx[i]][y+dy[i]]!=INT_MAX&&w+1<f[x+dx[i]][y+dy[i]])
			{
				t1 tmp;
				tmp.x=x;tmp.y=y;tmp.w=-a[x+dx[i]][y+dy[i]];
				q.push(tmp);
			}
		}
	}
}
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	int s,t;
	for(int i=1;i<=k;i++)
	{
		int x,y;
		x=read1();
		y=read1();
		if(i==1)
		{
			s=x;
			t=y;
		}
		a[x][y]=k-i;
	}
	string S;
	for(int i=1;i<=n;i++)
	{
		cin>>S;
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]==0)
			{
				if(S[j-1]=='.')
				{
					a[i][j]=0;
				}
				else
				{
					a[i][j]=INT_MAX;
				}
			}
			a[0][j]=a[n+1][j]=INT_MAX;
		}
		a[i][0]=a[i][m+1]=INT_MAX;
	}
	memset(f,0x3f,sizeof(f));
	bfs(s,t);
	unsigned long long ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			long long tmp=f[i][j];
			if(f[i][j]<1000000000)
			{
				ans=ans+tmp*tmp;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

标签:q1,int,Move,3005,cc,Snake,&&,empty
From: https://www.cnblogs.com/watersail/p/17985706

相关文章

  • remove 移除数据
    //云端代码constdb=uniCloud.database()exports.main=async(event,context)=>{constcollection=db.collection(event.name)constdocList=awaitcollection.where(event.data).get()if(!docList.data||docList.data.length===0){......
  • Rust 所有权和 Move 语义
    Rust所有权和Move语义所有权和生命周期是Rust和其它编程语言的主要区别,也是Rust其它知识点的基础。动态数组因为大小在编译期无法确定,所以放在堆上,并且在栈上有一个包含了长度和容量的胖指针指向堆上的内存。恰到好处的限制,反而会释放无穷的创意和生产力。Rust所有权......
  • 无涯教程-Redis - SMOVE 命令函数
    RedisSMOVE命令用于将集合中的元素从一个键移动到另一个键。如果源(source)不存在或不包含指定的元素,则不执行任何操作,并返回0。否则,该元素将从源集中删除,并添加到目标集中,如果源或目标没有设置值,则返回错误。SMOVE-返回值返回整数。1,如果元素已移动。0,如果元素不是......
  • appium报错DeprecationWarning: desired_capabilities argument is deprecated and wi
    不再用desired_capabilities,用options代替原来的desired_caps={"platformName":"ios","platformVersion":"11.4","deviceName":"iPhone6Plus",&......
  • 字符函数和字符串函数:memcpy、my_memcpy、memmove、my_memmove——《初学C语言第41天
    ////——————对字符进行函数操作时,函数厚的括号内需使用单引号:isspace('s')////函数     判断    如果他的参数符合下列条件就返回真(非0),不符合返回0////(判断函数是不是后续对应的参数)////注意头文件的引用////iscntrl    任何控制字符////issp......
  • [C++ 从入门到精通] 18.左值、右值,左值引用、右值引用、move
    文章预览:一.左值和右值二.引用分类三.左值引用(1个地址符&)四.右值引用(2个地址符&)五.std::move函数一.左值和右值inti;//赋值语句i=20;//左值:i(int类型的对象,代表一块内存区域),右值:20(代表一个值)左值(左值表达式):能用在赋值语句等号左侧的东西,就称之为左值。它能够代表一个......
  • MoveNet:超快且准确的姿态检测模型
    转载:https://zhuanlan.zhihu.com/p/569457464官方说明:https://www.tensorflow.org/hub/tutorials/movenet?hl=zh-cnncnn:https://github.com/FeiGeChuanShu/ncnn_Android_MoveNetgithub:https://github.com/tensorflow/docs-l10n/blob/master/site/zh-cn/hub/tutorials/movene......
  • Remove TraceParent header from HttpClient requests
    ASP.NETCorecreatesanActivitythatrepresentstherequestbydefault.ThisiswhattellsHttpClienttosendanoutgoingrequestidheader.Youcandisableitinacoupleofways:Bysettingthecurrentactivitytonullbeforemakingtherequest(Activi......
  • C++移动构造与std::move()
    背景及问题如下程序所示:#include<iostream>classMyString{public: MyString()=default; MyString(constchar*data) { printf("%s","MyStringConstructed!!\n"); size=strlen(data); m_data=newchar[size]; memcpy(m_data,......
  • string.replace()与removeprefix() 和 removesuffix()的区别 字符串技巧
    string.replace(),removeprefix()和removesuffix()是Python中的字符串方法,它们都用于修改字符串,但是它们的功能和使用方式有所不同:string.replace(old,new[,count]):这个方法会将字符串中的old子串替换为new子串。如果提供了可选参数count,则只替换前count个old子串¹......