首页 > 代码库 > HDU 4026 Unlock the Cell Phone(动态规划)

HDU 4026 Unlock the Cell Phone(动态规划)

Unlock the Cell Phone

Problem Description
Modern high-tech cell phones use unlock patterns to unlock the system. The pattern is usually a 3*3 dot array. By moving your finger over there dots, you can generate your personal unlock pattern. More specifically, press your finger over any starting dot, then slide all the way to the next dot, touch it, and so on. Jumping is not allowed. For example, starting from dot 1, you can slide to touch dot 2, dot 4 and dot 5, but sliding directly to dot 3, dot 7 or dot 9 are not allowed. Note that sliding from 1 to 6 and 8 is also allowed because they are not considered as jumping over any dot. However, you can jump a dot if it has been touched before. For example, staring with 1-5-9-6, you can slide directly to dot 4.

Here is a very particular cell phone. It has a dot array of size n*m. Some of the dots are ordinary ones: you can touch, and slide over them when touched before; some are forbidden ones: you cannot touch or slide over them; some are inactive ones: you cannot touch them, but can slide over them. Each dot can only be touched once. You are required to calculate how many different unlock patterns passing through all the ordinary dots.
 

Input
The input contains several test cases. Each test case begins with a line containing two integers n and m (1 <= n, m <= 5), indicating the row and column number of the lock keypad. The following n lines each contains m integers kij indicating the properties of each key, kij=0 stands for an ordinary key, kih=1 stands for a forbidden key; and kij=2 stands for an inactive key. The number of ordinary keys is greater than zero and no more than 16.
 

Output
For each test, output an integer indicating the number of different lock patterns.
 

Sample Input
2 2 0 0 0 0 3 3 0 0 0 0 2 1 0 0 0
 

Sample Output
24 2140
 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4030 4028 4027 4029 4022 
 


题目大意:

给一张图,问你解锁屏幕的方案数,0 表示按键,当被触摸过可以跳过,1表示不能被跳过,2表示可以被跳过。

问你按键的方法数?


解题思路:

用“ 0 1 ” 的二进制 表示0号按键的状态。


解题代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;

typedef long long ll;

int a[10][10],n,m,vsize,pos[10][10];
ll dp[20][(1<<16)];
vector <pair<int,int> > v;

void input(){
    v.clear();
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%d",&a[i][j]);
            if(a[i][j]==0){
                v.push_back(make_pair(i,j));
                pos[i][j]=v.size()-1;
            }
        }
    }
    vsize=v.size();
}

int gcd(int x,int y){
    return y==0?x:gcd(y,x%y);
}

bool canJump(int sum,int s,int d){
    int len=gcd( abs(v[s].first-v[d].first),abs(v[s].second-v[d].second) ) ;
    int offx=(v[d].first-v[s].first)/len,offy=(v[d].second-v[s].second)/len;
    for(int i=1;i<=len-1;i++){
        int x=v[s].first+offx*i,y=v[s].second+offy*i;
        if(a[x][y]==1) return false;
        else if(a[x][y]==0){
            if( ( sum&(1<<pos[x][y]) ) ==0 ) return false;
        }
    }
    return true;
}

ll DP(int s,int sum){
    if( sum==(1<<vsize)-1 ) return 1;
    if(dp[s][sum]!=-1) return dp[s][sum];
    ll ret=0;
    for(int i=0;i<vsize;i++){
        if(  sum&(1<<i)  ) continue;
        if( canJump(sum,s,i) ){
           ret+=DP(i,sum+(1<<i));
        }
    }
    return dp[s][sum]=ret;
}

void solve(){
    ll ans=0;
    for(int i=0;i<vsize;i++){
        ans+=DP(i,(1<<i));
    }
    printf("%I64d\n",ans);
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        input();
        solve();
    }
    return 0;
}