拨钟问题为啥测试不通过呢

zxpisme 2023-09-30 19:34:13 2023-10-01 1:20:54 7
#include<iostream>
#include<string>
using namespace std;

struct matrixData
{
	int data[10][10]={0};
	int tmp[10][10]={0};
	void turnClock(int);//转钟操作
	void Copy2Tmp();//原始输入复制到tmp
	void cinClock();//输入clock
	bool ifZero();//判断是否全部12点
	void showCin();//显示原始时钟
	void showClock();//显示操作后时钟
};

struct matrixOp
{
	int dataOp[10] = {0};//操作记录数组
	void writeOp(int);//写入操作点
	void showOpList();//显示操作记录列表
	void clearOpList(int,int);//清理操作记录列表
	int count();//返回操作数组操作的数量
	string getCountString();//返回操作数据的striing
};


int main()
{
	matrixData a;
	matrixOp b;
	int Min = 999;
	string MinString = "";
	a.cinClock();
	while(b.dataOp[0]!=1)
	{
        //这里是在做枚举,类似于四进制,从第3位每次+1,满4向前进一位
        //dataOp[0] 等于1的时候相当于枚举完了
		b.dataOp[3] = b.dataOp[3] +1;
		for(int i = 3;i>=1;i--)
		{
			if(b.dataOp[i]==4)
			{
				b.dataOp[i] = 0;
				b.dataOp[i-1] = b.dataOp[i-1]+1;
			}
		}
		//枚举123号钟
		for(int i = 0;i<b.dataOp[1];i++)
		{
			a.turnClock(1);
		}
		for(int i = 0;i<b.dataOp[2];i++)
		{
			a.turnClock(2);
		}
		for(int i = 0;i<b.dataOp[3];i++)
		{
			a.turnClock(3);
		}
        //b.showOpList();
		//调零 123
		while(a.tmp[1][1]!=0)
		{
			a.turnClock(4);
			b.dataOp[4] = (b.dataOp[4]+1)%4;
		}
		while(a.tmp[1][2]!=0)
		{
			a.turnClock(5);
			b.dataOp[5] = (b.dataOp[5]+1)%4;
		}
		while(a.tmp[1][3]!=0)
		{
			a.turnClock(6);
			b.dataOp[6] = (b.dataOp[6]+1)%4;
		}

		//调零 46
		while(a.tmp[2][1]!=0)
		{
			a.turnClock(7);
			b.dataOp[7] = (b.dataOp[7]+1)%4;
		}
		while(a.tmp[2][3]!=0)
		{
			a.turnClock(9);
			b.dataOp[9] = (b.dataOp[9]+1)%4;
		}
		//调零 8
		while(a.tmp[3][2]!=0)
		{
			a.turnClock(8);
			b.dataOp[8] = (b.dataOp[8]+1)%4;
		}
		//如果5 7 9 为 0 则全部为0
		if(a.tmp[2][2] ==0 && a.tmp[3][1]==0&&a.tmp[3][3]==0)
		{
			//a.showClock();
			//b.showOpList();
			//cout<<"-----"<<endl;
			//cout<<"look here!"<<endl;
			//system("pause");
			if(Min>b.count())
			{
				Min = b.count();
				MinString = b.getCountString();
			}
		}
		a.Copy2Tmp();
		b.clearOpList(4,9);	
	}
	cout<<MinString;

	
	return 0;
}
void matrixData::turnClock(int n)
{
	switch (n) 
	{
	case 1:
		tmp[1][1] = (tmp[1][1] + 1)%4;
		tmp[1][2] = (tmp[1][2] + 1)%4;
		tmp[2][1] = (tmp[2][1] + 1)%4;
		tmp[2][2] = (tmp[2][2] + 1)%4;
		break;
	case 2:
		tmp[1][1] = (tmp[1][1] + 1)%4;
		tmp[1][2] = (tmp[1][2] + 1)%4;
		tmp[1][3] = (tmp[1][3] + 1)%4;
		break;
	case 3:
		tmp[1][2] = (tmp[1][2] + 1)%4;
		tmp[1][3] = (tmp[1][3] + 1)%4;
		tmp[2][2] = (tmp[2][2] + 1)%4;
		tmp[2][3] = (tmp[2][3] + 1)%4;
		break;
	case 4:
		tmp[1][1] = (tmp[1][1] + 1)%4;
		tmp[2][1] = (tmp[2][1] + 1)%4;
		tmp[3][1] = (tmp[3][1] + 1)%4;
		break;
	case 5:
		tmp[2][1] = (tmp[2][1] + 1)%4;
		tmp[2][2] = (tmp[2][2] + 1)%4;
		tmp[2][3] = (tmp[2][3] + 1)%4;
		tmp[1][2] = (tmp[1][2] + 1)%4;
		tmp[3][2] = (tmp[3][2] + 1)%4;
		break;
	case 6:
		tmp[1][3] = (tmp[1][3] + 1)%4;
		tmp[2][3] = (tmp[2][3] + 1)%4;
		tmp[3][3] = (tmp[3][3] + 1)%4;
		break;
	case 7:
		tmp[2][1] = (tmp[2][1] + 1)%4;
		tmp[2][2] = (tmp[2][2] + 1)%4;
		tmp[3][1] = (tmp[3][1] + 1)%4;
		tmp[3][2] = (tmp[3][2] + 1)%4;
		break;
	case 8:
		tmp[3][1] = (tmp[3][1] + 1)%4;
		tmp[3][2] = (tmp[3][2] + 1)%4;
		tmp[3][3] = (tmp[3][3] + 1)%4;
		break;
	case 9:
		tmp[2][2] = (tmp[2][2] + 1)%4;
		tmp[2][3] = (tmp[2][3] + 1)%4;
		tmp[3][2] = (tmp[3][2] + 1)%4;
		tmp[3][3] = (tmp[3][3] + 1)%4;
		break;
	default:
		break;
	}
}
void matrixData::Copy2Tmp()
{
	for(int i = 1;i<=3;i++)
	{
		for(int j = 1;j<=3;j++)
		{
			tmp[i][j] = data[i][j];
		}
	}
}
void matrixData::cinClock()
{
	for(int i = 1;i<=3;i++)
	{
		for(int j = 1;j<=3;j++)
		{
			cin>>data[i][j];
			tmp[i][j] = data[i][j];
		}
	}
}
bool matrixData::ifZero()
{
	for(int i = 1;i<=3;i++)
	{
		for(int j = 1;j<=3;j++)
		{
			if(tmp[i][j]!=0)
			{
				return false;
			}
		}
	}
	return true;	
}
void matrixData::showCin()
{
	for(int i = 1;i<=3;i++)
	{
		for(int j = 1;j<=3;j++)
		{
			cout<<data[i][j]<<" ";
		}
		cout<<endl;
	}	
}
void matrixData::showClock()
{
	for(int i = 1;i<=3;i++)
	{
		for(int j = 1;j<=3;j++)
		{
			cout<<tmp[i][j]<<" ";
		}
		cout<<endl;
	}	
}
void matrixOp::writeOp(int n)
{
	dataOp[n] = (dataOp[n] + 1)%4;
}
void matrixOp::showOpList()
{
	for(int i = 1;i<=9;i++)
	{
		cout<<dataOp[i]<<" ";
	}
	cout<<endl;
}
void matrixOp::clearOpList(int s,int e)
{
	for(int i = s;i<=e;i++)
	{
		dataOp[i] = 0;
	}
}
int matrixOp::count()
{
	int cnt = 0;
	for(int i = 1;i<=9;i++)
	{
		if(dataOp[i]>0)
		{
			cnt = cnt + dataOp[i];
		}
	}
	return cnt;		
}
string matrixOp::getCountString()
{
	string s = "";
	for(int i = 1;i<=9;i++)
	{
		if(dataOp[i]>0)
		{
			s = s + to_string(i) + " ";
		}
	}
	return s;	
}
{{ vote && vote.total.up }}