HDU 3651 A Simple Problem (DP 记忆化搜索 或者 bfs)

来源:互联网 发布:cf无后坐力软件 编辑:程序博客网 时间:2024/05/20 15:10

A Simple Problem


Problem Description
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 specifc fingers,, one is on the left, the other is on the right. In one unit time, onmylove's two fingers can both operate. For each finger, each operation can be one of the following:
1.press on the button just under the finger.
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 finger.
2. in one unit of time, only one of these two fingers can press the button under it. Of course, the other
finger 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 finger is above the button "5" and the right finger 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
434565747
 

Sample Output
5223
 

Author
onmylove
 

Source
2010 Asia Regional Chengdu Site —— Online Contest
 

Recommend
lcy
 

题目大意:

你的左右手指一开始分别在5,6位置,现在要按顺序按出给定的数字,至少要几步?

要求:

(1)左手始终在右手左边

(2)在1步内,左手可以选择 向右,向左,按下这个数字三种操作之一,右手同样的操作可以在同1步内进行。

(3)在1部内,左手和右手不能同时选择 按下数字 这个操作。

解题思路:

暴力枚举状态,状态也就是 给定的一串数字当前 要按到第几个数字了以及左右手的位置

解题代码:

写了两种实现方法。

代码1:DP记忆化

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <algorithm>using namespace std;const int inf=1e9;string st;int dp[110][15][15];int DP(int len,int x,int y){    if(len==0) return 0;    if(dp[len][x][y]!=-1) return dp[len][x][y];    int ans=inf,pos=st[st.length()-len]-'0';    if(pos==0) pos=10;    for(int i=0;i<pos;i++){        int tmp=DP(len-1,i,pos)+max(abs(i-x),abs(y-pos)+1 );        if(tmp<ans) ans=tmp;    }    for(int i=pos+1;i<=10;i++){        int tmp=DP(len-1,pos,i)+ max(abs(i-y),abs(x-pos)+1 );        if(tmp<ans) ans=tmp;    }    return dp[len][x][y]=ans;}int main(){    while(cin>>st){        memset(dp,-1,sizeof(dp));        cout<<DP(st.length(),5,6)<<endl;    }    return 0;}

代码2:DP bfs队列实现

#include <iostream>#include <cstdio>#include <string>#include <cmath>#include <queue>#include <algorithm>using namespace std;string st;int visited[110][15][15],marked;struct node{    int len,x,y,step;    node(int len0=0,int x0=0,int y0=0,int step0=0){        len=len0;x=x0;y=y0;step=step0;    }};void solve(){    int ans=1e9;    marked++;    node s(0,5,6,0);    queue <node> q;    q.push(s);    visited[s.len][s.x][s.y]=marked;    int len=st.length();    while(!q.empty()){        s=q.front();        q.pop();        if(s.len>=len){            if(s.step<ans) ans=s.step;            continue;        }        int pos=st[s.len]-'0';        if(pos==0) pos=10;        for(int l=s.x-1;l<=s.x+1;l++){            for(int r=s.y-1;r<=s.y+1;r++){                if(l<r && l>=1 && r<=10){                    if(visited[s.len][l][r]!=marked){                        visited[s.len][l][r]=marked;                        q.push(node(s.len,l,r,s.step+1));                    }                }            }        }        if(s.x==pos){            for(int r=s.y-1;r<=s.y+1;r++){                if(s.x<r && r<=10){                    if(visited[s.len+1][s.x][r]!=marked){                        visited[s.len+1][s.x][r]=marked;                        q.push(node(s.len+1,s.x,r,s.step+1));                    }                }            }        }        if(s.y==pos){            for(int l=s.x-1;l<=s.x+1;l++){                if(l<s.y && l>=1){                    if(visited[s.len+1][l][s.y]!=marked){                        visited[s.len+1][l][s.y]=marked;                        q.push(node(s.len+1,l,s.y,s.step+1));                    }                }            }        }    }    cout<<ans<<endl;}int main(){    while(cin>>st){        solve();    }    return 0;}





3 0
原创粉丝点击