题目描述
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 15int 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;
}