【问题标题】:High score in grid walk格子行走高分
【发布时间】:2012-07-09 18:50:54
【问题描述】:

有一个有趣的游戏叫单人游戏。它在m*n 网格上播放。每个网格单元中都有一个非负整数。您从分数 0 开始。您不能输入包含整数 0 的单元格。您可以在任何您想要的单元格开始和结束游戏(当然单元格中的数字不能为0)。在每一步,您都可以向上、向下、向左和向右移动到相邻的网格单元。你最后能得到的分数是你路径上数字的总和。但是每个单元格最多只能输入一次。

游戏的目的是让你的分数尽可能高。

输入:
第一行输入是一个整数T 测试用例的数量。每个测试用例的第一行是包含 2 个整数 m 和 n 的单行,它们是网格中的行数和列数。接下来的每一行 m 都包含 n 空格分隔的整数 D 指示相应单元格中的数字

输出:
对于每个测试用例,在一行中输出一个整数,这是您最后可以获得的最高分。

约束:
T 小于 7。
D 小于 60001。
m 和 n 小于 8。

示例输入:

4
1 1
5911
1 2
10832 0
1 1
0
4 1
0
8955
0
11493

样本输出:

5911
10832
0
11493

我试过了,但是对于 7x7 网格,我的方法工作得非常慢。我正在尝试递归访问网格的每条可能路径并比较每条路径的总和。下面是我的代码

#include<iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

int max(int a,int b,int c, int d)
{
int max = a;
if(b>max)
    max = b;
if(c>max)
    max = c;
if(d>max)
    max = d;
return max;
}

int Visit_Component( int (*A)[8], int Visit[8][8], int m,int n , int row, int col)
{

if ( ( row >= m ) || (col >= n )  || (col < 0) || (row < 0)  || A[row][col] == 0         || Visit[row][col] == 1 )
{
    return 0;
}
else
{

    Visit[row][col] = 1;
    int a= 0,b=0,c=0,d=0,result =0;
    a = Visit_Component( A, Visit,m,n, row+1, col);
    b = Visit_Component( A, Visit,m,n, row, col +1);
    c = Visit_Component( A, Visit,m,n, row, col -1);
    d = Visit_Component( A, Visit,m,n, row-1, col );
    Visit[row][col] = 0;
    result  = A[row][col] + max(a,b,c,d);
    return result;
}
}

int main(){

int T;
scanf("%d",&T);
for(int k =0; k<T;k++)
{
    int N ;
    int M;
    int count = 0;
    int maxcount = 0;
    scanf("%d %d",&M,&N);
    int C[8][8];
    int visit[8][8];
    for(int i = 0; i < M; i++)
        for(int j = 0; j < N; j++)
        {
            scanf("%d",&C[i][j]);
            visit[i][j] = 0;
        }
    for( int i= 0 ; i< M ; i++ )
    {
        for( int j =0; j< N ; j++ )
        {

            count  = Visit_Component( C, visit,M,N, i, j);
            if(count > maxcount)
            {
                maxcount  = count;
            }
        }
    }
    printf("%d\n",maxcount);
}
return 0;
}

请建议我如何优化这种方法或更好的算法。

【问题讨论】:

  • 由于代码有效,是否应该移至代码审查?
  • @tomdemuyt 代码正在运行,但我正在寻找此代码的优化,因为它对于大型数据集的运行速度非常慢
  • 这不是真正的代码审查,因为问题不在于如何更好地组织代码,而在于高效的算法。
  • 我不太确定我是否理解这个问题:你可以在游戏后期访问值为 0 的元素,而不是从那里开始吗?还是它们像墙?
  • 问题的呈现方式看起来和google code jam问题一模一样,但我在前面的问题中找不到。

标签: algorithm


【解决方案1】:

正如Wikipedia article on Travelling salesman problem 建议的那样,有精确的算法可以快速解决这个任务。但很难找到。而且它们很可能很复杂。

至于优化OP的方法,有几种可能。

从简单的微优化开始更容易:条件Visit[row][col] == 1满足最高概率,所以它应该排在第一位。

另外,使用动态规划优化分支定界算法以避免一些重复计算是合理的。对于多达 19 个已访问单元格的情况,记忆计算结果为简单的哈希表,性能提高了 25% 以上(对于一些改进的哈希表,可能期望更多)。这是修改后的代码sn-p:

#include<iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

int max(int a,int b,int c, int d)
{
  int max = a;
  if(b>max)
    max = b;
  if(c>max)
    max = c;
  if(d>max)
    max = d;
  return max;
}

typedef unsigned long long ull;
static const int HS = 10000019;
static const int HL = 20;
struct HT {
  ull v;
  int r;
  int c;
};
HT ht[HS] = {0};

