zoukankan      html  css  js  c++  java
  • hdu 2216 Game III

    网上查了标记路径的方法之后就是一道普通的搜索题了,自己还是想不到关键的,努力!

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    struct node
    {
        int zx,zy,sx,sy,s;
    } t,t0;
    char g[25][25];
    int m,n,vis[25][25][25][25];
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0};
    int fx[]={0,0,-1,1};
    int fy[]={-1,1,0,0};
    queue<node>q;
    
    int check(int z1,int z2,int s1,int s2)
    {
        int i;
        if(z1==s1&&z2==s2)
            return 1;
    
        for(i=0;i<4;i++)
            if(z1+dx[i]==s1&&z2+dy[i]==s2)
                return 1;
    
        return 0;
    }
    
    int bfs(int z1,int z2,int s1,int s2)
    {
        int i,flag=-1;
        if(check(z1,z2,s1,s2)==0)
        {
            t.zx=z1;
            t.zy=z2;
            t.sx=s1;
            t.sy=s2;
            t.s=0;
            g[z1][z2]='.';
            g[s1][s2]='.';
            vis[z1][z2][s1][s2]=1;
            while(!q.empty()) q.pop();
            q.push(t);
            while(!q.empty())
            {
                t0=q.front();
                q.pop();
                if(check(t0.zx,t0.zy,t0.sx,t0.sy))
                {
                    flag=t0.s;
                    break;
                }
    
               for(i=0;i<4;i++)
               {
                   t.zx=t0.zx+dx[i];
                   t.zy=t0.zy+dy[i];
                   t.sx=t0.sx+fx[i];
                   t.sy=t0.sy+fy[i];
                   t.s=t0.s+1;
                   if(t.zx<0||t.zx>n||t.zy<0||t.zy>m||g[t.zx][t.zy]!='.') continue;
                   else
                   {
                       if(t.sx<0||t.sx>n||t.sy<0||t.sy>m||g[t.sx][t.sy]!='.')
                       {
                           t.sx=t0.sx;
                           t.sy=t0.sy;
                       }
                       if(!vis[t.zx][t.zy][t.sx][t.sy])
                       {
                           vis[t.zx][t.zy][t.sx][t.sy]=1;
                           q.push(t);
                       }
                   }
               }
            }
        }
        else flag=0;
        return flag;
    }
    
    int main()
    {
        int i,j,z1,z2,s1,s2;
        while(~scanf("%d%d",&n,&m))
        {
            memset(vis,0,sizeof(vis));
    
            for(i=0; i<n; i++)
                scanf("%s",g[i]);
    
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    if(g[i][j]=='Z')
                    {
                        z1=i;
                        z2=j;
                    }
                    else if(g[i][j]=='S')
                    {
                        s1=i;
                        s2=j;
                    }
    
            int k=bfs(z1,z2,s1,s2);
    
            if(k==-1)
                printf("Bad Luck!
    ");
            else
                printf("%d
    ",k);
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    自学python day 10 函数的动态参数、命名空间、作用域
    老男孩 python 自学 打印05 dict 复习总结
    老男孩python 自学day09 函数开始
    今天
    day 03
    eclipse如何安装配置tomcat
    windows上配置maven环境
    如何创建ssh key使电脑和Github关联在一起
    怎么将本地文件上传到github
    使用git工具上传代码到github
  • 原文地址:https://www.cnblogs.com/xryz/p/4847985.html
Copyright © 2011-2022 走看看