P1168跳房子(焫鷄如我)

描述 Description

奶牛们按不太传统的方式玩起了小孩子们玩的"跳房子"游戏。奶牛们创造了

一个5x5的、由与x,y轴平行的数字组成的直线型网格,而不是用来在里面跳

的、线性排列的、带数字的方格。

然后他们熟练地在网格中的数字中跳:向前跳、向后跳、向左跳、向右跳

(从不斜过来跳),跳到网格中的另一个数字上。他们再这样跳啊跳(按相同规

则),跳到另外一个数字上(可能是已经跳过的数字)。

一共在网格内跳过五次后,他们的跳跃构建了一个六位整数(可能以0开头,

例如000201)。

求出所有能被这样创造出来的不同整数的总数。

 

输入格式 Input Format

* 第1到5行: 这样的网格,一行5个整数

输出格式 Output Format

* 第1行: 能构建的不同整数的总数

样例输入 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

(输出详细说明:

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112,

121211, 121212, 211111, 211121, 212111和 212121 能够被构建。没有其它可

能的数了。)

时间限制 Time Limitation

1s

 

这是一道非常早的laji搜索题然而我现在才写出了QAQ(我果真太弱了)

直接用DFS搜索然后每到6位时记录一下这个数字是否出现过如果没有出现过答案就加一

中间我用了一个新了解到的知识:

就是讲字符串转换为int整形(QAQ)

在程序开头加一个#include<cstdlib>(或者(#include<stdlib.h>)反正c++是一样的)

然后就比方说:

stirng s;//(声明一个字符串)

s="123456";

int n=atoi(s.c_str());

cout<<n<<endl;

然后n就是123456;(注意:这个函数只能转换数字,如果是字母就不行了)

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
char a[50][50];
int dx[7]={-1,0,1,0};
int dy[7]={0,1,0,-1};
string s;
bool ans[1110000];
int t=0;
void dfs(int x,int y,int b,string s)
{
    if(b>5)
    {
        int n=atoi(s.c_str());
        if(ans[n]==false)
        {
            ans[n]=true;
            t++;
        }
    }
    else
    {
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(xx>5||yy>5||xx<=0||yy<=0)
                continue;
            if(a[xx][yy]==' ')
                continue;
            dfs(xx,yy,b+1,s+a[xx][yy]);
        }
    }
}
int main()
{
    memset(ans,false,sizeof(ans));
    for(int i=1;i<=5;i++)
        for(int j=1;j<=5;j++)
            cin>>a[i][j];
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=5;j++)
        {
            s=a[i][j];
            dfs(i,j,1,s);
            s="";
        }
    }
    cout<<t<<endl;
    return 0;
}
o( ̄▽ ̄)d

 

posted @ 2017-06-02 11:12  列車員lcy  阅读(686)  评论(0编辑  收藏  举报