int Visit_Component(
  int (*A)[8], ull& Visit, int m,int n , int row, int col, int x)
{

  if ( (Visit & (1ull << (8*row+col))) || ( row >= m ) || (col >= n )  ||
    (col < 0) || (row < 0)  || A[row][col] == 0)
  {
    return 0;
  }
  else
  {
    if (x < HL)
    {
      HT& h = ht[(Visit+4*row+col)%HS];
      if (h.v == Visit && h.r == row && h.c == col)
        return 0;
    }

    Visit |= (1ull << (8*row+col));
    int a= 0,b=0,c=0,d=0,result =0;
    a = Visit_Component( A, Visit,m,n, row+1, col, x+1);
    b = Visit_Component( A, Visit,m,n, row, col +1, x+1);
    c = Visit_Component( A, Visit,m,n, row, col -1, x+1);
    d = Visit_Component( A, Visit,m,n, row-1, col , x+1);
    Visit &= ~(1ull << (8*row+col));
    result  = A[row][col] + max(a,b,c,d);

    if (x < HL)
    {
      HT& h = ht[(Visit+4*row+col)%HS];
      h.v = Visit;
      h.r = row;
      h.c = col;
    }

    return result;
  }
}

int main(){

  int T;
  scanf("%d",&T);
  for(int k =0; k<T;k++)
  {
    int N ;
    int M;
    int count = 0;
    int maxcount = 0;
    scanf("%d %d",&M,&N);
    int C[8][8];
    ull visit = 0;
    for(int i = 0; i < M; i++)
        for(int j = 0; j < N; j++)
        {
            scanf("%d",&C[i][j]);
        }
    for( int i= 0 ; i< M ; i++ )
    {
        for( int j =0; j< N ; j++ )
        {

            count  = Visit_Component( C, visit,M,N, i, j, 0);
            if(count > maxcount)
            {
                maxcount  = count;
            }
        }
    }
    printf("%d\n",maxcount);
  }
  return 0;
}

通过预处理输入矩阵可以进行更多改进。如果矩阵中没有零或角落只有一个零,则可以将所有值相加。

如果只有一个零值(不在角落),则最多应从总和中排除一个非零值。如果您发明了一种算法来确定必须从中删除一个单元格的单元格子集,那么您只需从该子集中选择最小值。

如果有两个或多个零值,则使用分支定界算法:在这种情况下它大约快 20 倍,因为输入矩阵中的每个零值意味着大约五倍的速度增加。

