2 条题解

  • 0
    @ 2023-3-6 13:02:31

    暴力算法75pts

    #include<bits/stdc++.h>
    using namespace std;
    bool area[10][10],line[10][10],lie1[10][10];//判断是否重复 
    char a1[10][10];
    int a[10][10],f[10][10],sum2[100001],sum1;
    int s[10][10]={{0,0,0,0,0,0,0,0,0,0},{0,6,6,6,6,6,6,6,6,6},{0,6,7,7,7,7,7,7,7,6},{0,6,7,8,8,8,8,8,7,6},{0,6,7,8,9,9,9,8,7,6},{0,6,7,8,9,10,9,8,7,6,},{0,6,7,8,9,9,9,8,7,6},{0,6,7,8,8,8,8,8,7,6},{0,6,7,7,7,7,7,7,7,6},{0,6,6,6,6,6,6,6,6,6}};
    void init()
    {
    	int l=0,lie=0;
    	for(int i=1;i<=9;i++)
    	{
    		l++;
    		lie=0;
    		for(int j=1;j<=9;j++)
    		{
    			cin>>a1[i][j];
    			lie++;
    			a[i][j]=a1[i][j]-'0';
    			if(a[i][j]!=0)
    			{
    				f[i][j]=a[i][j];
    				line[l][a[i][j]]=true;
    				lie1[lie][a[i][j]]=true;
    				if(l<=3)
    				{
    					if(lie<=3) area[1][a[i][j]]=true;
    					if(lie>3&&lie<=6) area[2][a[i][j]]=true;
    					if(lie>6&&lie<=9) area[3][a[i][j]]=true;
    				} 
    				if(l>3&&l<=6)
    				{
    					if(lie<=3) area[4][a[i][j]]=true;
    					if(lie>3&&lie<=6) area[5][a[i][j]]=true;
    					if(lie>6&&lie<=9) area[6][a[i][j]]=true;
    				} 
    				if(l>6&&l<=9)
    				{
    					if(lie<=3) area[7][a[i][j]]=true;
    					if(lie>3&&lie<=6) area[8][a[i][j]]=true;
    					if(lie>6&&lie<=9) area[9][a[i][j]]=true;
    				} 
    			}
    		}
    	}
    }
    void printf()
    {
    	sum1++;
    	for(int i=1;i<=9;i++)
    	{
    		for(int j=1;j<=9;j++)
    		{
    			sum2[sum1]+=f[i][j]*s[i][j];
    		}
    	}
    }
    void search(int x,int y)
    {
    	int area1;
    	if(a[x][y]!=0)
    	{
    		if(x==9&&y==9) printf();
    		if(y!=9)
    			search(x,y+1);
    		if(y==9)
    			search(x+1,1);
    	}
    	if(a[x][y]==0)
    	{
    		if(x<=3)
    		{
    			if(y<=3) area1=1;
    			if(y>3&&y<=6) area1=2;
    			if(y>6&&y<=9) area1=3;
    		} 
    		if(x>3&&x<=6)
    		{
    			if(y<=3) area1=4;
    			if(y>3&&y<=6) area1=5;
    			if(y>6&&y<=9) area1=6;
    		} 
    		if(x>6&&x<=9)
    		{
    			if(y<=3) area1=7;
    			if(y>3&&y<=6) area1=8;
    			if(y>6&&y<=9) area1=9;
    		} 
    		for(int i=1;i<=9;i++)
    		{
    			if(!line[x][i]&&!lie1[y][i]&&!area[area1][i])
    			{
    				f[x][y]=i;
    				line[x][i]=true;
    				lie1[y][i]=true;
    				area[area1][i]=true;
    				if(x==9&&y==9)                  
                        printf();
                    if(y==9) search(x+1,1);
    				else search(x,y+1);
    				f[x][y]=0;
    				line[x][i]=false;
    				lie1[y][i]=false;
    				area[area1][i]=false;
    			}
    		}
    	}
    }
    int main()
    {
    	init();
    	search(1,1);
    	int max1=0;
    	for(int i=1;i<=sum1;i++)
    	{
    		max1=max(sum2[i],max1);
    	}
    	cout<<max1; 
    	return 0;
     }
    

    信息

    ID
    178
    时间
    2000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    27
    已通过
    6
    上传者