BUPT OJ177 Android key

44 篇文章 3 订阅

题目描述

Xixi has a smart phone with Symbian OS. She loves it for its convenience and massive software supplies. But recently, she could not help but notice that more and more people are using Android. Not only the staff at Google, but also her boyfriend and many other ACM team members are turning to Android users.
If you are already a user of Android, you must be familiar with its key lock system. The Android lock system uses a series of nine dots in a 3 x 3 square that you need to replicate a pre-set pattern to unlock the phone.
Starting from one dot in the 3 x 3 square, you can then move to the adjacent dot in four directions, up, down, left, and right. Sometimes you are on the edge or corner, your choices will be limited to 2 or 3 directions.
Another rule of setting the lock is that you cannot move to the dot that you have already visited. So 4-5-2-1 is a valid lock, whereas 4-5-2-1-4 is not.
Now xixi has found poemqiong’s new cell, and it is an Android phone. Knowing the length of peomqiong’s phone lock is K, she wonders how many different lock patterns are there.

To make this problem a little more difficult, we assume that the keyboard is N x M square, instead of 3 x 3. An N x M square has N columns and M rows.

Now, given N (1<=N<=10), M (1<=M<=10) and the length of the lock pattern K (0<=K<=10), can you write a program to help xixi figure out how many patterns are there?

 

 

 

输入格式

 N (1<=N<=10), M (1<=M<=10) and the length of the lock pattern K (0<=K<=10)

输出格式

The number of different patterns, noting that 4-5 and 5-4 are two different patterns.

输入样例

2 2 3
3 3 2

输出样例

8
24


比较普通的思路, dfs求解, 理应可以通过对称简化来着?

比赛时也没想这么多, 从头到尾dfs了一遍...嘛, 过了就行了



/*
USER_ID: test#birdstorm
PROBLEM: 177
SUBMISSION_TIME: 2014-03-08 01:06:00
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX(x,y) (x)>(y)?(x):(y)
#define For(i,m,n) for(i=m;i<n;i++)
#define MAXN 15
 
int n, m, k, step, num;
int move[4][2]={1,0,-1,0,0,1,0,-1}, vis[MAXN][MAXN];
 
void dfs(int x, int y)
{
    int i, j, tx, ty;
    if(step==k) num++;
    else
    {
        For(i,0,4){
            tx=x+move[i][0];
            ty=y+move[i][1];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty])
            {
                vis[tx][ty]=1; step++;
                dfs(tx,ty);
                vis[tx][ty]=0; step--;
            }
        }
    }
}
 
main()
{
    int i, j;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        num=0;
        For(i,1,n+1) For(j,1,m+1){
            step=1;
            memset(vis,0,sizeof(vis));
            vis[i][j]=1;
            dfs(i,j);
        }
        printf("%d\n",num);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值