求助,3个TLE,49分

P1123 取数游戏

和你一样......```cpp #include<bits/stdc++.h> #include<windows.h> using namespace std; int n,m,a[11][11] = {0},b[11][11] = {0},T,ans = -1; const int prox[8] = {0,0,1,-1,1,1,-1,-1},proy[8] = {1,-1,0,0,1,-1,1,-1}; bool check(){ // for(int i = 1;i<=n;i++){ // for(int j = 1;j<=m;j++){ // cout<<b[i][j]<<" "; // } // cout<<endl; // } // Sleep(2000); for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(b[i][j]==0)return false; } } return true; } void dfs(int num){ if(check()){ //cout<<"Oops!Here is......"<<num<<endl; ans = max(ans,num); //Sleep(10); return ; } for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(b[i][j] == 0){ //cout<<"checking: x:"<<i<<" y:"<<j<<endl; //cout<<"Now num:"<<num<<endl; //Sleep(100); int k; b[i][j] = 1; for(k = 0;k<8;k++)b[i+prox[k]][j+proy[k]]++; dfs(num+a[i][j]); b[i][j] = 0; for(k = 0;k<8;k++)b[i+prox[k]][j+proy[k]]--; } } } return; } int main(){ cin>>T; while(T--){ ans = -1; cin>>n>>m; for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ scanf("%d",&a[i][j]); } } dfs(0); cout<<ans<<endl; } system ("pause"); } ```
by SunsetVoice @ 2023-03-13 21:19:56


和你一样: ```cpp #include<bits/stdc++.h> #include<windows.h> using namespace std; int n,m,a[11][11] = {0},b[11][11] = {0},T,ans = -1; const int prox[8] = {0,0,1,-1,1,1,-1,-1},proy[8] = {1,-1,0,0,1,-1,1,-1}; bool check(){ // for(int i = 1;i<=n;i++){ // for(int j = 1;j<=m;j++){ // cout<<b[i][j]<<" "; // } // cout<<endl; // } // Sleep(2000); for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(b[i][j]==0)return false; } } return true; } void dfs(int num){ if(check()){ //cout<<"Oops!Here is......"<<num<<endl; ans = max(ans,num); //Sleep(10); return ; } for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(b[i][j] == 0){ //cout<<"checking: x:"<<i<<" y:"<<j<<endl; //cout<<"Now num:"<<num<<endl; //Sleep(100); int k; b[i][j] = 1; for(k = 0;k<8;k++)b[i+prox[k]][j+proy[k]]++; dfs(num+a[i][j]); b[i][j] = 0; for(k = 0;k<8;k++)b[i+prox[k]][j+proy[k]]--; } } } return; } int main(){ cin>>T; while(T--){ ans = -1; cin>>n>>m; for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ scanf("%d",&a[i][j]); } } dfs(0); cout<<ans<<endl; } system ("pause"); } ```
by SunsetVoice @ 2023-03-13 21:20:32


这...
by LethE_YoreN_BuriaLdl @ 2023-04-07 19:46:51


|