题目链接:http://poj.org/problem?id=3050
题面:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2563 | Accepted: 1823 |
Description
Input
Output
Sample Input
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1
Sample Output
15
Hint
Source
题目大意:
一只牛在5*5的方格内可以从任意点开始跳,每次可以往4个方向跳,共跳5次,根据走过的方格,形成一个长度为6的序列,问这样的序列有多少种?(方格可以重复跳。)
解题:
题目挂的时候写的是穷竭搜索,看到图这么小,估计就是暴搜的意思吧。枚举从每个点开始跳,跳到5步后停止,并记录此时形成的路径,用set排重计数即可。
代码:
#include
#include
#include
#include
#include
using namespace std;
char map[6][6],path[10];
//四个方向
int dir[4][2]={0,1,-1,0,0,-1,1,0};
//计数
set cnt;
string tmp;
//判断是否方格内
bool inside(int x,int y)
{
if(x>=1&&x<=5&&y>=1&&y<=5)
return true;
else
return false;
}
//深搜
void dfs(int x,int y,int step)
{
int tx,ty;
path[step]=map[x][y];
if(step==5)
{
//记录路径
tmp="";
for(int i=0;i<=5;i++)
tmp+=path[i];
cnt.insert(tmp);
return;
}
//往四个方向跳
for(int i=0;i<4;i++)
{
tx=x+dir[i][0];
ty=y+dir[i][1];
if(inside(tx,ty))
dfs(tx,ty,step+1);
}
}
int main()
{
//读入
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
scanf(" %c",&map[i][j]);
//枚举从每个点开始
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
dfs(i,j,0);
//输出
printf("%d\n",cnt.size());
return 0;
}