zoukankan      html  css  js  c++  java
  • 搜索例题

    http://acm.sdibt.edu.cn/vjudge/contest/view.action?cid=1977#problem/A

    广搜:

    e2 e4
    a1 b2
    b2 c3
    a1 h8
    a1 h7
    h8 a1
    b1 c3
    f6 f6

    在一个8*8的棋盘中,a1代表第零行的第一个元素,每个棋子只能像象棋中“马”跳的方式跳,问从前面跳到后面需要最少几步(广搜);

    代码如下:

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<algorithm>
    using namespace std;
    char a,b;
    int a1,b1;
    int mapp[10][10];
    int bj[9][3]= {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    struct p
    {
        char a;
        int a1;
        int step;
    } x,y,neww;
    queue<p>q;
    int bfs()
    {
        int i;
        x.a=a;
        x.a1=a1;
        x.step=0;
        while(q.size()!=0)
            q.pop();
        memset(mapp,0,sizeof(mapp));
        mapp[x.a-'a'][x.a1]=1;
        q.push(x);
        while(q.size()!=0)
        {
            y=q.front();
            q.pop();
            if(y.a==b&&y.a1==b1)
            {
                return y.step;
            }
            x=y;
            for(i=0; i<=7; i++)
            {
                int xx=x.a-'a'+bj[i][0];   
                int yy=x.a1+bj[i][1];
                if(xx>=0&&xx<=7&&yy>=1&&yy<=8&&mapp[xx][yy]==0)
                {
                    neww.step=x.step+1;
                    mapp[xx][yy]=1;
                    neww.a=xx+'a';
                    neww.a1=yy;
                    q.push(neww);
                }
            }
    
        }
    }
    int main()
    {
        while(~scanf("%c",&a))
        {
            scanf("%d",&a1);
            getchar();
            scanf("%c",&b);
            scanf("%d",&b1);
            getchar();
            printf("To get from %c%d to %c%d takes %d knight moves.
    ",a,a1,b,b1,bfs());
        }
        return 0;
    }
    

    深搜:

    6 9
    ....#.
    .....#
    ......
    ......
    ......
    ......
    ......
    #@...#
    .#..#.
    11 9
    .#.........
    .#.#######.
    .#.#.....#.
    .#.#.###.#.
    .#.#..@#.#.
    .#.#####.#.
    .#.......#.
    .#########.
    ...........
    11 6
    ..#..#..#..
    ..#..#..#..
    ..#..#..###
    ..#..#..#@.
    ..#..#..#..
    ..#..#..#..
    7 7
    ..#.#..
    ..#.#..
    ###.###
    ...@...
    ###.###
    ..#.#..
    ..#.#..
    0 0

    45
    59
    6
    13

    #include <iostream>
    #include<stdio.h>
    #include<string.h>
    int a,b;
    char c[200][200];
    int d[200][200];
    using namespace std;
    void l(int i,int j)
    {
        if(i>=0&&j>=0&&i<b&&j<a)
        {
            if(d[i][j]==0&&c[i][j]=='.')
            {
                d[i][j]=1;
                l(i,j-1);
                l(i,j+1);
                l(i+1,j);
                l(i-1,j);
            }
        }
    }
    int main()
    {
        int m,n,sum=0,i,j;
        while(~scanf("%d%d",&a,&b))
        {
            getchar();
            memset(d,0,sizeof(d));
            if(a==b&&a==0)
                break;
            sum=0;
            for(i=0; i<=b-1; i++)
            {
                for(j=0; j<=a-1; j++)
                {
                    scanf("%c",&c[i][j]);
                    if(c[i][j]=='@')
                    {
                        m=i;
                        n=j;
                    }
                }
                getchar();
            }
            c[m][n]='.';
            l(m,n);
            for(i=0; i<=b-1; i++)
                for(j=0; j<=a-1; j++)
                        sum+=d[i][j];
            printf("%d
    ",sum);
        }
    
    
    }
  • 相关阅读:
    Djano restframework
    python测试一
    SQL分类,DDL,DML,DCL
    sql查询时,根据特定的条件给表的某一个字段赋值
    数据类型之Nullable
    web.config节点
    拼凑的宿主-host
    css的优先级
    jquery——write less,do more
    double类型计算
  • 原文地址:https://www.cnblogs.com/bhd123/p/9498138.html
Copyright © 2011-2022 走看看