zoukankan      html  css  js  c++  java
  • hdu 1728 逃离迷宫 [ dfs ]

    传送门

    逃离迷宫

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 17478    Accepted Submission(s): 4247


    Problem Description
      给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
     
    Input
      第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
      第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
     
    Output
      每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
     
    Sample Input
    2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
     
    Sample Output
    no yes
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1253 1072 1026 1372 1180 
     
     
    题解:
    dfs,记得用vis记录,防止重复搜索
    13261396 2015-03-27 21:05:25 Accepted 1728 93MS 4272K 2241 B G++ czy
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <stack>
      4 #include <vector>
      5 #include <map>
      6 #include <algorithm>
      7 
      8 #define ll long long
      9 int const N = 105;
     10 int const M = 205;
     11 int const inf = 1000000000;
     12 ll const mod = 1000000007;
     13 
     14 using namespace std;
     15 
     16 int T;
     17 int n,m;
     18 int k;
     19 int vis[N][N][12][5];
     20 
     21 struct PP
     22 {
     23     int x;
     24     int y;
     25     int operator ==(const PP &b) const
     26     {
     27         if(x==b.x && y==b.y) return 1;
     28         else return 0;
     29     }
     30 };
     31 
     32 PP st,en;
     33 char s[N][N];
     34 int dirx[]={-1,0,1,0};
     35 int diry[]={0,1,0,-1};
     36 int flag;
     37 
     38 void ini()
     39 {
     40     int i,j;
     41     flag=0;
     42     scanf("%d%d",&n,&m);
     43     memset(vis,0,sizeof(vis));
     44     for(i=0;i<=n+1;i++){
     45         for(j=0;j<=m+1;j++){
     46             s[i][j]='*';
     47         }
     48     }
     49     for(i=1;i<=n;i++){
     50         scanf("%s",s[i]+1);
     51     }
     52     scanf("%d%d%d%d%d",&k,&st.y,&st.x,&en.y,&en.x);
     53 }
     54 
     55 void dfs(PP te,int d,int num)
     56 {
     57 
     58     if(num>k){
     59         return;
     60     }
     61     if(te==en){
     62         flag=1;return;
     63     }
     64     int i;
     65     PP nt;
     66     for(i=0;i<4;i++){
     67         nt.x=te.x+dirx[i];
     68         nt.y=te.y+diry[i];
     69         if(s[nt.x][nt.y]=='.'){
     70             if(i==d){
     71                 if(vis[nt.x][nt.y][num][i]==1) continue;
     72                 vis[nt.x][nt.y][num][i]=1;
     73                 dfs(nt,i,num);
     74             }
     75             else{
     76                 if(vis[nt.x][nt.y][num+1][i]==1) continue;
     77                 vis[nt.x][nt.y][num+1][i]=1;
     78                 dfs(nt,i,num+1);
     79             }
     80             if(flag==1) return;
     81         }
     82     }
     83 }
     84 
     85 void solve()
     86 {
     87     int d;
     88     PP nt;
     89     if(nt==en){
     90         flag=1;return;
     91     }
     92     for(d=0;d<4;d++){
     93         if(flag==1) return;
     94         nt.x=st.x+dirx[d];
     95         nt.y=st.y+diry[d];
     96         if(s[nt.x][nt.y]=='.'){
     97             vis[nt.x][nt.y][0][d]=1;
     98             dfs(nt,d,0);
     99         }
    100     }
    101 }
    102 
    103 void out()
    104 {
    105     if(flag==1){
    106         printf("yes
    ");
    107     }
    108     else{
    109         printf("no
    ");
    110     }
    111 }
    112 
    113 int main()
    114 {
    115     //freopen("data.in","r",stdin);
    116    // freopen("data.out","w",stdout);
    117     scanf("%d",&T);
    118     //for(int cnt=1;cnt<=T;cnt++)
    119     while(T--)
    120     //while(scanf("%d%d",&n,&sum)!=EOF)
    121     {
    122         ini();
    123         solve();
    124         out();
    125     }
    126 }
  • 相关阅读:
    Python PEP8 编码规范中文版
    MySQL分区表
    mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
    斐讯K2刷不死breed与第三方固件教程
    Mysql 多表连接查询 inner join 和 outer join 的使用
    mysql多表关联删除示例
    sublime Text 几款插件
    多进程vs多线程
    git 命令常用总结
    LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4372857.html
Copyright © 2011-2022 走看看