码迷,mamicode.com
首页 > 其他好文 > 详细

九度 题目1335:闯迷宫 题目1365:贝多芬第九交响曲

时间:2014-11-03 00:05:14      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   for   sp   on   2014   art   


转载请注明本文地址http://blog.csdn.net/yangnanhai93/article/details/40718149


简单说说宽度优先搜索BFS


说实话,这是第一个自己写的宽度优先搜索的题目,之前也是不太明白之间的区别,好吧,只能说自己学的太渣……


言归正传,对于初学者来说,可能最大的概念就是一个是深度搜索,一个是宽度搜索,好吧,我表示废话了,我其实就是这个样子的,然后一直不得甚解。。。所以第一次上来,我就直接搜索DFS,结果太明显,就是TLE或者MLE,然后就抓狂中,这可能是很多初学者在开始的时候犯的错误了。


我个人的感觉宽度搜索和深度搜索都是很暴力的枚举,但是区别呢,还是比较明显的,就比如下面这两题来说,基本上的都是一样,通过题目的描述,都是最快找到,在深度优先搜索中就是要找到所有能到达的最短路径了,所以,其实应该说都能够找到的,只是花费的时间不一样而已。


总结起来就是以下几点:

1:宽度优先搜索的用意一般都会比较明显,比如最小啊,就是有比较限制的时候,比较多的时候会用宽度优先,这个,可以用一个比喻来说,就是从一个点,滴墨水,看谁先到,这就是宽度,每做一步,墨水都会扩散,然后得到新的起始点,继续扩散,一个循环的过程。

2:深度优先的话,用于枚举多少类型的时候会比较多,很常见的应用就是8皇后,N皇后的问题了


附上两题的题解

 题目1365:贝多芬第九交响曲

题目链接地址http://ac.jobdu.com/problem.php?pid=1365


#include <stdio.h>
#include <queue>
#include <memory.h>
using namespace  std;
 
struct Node
{
    int x, y,step;
};
bool A[101][101];
short testCase[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
int Cal(int x,int y,int ex,int ey,int num)
{
    if(x==ex&&y==ey)
        return 0 ;
    queue <Node> p;
    Node tmp,q;
    int result=0;
    tmp.x=x;
    tmp.y=y;
    tmp.step=0;
    p.push(tmp);
    while(p.size()>0)
    {
        tmp=p.front();
        p.pop();
        for (int i=0;i<8;i++)
        {
            q.x=tmp.x+testCase[i][0];
            q.y=tmp.y+testCase[i][1];
            if(q.x>0&&q.y<=num&&q.x<=num&&q.y>0&&!A[q.x][q.y])
            {
                q.step=tmp.step+1;
                A[q.x][q.y]=true;
                if(q.x==ex&&q.y==ey)
                    return q.step;
                else
                    p.push(q);
            }
        }
 
    }
    return -1;
}
 
int main()
{
    int num,x,y,ex,ey;
    //freopen("data.in","r",stdin);
    while(scanf("%d",&num)!=EOF)
    {
        memset(A,0,sizeof(A));
        scanf("%d%d%d%d",&x,&y,&ex,&ey);
        printf("%d\n",Cal(x,y,ex,ey,num));
    }
    return 0;
}
/**************************************************************
    Problem: 1365
    User: vincent_ynh
    Language: C++
    Result: Accepted
    Time:450 ms
    Memory:1064 kb
****************************************************************/


九度 题目1335:闯迷宫

题目链接地址:http://ac.jobdu.com/problem.php?pid=1335

#include <stdio.h>
#include <memory.h>
#include <queue>
using namespace std;
//#define LOCAL
bool A[100][100];
struct Node
{
    int x,y,step;
};
 
int Cal(int num)
{
    if(num==1)
        return 0;
    Node first,second;
    first.x=0;
    first.y=0;
    first.step=0;
    int testCase[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    queue<Node> result;
    result.push(first);
    while(result.size()>0)
    {
        first=result.front();
        A[first.x][first.y]=true;
        result.pop();
        for (int i=0;i<4;i++)
        {
            second.x=first.x+testCase[i][0];
            second.y=first.y+testCase[i][1];
            second.step=first.step+1;
            if(second.x>=0&&second.y<num&&second.x<num&&second.y>=0&&!A[second.x][second.y])
            {
                A[second.x][second.y]=true;
                if(second.x==num-1&&second.y==num-1)
                    return second.step;
                else
                    result.push(second);
            }
             
        }
    }
    return -1;
}
int main()
{
    int inf=10000;
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        for(int i=0;i<num;i++)
            for(int j=0;j<num;j++)
            {
                scanf("%d",&A[i][j]);
            }
        if(A[0][0]==false&&A[num-1][num-1]==false)
            printf("%d\n",Cal(num));
        else
            printf("-1\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1335
    User: vincent_ynh
    Language: C++
    Result: Accepted
    Time:100 ms
    Memory:1064 kb
****************************************************************/


九度 题目1335:闯迷宫 题目1365:贝多芬第九交响曲

标签:blog   http   io   ar   for   sp   on   2014   art   

原文地址:http://blog.csdn.net/yangnanhai93/article/details/40718149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!