hihoCoder week 85 Numeric Keypad 【DFS】
来源:互联网 发布:java quartz 编辑:程序博客网 时间:2024/06/06 08:53
P1 : Numeric Keypad
- Sample Input
32583131
- Sample Output
2580129
Description
The numberic keypad on your mobile phone looks like below:
1 2 34 5 67 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.
Input
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.
Output
For each testcase output one line, the maximum number less than or equal to the corresponding K that can be produced.
题意:给你一个手机键盘,然后我们在手机上输入数字,即当按下一个键之后,只能把手指向下或者向右移动。开始时手指放在1上。问通过这种特殊方式输入的最大不超过S的数字是多少。
思路:DFS 从高位扫一遍,每次选尽可能大的数去填。
AC代码:
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <cstdlib>#define CLR(a, b) memset(a, (b), sizeof(a))#define PI acos(-1.0)using namespace std;typedef long long LL;typedef double DD;bool g[10][10] ={ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,};string str;int ans[502];bool DFS(int len, int lastval, int bit, bool yes){ if(bit == len) return true; if(yes) { for(int i = bit; i < len; i++) ans[i] = lastval ? 9 : 0; return true; } int v = str[bit] - '0'; for(int i = 9; i >= 0; i--) { if(g[lastval][i] && i <= v) { ans[bit] = i; if(DFS(len, i, bit+1, yes|i < v)) return true; } } return false;}int main(){ int t; cin >> t; while(t--) { cin >> str; int len = str.size(); DFS(len, 1, 0, false); for(int i = 0; i < len; i++) cout << ans[i]; cout << endl; } return 0;}
- hihoCoder week 85 Numeric Keypad 【DFS】
- hihocoder #1153 : Numeric Keypad
- Numeric Keypad
- Numeric Keypad
- Numeric Keypad
- Numeric Keypad
- Numeric Keypad
- Numeric Keypad 解题报告
- Numeric Keypad:多思考,别蛮力!
- hihoCoder week 80
- Numeric Keypad 微软2016校园招聘在线笔试第二场
- hihoCoder #1054 dfs
- hihoCoder #1268 : 九宫 【DFS】
- hihocoder 1268 九宫 dfs
- hihocoder #1224 : 赛车 dfs
- hihoCoder week 84 P1 : Lucky Substrings 【dp】
- hihocoder week 86 Spring Outing【思维】
- hihoCoder 92week Miller-Rabin质数测试
- HDU 1435 Stable Match 稳定婚姻
- leetcode: Decode Ways
- sigprocmask
- MVC
- 放肆地使用UIBezierPath和CAShapeLayer画各种图形
- hihoCoder week 85 Numeric Keypad 【DFS】
- 1019. General Palindromic Number (20)
- String类型
- 代码检测工具(一)FindBugs、PMD和CheckStyle概况、安装
- PHP数值的保留2位小数和四舍五入
- 用JAVA写的插入排序
- 代码检测工具(二)findbugs学习
- am335x嵌入式开发-GPIO操作
- spring mvc 的两种配置方式继承controller和xml配置delegate