TLE求助!!!
查看原帖
TLE求助!!!
965313
youth191954楼主2024/6/1 15:21
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int a[10][10],h[10][10],l[10][10],g[10][10],num,tot,cx[100];
string s;
struct node{
    int x,y;
    bool operator<(node b)const{return y>b.y;}
}aa[81];
bool dfs(int i) {
    if (i==tot){
        for(int i=0;i<9;i++)for(int j=0;j<9;j++)cout<<a[i][j];
        return true;
    }
    int x=aa[i].x,y=aa[i].y;
    for(int k=1;k<=9;k++){
        int xy=(x/3)*3+(y/3);
        if (!h[x][k]&&!l[y][k]&&!g[xy][k]){
            a[x][y]=k;
            h[x][k]=l[y][k]=g[xy][k]=1;
            if(dfs(i+1)) return true;
            a[x][y]=0;
            h[x][k]=l[y][k]=g[xy][k]=0;
        }
    }
    return false;
}

int main() {
    while(1){
        memset(h,0,sizeof(h));
        memset(l,0,sizeof(l));
        memset(g,0,sizeof(g));
        tot=0;
        cin>>s;
        for(int i=0;i<81;i++){
        	if(s[i]>='0'&&s[i]<='9')cx[i]=s[i]-'0';	
        	else if(s[i]=='.')cx[i]=0;
        	else return 0;
		}
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)a[i][j]=cx[i];
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				if(a[i][j]>0){
            		int val=a[i][j];
                	int xy=(i/3)*3+(j/3);
                	h[i][val]=l[j][val]=g[xy][val]=1;
                } 
				else aa[tot++]={i,j};			
			}
		}
        sort(aa,aa+tot);
        dfs(0);
        cout<<endl;
    }
    return 0;
} 

不知道错在哪

2024/6/1 15:21
加载中...