2010 ACM/ICPC Online-Contest-SCU[四川赛区网络预选赛]

Problem A.A Simple Problem

比赛时唯一做出来的一道题。

描述:

Time limit: 1 second
Memory limit: 256 megabytes
There's one row of buttons lying on onmylove's laptop keyboard, which are used to input the numbers,
just as shown in the picture:


onmylove used to input the numbers with his two speci c ngers, one is on the left, the other is on the
right. In one unit time, onmylove's two ngers can both operate. For each nger, each operation can be
one of the following:
1.press on the button just under the nger.
2. move to the left button or the right button.
But there're still some points you should pay attention to:
1. at any time, the left nger should at the left side of the right nger.
2. in one unit of time, only one of these two ngers can press the button under it. Of course, the other
nger can move at this unit of time.
Now you are given a string which consists of only numbers, you are asked to calculate: if onmylove want
to input all these numbers, how many time does he need at least? At the every beginning, the left nger
is above the button \5" and the right nger is above the button \6".


Input
Multiple inputs. For each test case, there is one string at a line. It's promised there're only numbers in
the string and the length of it is not more than 100.


Output
For each test case, output one number at a line, which represents the least time you need.
Sample input and output


input:
434
56
57
47

 

output:
5
2
2
3


Problemsetter
onmylove

 

分析:这是一道DP题,根据时间划分阶段,状态d[t,i,j]表示第t秒,左手放在i,右手放在j可以敲出的最多字符数。根据题目规则,有9种转移方式,

用一个二维数组表示就是:dir[9][2] = {{0, -1}, {0, 1}, {-1, 0}, {-1, 1}, {-1, -1}, {1, 0}, {1, -1}, {1, 1}, {0, 0}}。

 

代码
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <math.h>
6 #include <set>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 #include <algorithm>
11  using namespace std;
12
13  #define NL1 201
14  #define NL2 21
15 #define EP 1e-10
16 #define MAX(x,y) ((x)>(y)?(x):(y))
17 #define MIN(x,y) ((x)<(y)?(x):(y))
18 #define LL long long
19
20 int d[NL2][NL2], d1[NL2][NL2];
21 int dir[9][2] = {{0, -1}, {0, 1}, {-1, 0}, {-1, 1}, {-1, -1}, {1, 0}, {1, -1}, {1, 1}, {0, 0}};
22 char ch[NL1];
23
24 int main() {
25 int i, j, k;
26 // freopen("in.txt", "r", stdin);
27 while (scanf("%s", ch+1) != EOF) {
28 int L = strlen(ch+1);
29 memset(d, -1, sizeof(d));
30 d[5][6] = 0;
31 int t=0;
32 bool suc = false;
33 while (1) {
34 t++;
35 memset(d1, -1, sizeof(d1));
36 for (i=1; i<=9; i++) {
37 for (j=i+1; j<=10; j++) {
38 if (d[i][j]>=0) {
39 for (k=0; k<9; k++) {
40 int i0 = i+dir[k][0];
41 int j0 = j+dir[k][1];
42 if (i0<j0 && i0>=1 && i0<=9 && j0>=2 && j0<=10) {
43 if (i == i0 && ch[d[i][j]+1] == i+'0') {
44 if (d1[i0][j0] == -1) {
45 d1[i0][j0] = d[i][j]+1;
46 }else {
47 d1[i0][j0] = MAX(d1[i0][j0], d[i][j]+1);
48 }
49 }
50 if (j==j0 && ch[d[i][j]+1] == ((j==10)?0:j)+'0') {
51 if (d1[i0][j0] == -1) {
52 d1[i0][j0] = d[i][j]+1;
53 }else {
54 d1[i0][j0] = MAX(d1[i0][j0], d[i][j]+1);
55 }
56 }
57 if (i!=i0 && j!=j0) {
58 if (d1[i0][j0] == -1) {
59 d1[i0][j0] = d[i][j];
60 }else {
61 d1[i0][j0] = MAX(d1[i0][j0], d[i][j]);
62 }
63 }
64 if (d1[i0][j0] == L) {
65 suc = true;
66 break;
67 }
68 }
69 }
70 if (suc) break;
71 }
72 if (suc) break;
73 }
74 if (suc) break;
75 }
76 for (i=0; i<=9; i++) {
77 for (j=i+1; j<=10; j++) {
78 d[i][j] = d1[i][j];
79 }
80 }
81 if (suc) break;
82 }
83 printf("%d\n", t);
84 }
85 return 0;
86 }
87

 

posted @ 2010-09-25 13:30  superbin  阅读(417)  评论(0编辑  收藏  举报