这份代码样例全过,求大佬指点
查看原帖
这份代码样例全过,求大佬指点
1039274
210101zhaosicheng楼主2025/7/3 12:43

hard 全对

random 和 max 全错

#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
int a[1010][1010],n,m;
int cnt; 
struct node
{
	int x,y;
}ji[1000010];
int tot;
void move(int x,int y,int edx,int edy)
{
	if(x>edx)
	{
		for(int i=x;i>edx;i--)
		{
			a[i][y]--;
			a[i-1][y]++; 
			cout<<i<<' '<<y<<' '<<i-1<<' '<<y<<'\n';
		}
	}
	else
	{
		for(int i=x;i<edx;i++)
		{
			a[i][y]--;
			a[i+1][y]++;
			cout<<i<<' '<<y<<' '<<i+1<<' '<<y<<'\n';
		}
	}
	if(y>edy)
	{
//		cout<<"!";
		for(int i=y;i>edy;i--)
		{
			a[edx][i]--;
			a[edx][i-1]++; 
			cout<<edx<<' '<<i<<' '<<edx<<' '<<i-1<<'\n';
		}
	}
	else
	{
		for(int i=y;i<edy;i++)
		{
			a[edx][i]--;
			a[edx][i+1]++;
			cout<<edx<<' '<<i<<' '<<edx<<' '<<i+1<<'\n';
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(i&1)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]%2==1)
				{
					cnt++;
					ji[cnt]={i,j};
				}
			}
		}
		else
		{
			for(int j=m;j>=1;j--)
			{
				if(a[i][j]%2==1)
				{
					cnt++;
					ji[cnt]={i,j};
				}
			}
		}
	}
	for(int i=1;i<=cnt;i+=2)
	{
		if(i+1>cnt)
		{
			break;
		}
		tot+=abs(ji[i].x-ji[i+1].x)+abs(ji[i].y-ji[i+1].y);
	}
	cout<<tot<<'\n';
	for(int i=1;i<=cnt;i+=2)
	{
		if(i+1>cnt)
		{
			break;
		}
		move(ji[i].x,ji[i].y,ji[i+1].x,ji[i+1].y);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<a[i][j]<<' ';
		}
		cout<<endl;
	}
    return 0;
}
2025/7/3 12:43
加载中...