【讨论】:

    【解决方案2】:

    我能想到的一个优化是应用Dijkstra's algorithm。该算法将为您提供特定源节点到所有目标节点的最小(在您的情况下为最大值)路径。

    在此示例中,第一步是构建图表。

    因为您不知道要从哪个源节点开始,所以您必须对网格中的每个节点应用 Dijkstra 算法。时间复杂度会比你的递归方法好,因为对于一个特定的源节点,当找到最大路径时,Dijkstra 的算法不会遍历所有可能的路径。

    【讨论】:

    • 不幸的是,最长路径问题是 NP 完全问题,因此 Dijkstra(它适用于不存在负权重循环的假设)在这里无济于事。你必须为此推导出某种哈密顿路径搜索启发式。但比 OP 的版本更高效。
    • 问题陈述明确指出每个网格单元格中都有非负整数。
    • 但是 Dijkstra 也能找到最短路径,要让它找到最长的路径,你必须否定所有的权重......
    【解决方案3】:
    #include<iostream>
    #include<vector>
    
    using namespace std;
    vector<vector<int> >A;
    vector<vector<bool> >test;
    vector<vector<bool> >test1;
    int sum_max=0;
    int m,n;
    vector<vector<bool> > stamp;
    void color1(int i,int j,vector<vector<bool> >temp_vector,vector<vector<bool> > st,int summ){
    
       temp_vector[i][j]=false;summ+=A[i][j];st[i][j]=true;
     //1.1
      if(i+1<m && temp_vector[i+1][j]){
       if(test1[i+1][j]){
                         if(sum_max<(summ)){sum_max=summ;stamp=st;}
                         }    
    else{color1(i+1,j,temp_vector,st,summ);}
    }
       //1.2
       if(i+1<m){if(!temp_vector[i+1][j]){ if(sum_max<(summ)){sum_max=summ;}}} 
       if(i+1>=m){if(sum_max<(summ)){sum_max=summ;}} 
    
        //2
     if(i-1>=0 && temp_vector[i-1][j]){
              if(test1[i-1][j]){
                         if(sum_max<(summ)){sum_max=summ;}
                         }    
        else{ color1(i-1,j,temp_vector,st,summ);}
         }
       //2.2
       if(i-1>=0){if(!temp_vector[i-1][j]){ if(sum_max<(summ)){sum_max=summ;}}}
          if(i-1<0){if(sum_max<(summ)){sum_max=summ;}} 
    
         //3
         if(j+1<n && temp_vector[i][j+1]){
            if(test1[i][j+1]){
                             if(sum_max<(summ)){sum_max=summ;}
                         }    
        else{      color1(i,j+1,temp_vector,st,summ);}}
      //3.2
       if(j+1<n){if(!temp_vector[i][j+1]){ if(sum_max<(summ)){sum_max=summ;}}}
          if(j+1>=n){if(sum_max<(summ)){sum_max=summ;}} 
    
         //4
         if(j-1>=0 && temp_vector[i][j-1]){
            if(test1[i][j-1]){
                         if(sum_max<(summ)){sum_max=summ;}
                         }    
    else{       color1(i,j-1,temp_vector,st,summ);}}
    //4.2
       if(j-1>=0){if(!temp_vector[i][j-1]){ if(sum_max<(summ)){sum_max=summ;}}}
          if(j+1<0){if(sum_max<(summ)){sum_max=summ;}} 
    
     }
    
    
    void color(int i,int j){
       test[i][j]=false;
      if(i+1<m && test[i+1][j]){
        color(i+1,j);}
         if(i-1>=0 && test[i-1][j]){
               color(i-1,j);
     }
     if(j+1<n && test[i][j+1]){
              color(i,j+1);}
     if(j-1>=0 && test[i][j-1]){color(i,j-1);}
    
    }
    
    int main(){
        int tc;cin>>tc;
        for(int i=0;i<tc;i++){
            int mp,np;
            cin>>mp;
            cin>>np;m=mp;n=np;A.resize(m);test.resize(m);test1.resize(m);int sum=0;
            vector<bool> ha1(m,1);
            vector<bool> ha2(n,1);
            for(int i=0;i<m;i++){A[i].resize(n);test[i].resize(n);test1[i].resize(n);
                    for(int j=0;j<n;j++){
                            cin>>A[i][j];sum+=A[i][j];
                                                        test[i][j]=true;test1[i][j]=false;
                                                        if(A[i][j]==0){test[i][j]=false;ha1[i]=false;ha2[j]=false;}
                            }
                    }cout<<endl;
                   for(int i=0;i<m;i++){cout<<"  "<<ha1[i];} cout<<endl;
                   for(int i=0;i<n;i++){cout<<"  "<<ha2[i];} cout<<endl;
                  cout<<"sum "<<sum<<"\n";
                    int temp_sum=0;
                     for(int i=0;i<m;i++){
                                    for(int j=0;j<n;j++){//if(A[i][j]<=8845){cout<<"\nk "<<A[i][j]<<"  "<<(8845-A[i][j]);}
                                            if(test[i][j]){
                                                           if((i-1)>=0 && test[i-1][j] && (i+1)<m && test[i+1][j] && (j-1)>=0 && test[i][j-1] && (j+1)<n && test[i][j+1] && test[i-1][j-1] && test[i-1][j+1]&& test[i+1][j-1] && test[i+1][j+1]){
                                                                       temp_sum+=A[i][j];test1[i][j]=true;}
    
                                                           }
                                                         //  cout<<test1[i][j]<<"    ";
                                            }//cout<<"\n";
                                            }
    
    //         /*
                     for(int i=0;i<m;i++){
                                    for(int j=0;j<n;j++){
    
                                            if(test1[i][j]){if(!((test1[i-1][j]||test1[i+1][j]) && (test1[i][j-1]||test1[i][j+1]))){
                                                                                             temp_sum-=A[i][j];   test1[i][j]=false;}
                                            }
    
                                                          //
                                                       //    cout<<test1[i][j]<<"    ";
                                            }//
                                           // cout<<"\n";
                                            }
      //              */
    
                   //cout<<"\n temp_sum is "<<temp_sum<<endl;
                   vector<vector<bool> > st(m,vector<bool>(n,0));st=test1;
                     for(int i=0;i<m;i++){
                                    for(int j=0;j<n;j++){
                                            if(test[i][j] && (!test1[i][j])){
                                                           color1(i,j,test,st,0);
    
                                                           }}}
    
                //    cout<<"\nsum is "<<(sum_max+temp_sum)<<endl<<endl;
                cout<<(sum_max+temp_sum)<<endl;
          for(int i=0;i<m;i++){
                                  for(int j=0;j<n;j++){cout<<stamp[i][j]<<"  ";}    cout<<endl;}
    //            cout<<max<<endl;
            A.clear();
            test.clear();
            test1.clear();
            sum_max=0;
                }
    
    
        cout<<endl;system("pause");
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2013-12-03
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多