Numeric Keypad

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
The numberic keypad on your mobile phone looks like below:

1 2 3
4 5 6
7 8 9
0
Suppose you are holding your mobile phone with single hand. Your thumb points at digit 1. Each time you can 1) press the digit your thumb pointing at, 2) move your thumb right, 3) move your thumb down. Moving your thumb left or up is not allowed.

By using the numeric keypad under above constrains, you can produce some numbers like 177 or 480 while producing other numbers like 590 or 52 is impossible.

Given a number K, find out the maximum number less than or equal to K that can be produced.

输入
The first line contains an integer T, the number of testcases.

Each testcase occupies a single line with an integer K.

For 50% of the data, 1 <= K <= 999.

For 100% of the data, 1 <= K <= 10500, t <= 20.

输出
For each testcase output one line, the maximum number less than or equal to the corresponding K that can be produced.

样例输入
3
25
83
131
样例输出
25
80
129

#include "string"
#include "iostream"
#include "cstring"
#define MAX 600
using namespace std;


int T;
char K[MAX];
int S[MAX];
int length;
int result[MAX];
//go[i][j] = true,表示可以从数字i移动到数字j
bool go[10][10] = {  
    {true,  false, false, false, false, false, false, false, false, false}, //0
    {true,  true,  true,  true,  true,  true,  true,  true,  true,  true }, //1
    {true,  false, true,  true,  false, true,  true,  false, true,  true }, //2
    {false, false, false, true,  false, false, true,  false, false, true }, //3
    {true,  false, false, false, true,  true,  true,  true,  true,  true }, //4
    {true,  false, false, false, false, true,  true,  false, true,  true }, //5
    {false, false, false, false, false, false, true,  false, false, true }, //6
    {true,  false, false, false, false, false, false, true,  true,  true }, //7
    {true,  false, false, false, false, false, false, false, true,  true }, //8
    {false, false, false, false, false, false, false, false, false, true }, //9
};

bool dfs(int depth, int last, bool below)
{
    int i, j;
    if(depth >= length)
    {
        /*
        找到一个合法解
        由于总是从高到低对数字进行枚举,因此第一个找到的解一定是最大解
        */
        return true;
    }

    if(below == true)
    {
        /*
        已经有高位小于S的对应位置
        将后面的数位填充为last能够移动到的最大数字
        */
        for(j=9; j>=0; j--)
            if(go[last][j] == true)
                break;
        for(i=depth; i<length; i++)
            result[i] = j;

        return true;
    }

    for(i=9; i>=0; i--)
    {
        if(i<=S[depth] && go[last][i])
        {
            result[depth] = i;
            if(dfs(depth+1, i, i < S[depth]))
                return true;
        }
    }
    return false;
}

int main()
{
    scanf("%d", &T);
    int i, j;
    for(i=0; i<T; i++)
    {
        cin >> K;
        length = strlen(K);
        memset(result, -1, sizeof(S));
        for(j=0; j<length; j++)
            S[j] = K[j] - '0';

        dfs(0, 1, false);

        for(j=0; j<length; j++)
            cout << result[j];
        printf("\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值