Biggest Number深搜

简介: You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once.

Description


You have a maze with obstacles and non-zero digits in it:


20210719191755340.png


You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once. When you finish, you can get a number by writing down the digits you encounter in the same order as you meet them. For example, you can get numbers 9784, 4832145 etc. The biggest number you can get is 791452384, shown in the picture above.


Your task is to find the biggest number you can get.


Input


There will be at most 25 test cases. Each test begins with two integers R and C (2≤R,C≤15, R∗C≤30), the number of rows and columns of the maze. The next R rows represent the maze. Each line contains exactly C characters (without leading or trailing spaces), each of them will be either ‘#’ or one of the nine non-zero digits. There will be at least one non-obstacle squares (i.e. squares with a non-zero digit in it) in the maze. The input is terminated by a test case with R=C=0, you should not process it.


Output


For each test case, print the biggest number you can find, on a single line.


Samples


Input


3 7
##9784#
##123##
##45###
0 0


Output


791452384


在训练的时候直接硬搜,TLE


TLE_code:


int n,m;
vector<string>vet[33];
char s[33][33];
bool vis[30][30];
bool edge(int x,int y) {
  if(x < 1 || x > n || y < 1 || y > m) return false;
  return true;
}
int dx[5]= {0,0,0,1,-1};
int dy[5]= {0,1,-1,0,0};
void dfs(int x,int y,int len,string ans) {
  if(!edge(x,y)) return;
  int flag = 0;
  for(int i=1; i<=4; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if(edge(tx,ty) && s[tx][ty] !='#' && !vis[tx][ty]) {
      vis[x][y] = 1;
      flag = 1;
      dfs(tx,ty,len+1,ans+s[x][y]);
      vis[x][y] = 0;
    }
  }
  if(!flag) vet[len+1].push_back(ans+s[x][y]);
}
int main() {
  while(cin >> n >> m && n && m) {
    for(int i=1; i<=n; i++) cin >> (s[i] + 1);
    for(int i=0; i<33; i++) vet[i].clear();
    for(int i=1; i<=n; i++) {
      for(int j=1; j<=m; j++) {
        if(isdigit(s[i][j])) {
          ///memset(vis,0,sizeof vis);
          vis[i][j] = 1;
          dfs(i,j,0,"");
          vis[i][j] = 0;
        }
      }
    }
    for(itn i=30; i>=1; i--) {
      if(vet[i].size()) {
        sort(vet[i].begin(),vet[i].end());
        int siz = vet[i].size();
        printf("%s\n",vet[i][siz-1].c_str());
        /// cout << vet[i][siz-1] <<endl;
        break;
      }
    }
  }
  return 0;
}
/**
3 7
##9784#
##123##
##45###
**/


以上代码是将所有的结果都进行保存放到vector里,然后按照长度从大到小进行查找符合而条件的最大的一个字符串

/*** keep hungry and calm PushyTao!***/
typedef int itn;
int n,m,flag,mx;
char s[33][33];
bool vis[30][30];
char a[33][33];
char ans[40],cur[40];
bool edge(int x,int y) {
  if(x < 1 || x > n || y < 1 || y > m) return false;
  return true;
}
int dx[5]= {0,0,0,1,-1};
int dy[5]= {0,1,-1,0,0};
bool cmp(int len){
  if(!strlen(ans)) return true;
  for(int i=0;i<len;i++){
    if(ans[i] != cur[i]) return ans[i] < cur[i];
  }
  return true;
}
void dfs(int x,int y,int len){
  if(len == mx){
    flag = 1;
    if(cmp(len)) strcpy(ans,cur),ans[len]='\0';
    return;
  }
  for(itn i=1;i<=4;i++){
    int tx = x + dx[i];
    int ty = y + dy[i];
    if(edge(tx,ty) && a[tx][ty] && !vis[tx][ty]){
      cur[len] = a[tx][ty];
      if(cmp(len)){
        ///cur[len] = a[tx][ty];
        vis[tx][ty] = 1;
        dfs(tx,ty,len+1);
        vis[tx][ty] = 0;
      }
    }
  }
}
int main() {
  while(cin >> n >> m && n && m) {
    memset(a,0,sizeof a);
    memset(vis,0,sizeof vis);
    memset(ans,0,sizeof ans);
    flag = 0;
    int cnt = 0;
    for(int i=1;i<=n;i++){
      cin >> (s[i] + 1);
      for(int j=1;j<=m;j++){
        if(s[i][j] != '#') a[i][j] = s[i][j],cnt++;
      }
    }
    for(mx = cnt;!flag;mx--){
      for(itn i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
          if(a[i][j]){
            cur[0] = a[i][j];
            vis[i][j] = 1;
            dfs(i,j,1);
            vis[i][j] = 0;
          }
        }
      }
    }
    cout << ans <<endl;
  }
  return 0;
}


目录
相关文章
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
|
Java
HDOJ/HDU 1297 Children’s Queue(推导~大数)
HDOJ/HDU 1297 Children’s Queue(推导~大数)
148 0
|
算法 Java C语言
LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
823 0
|
Java 数据安全/隐私保护
[LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数。 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛。
1255 0
|
算法
洛谷 P1348 Couple number
题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number。你的工作就是判断一个数N是不是Couple number。 输入输出格式 输入格式: 仅一行,两个长整型范围内的整数$n_1$和$n_2$,之间用1个空格隔开。
1127 0