进队出队所需的时间是多少啊
  • 板块P1644 跳马问题
  • 楼主段落
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/9/21 22:44
  • 上次更新2023/10/27 10:23:34
查看原帖
进队出队所需的时间是多少啊
392327
段落楼主2022/9/21 22:44
#include<bits/stdc++.h>
using namespace std;
int n,m,a[20][20];
queue<int>q;
int main(){
	cin>>n>>m;
	q.push(0);
	q.push(0);
	int x,y;
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		y=q.front();
		q.pop();
		if(x+2<=n&&y+1<=m)
		{
			a[x+2][y+1]++;
			q.push(x+2);
			q.push(y+1);
		}
		if(x+1<=n&&y+2<=m)
		{
			a[x+1][y+2]++;
			q.push(x+1);
			q.push(y+2);
		}
		if(x-2>=0&&y+1<=m)
		{
			a[x-2][y+1]++;
			q.push(x-2);
			q.push(y+1);
		}
		if(x-1>=0&&y+2<=m)
		{
			a[x-1][y+2]++;
			q.push(x-1);
			q.push(y+2);
		}
	}
	cout<<a[n][m];
	return 0;
}

TLE了三个点

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
char a[100000000][2];
const int Mod=100000000;
int main(){
	cin>>n>>m;
	a[0][0]=0+'0';
	a[0][1]=0+'0';
	int i=0,j=0;
	while(i<=j)
	{
		if(a[i%Mod][0]-'0'>n||a[i%Mod][0]-'0'<0||a[i%Mod][1]-'0'>m){
			i++;
			continue;
		}
		if(a[i%Mod][0]-'0'==n&&a[i%Mod][1]-'0'==m)
		{
			ans++;
			i++;
			continue;
		}
		j++;
		a[j%Mod][0]=a[i%Mod][0]-2;
		a[j%Mod][1]=a[i%Mod][1]+1;
		j++;
		a[j%Mod][0]=a[i%Mod][0]+2;
		a[j%Mod][1]=a[i%Mod][1]+1;
		j++;
		a[j%Mod][0]=a[i%Mod][0]-1;
		a[j%Mod][1]=a[i%Mod][1]+2;
		j++;
		a[j%Mod][0]=a[i%Mod][0]+1;
		a[j%Mod][1]=a[i%Mod][1]+2;
		i++;	
	}
	cout<<ans;
	return 0;
}

AC了

所以进队出队比较耗时吗

2022/9/21 22:44
加载中...