C/C++ Biggest Number

题目描述

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

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.

输入

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.

 

输出

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

 

样例输入

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

样例输出

791452384
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n, m, vs1[35][35], vs2[35][35], len_now, len_ans,total;
char Map[35][35];
char can[35], now[35], ans[35], temp[35];
struct Point{
	int x, y;
	Point(int x = 0, int y = 0){
		this->x = x;
		this->y = y;
	}
}pre, cur;
struct Dir{
	int x;
	int y;
}dir[4] = { 0, 1, 0, -1, 1, 0, -1, 0 };
struct Queue{
    Point data[35];
    int front;
    int rear;
    bool empty(){
        if(rear==front)return true;
        return false;
    }
    void Push(Point e){
        data[rear]=e;
        rear=(rear+1)%35;
    }
    void Pop(){
        front=(front+1)%35;
    }
    Point Front(){
        return data[front];
    }
    void init(){
        front=rear=0;
    }
};
Queue que;
bool cmp(const char c1, const char c2){
	return c1>c2;
}
bool judge(char*now, char*ans){
	if (len_now != len_ans)return len_now>len_ans;
	for (int i = 0; i<len_now; i++){
		if (now[i] != ans[i])return now[i]>ans[i];
	}
}
int bfs(int x, int y){
    que.init();
	memset(vs2, 0, sizeof(vs2));
	vs2[x][y] = 1;
	int ret = 0;
	que.Push(Point(x, y));
	while (!que.empty()){
		pre = que.Front();
		que.Pop();
		can[ret++] = Map[pre.x][pre.y];
		for (int i = 0; i<4; i++){
			cur.x = pre.x + dir[i].x;
			cur.y = pre.y + dir[i].y;
			if (!(0 <= cur.x&&cur.x <= n && 0 <= cur.y&&cur.y <= m))continue;//数组是否越界
			if (!('0' <= Map[cur.x][cur.y] && Map[cur.x][cur.y] <= '9'))continue;//判断是否是数字
			if (!(vs1[cur.x][cur.y] == 0 && vs2[cur.x][cur.y] == 0))continue;//判断是否被访问过
			que.Push(cur);
			vs2[cur.x][cur.y] = 1;
		}
	}
	return ret;
}
void dfs(int x, int y){

	int x1, y1;
	vs1[x][y] = 1;
	now[len_now++] = Map[x][y];
	if (judge(now, ans)){
		for (int i = 0; i<len_now; i++){
			ans[i] = now[i];
		}
		len_ans = len_now;
	}
	for (int i = 0; i<4; i++){
		x1 = x + dir[i].x;
		y1 = y + dir[i].y;
		if (!(0 <= x1&&x1 <= n && 0 <= y1&&y1 <= m))continue;
		if (!('0' <= Map[x1][y1] && Map[x1][y1] <= '9'&&vs1[x1][y1] == 0))continue;
		int maxlen = bfs(x1, y1);
		if (len_now + maxlen<len_ans)continue;
		else if (len_now + maxlen == len_ans){
		    int i;
		    bool ok=false;
		    for(i=0;i<len_now;i++){
                if(now[i]!=ans[i]){
                    ok = now[i] < ans[i];
                    break;
                }
		    }
		    if(ok)continue;

			/*sort(can, can + maxlen, cmp);
			for (int i = 0; i<len_now; i++)temp[i] = now[i];
			for (int i = len_now, k = 0; k<maxlen; k++, i++)temp[i] = can[k];
			int flag=0;
			for (int i = 0; i<len_ans; i++){
				if (temp[i]>ans[i]){
				    flag=1;
				    break;
				}
			}
			if(!flag)continue;*/

		}

		dfs(x1, y1);

	}
	len_now--;
	vs1[x][y] = 0;
}
int main(){
	while (scanf("%d%d", &n, &m) != EOF){
		if (n == 0 && m == 0)break;
		memset(vs1, 0, sizeof(vs1));
		len_now = 0;
		len_ans = 0;
		for (int i = 1; i <= n; i++)scanf("%s", Map[i] + 1);
		for (int i = 1; i <= n; i++){
			for (int k = 1; k <= m; k++){
				if ('0' <= Map[i][k] && Map[i][k] <= '9'){
					dfs(i, k);
				}
			}
		}
		ans[len_ans] = '\0';
		printf("%s\n", ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值