1.题目
【题目描述】
奶牛们按不太传统的方式玩起了小孩子们玩的"跳房子"游戏。奶牛们创造了一个5×55×5的、由与xx,yy轴平行的数字组成的直线型网格,而不是用来在里面跳的、线性排列的、带数字的方格。 然后他们熟练地在网格中的数字中跳:向前跳、向后跳、向左跳、向右跳(从不斜过来跳),跳到网格中的另一个数字上。他们再这样跳啊跳(按相同规则),跳到另外一个数字上(可能是已经跳过的数字)。 一共在网格内跳过五次后,他们的跳跃构建了一个六位整数(可能以00开头,例如000201000201)。 求出所有能被这样创造出来的不同整数的总数。
【输入】
第11到55行: 这样的网格,一行55个整数。
【输出】
第11行: 能构建的不同整数的总数。
【输入样例】
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
【输出样例】
15
【提示】
【样例说明】
输出详细说明: 111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112,121211, 121212, 211111, 211121, 212111和 212121 能够被构建。没有其它可能的数了。
2.代码
#include<bits/stdc++.h>
using namespace std;
int ans;
char ch[6][6];
map<string,bool>f;
int dx[]={0,1, 0,-1};
int dy[]={1,0,-1,0 };
void search(int x,int y,int step,string s)
{
s=s+ch[x][y];
if(step==6)
{
if(f.count(s)==0)
f[s]=1,ans++;
return;
}
for(int i=0;i<=3;i++)
{
int px=x+dx[i];
int py=y+dy[i];
if(px>=1 && px<=5 && py>=1 && py<=5)
search(px,py,step+1,s);
}
}
int main()
{
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
cin>>ch[i][j];
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
search(i,j,1,"");
cout<<ans;
return 0;
}