zoukankan      html  css  js  c++  java
  • Codevs 1215 迷宫

    时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold
    题目描述 Description

    在N*N的迷宫内,“#”为墙,“.”为路,“s”为起点,“e”为终点,一共4个方向可以走。从左上角((0,0)“s”)位置处走到右下角((n-1,n-1)“e”)位置处,可以走通则输出YES,不可以走则输出NO。

    输入描述 Input Description

    输入的第一行为一个整数m,表示迷宫的数量。 
    其后每个迷宫数据的第一行为一个整数n(n≤16),表示迷宫的边长,接下来的n行每行n个字符,字符之间没有空格分隔。

    输出描述 Output Description

    输出有m行,每行对应的迷宫能走,则输出YES,否则输出NO。

    样例输入 Sample Input
    1 
    7
    s...##.
    .#.....
    .......
    ..#....
    ..#...#
    ###...#
    ......e
    样例输出 Sample Output
    YES
    优先队列,又你妈MLE5555....
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #define N 18
     6 using namespace std;
     7 int map[N][N],n,T,x,y,sx,sy;
     8 int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};
     9 queue<int> qx,qy;
    10 bool BFS()
    11 {
    12     qx.push(x);qy.push(y);
    13     while(!qx.empty())
    14     {
    15         int zx=qx.front();int zy=qy.front();
    16         qx.pop();qy.pop();
    17         if(zx==sx&&zy==sy)
    18           return true;
    19         for(int i=0;i<4;i++)
    20         {
    21             int xx=zx+dx[i],yy=zy+dy[i];
    22             if(map[xx][yy]==1)
    23             {
    24                 qx.push(xx);qy.push(yy);
    25             }
    26         }
    27     }
    28     return false;
    29 }
    30 int main()
    31 {
    32     char sh;
    33     scanf("%d",&T);
    34     while(T--)
    35     {
    36         scanf("%d",&n);
    37         memset(map,0,sizeof(map));
    38         for(int i=1;i<=n;i++)
    39           for(int j=1;j<=n;j++)
    40           {
    41               cin>>sh;
    42               if(sh=='#') map[i][j]=0;
    43               if(sh=='.') map[i][j]=1;
    44               if(sh=='s'){
    45                   x=i;y=j;map[i][j]=1;
    46               }
    47             if(sh=='e'){
    48                 sx=i;sy=j;map[i][j]=1;
    49             }
    50           }
    51         if(BFS())
    52           printf("YES
    ");
    53         else printf("NO
    ");
    54     }
    55     return 0; 
    56 }

    AC代码:(DFS)

     1  #include <iostream>
     2 using namespace std;
     3 const int MAXN=18;
     4 char map[MAXN][MAXN];
     5 int book[MAXN][MAXN];
     6 int flag=0;
     7 int m,n;
     8 int dx[]={0,0,1,-1};
     9 int dy[]={1,-1,0,0};
    10 bool islegal(int x,int y)
    11 {
    12     if (x>=1 && x<=m &&y>=1 && y<=m)
    13         return true;
    14     else return false;
    15 }
    16 void dfs(int x,int y);
    17 int main()
    18 {
    19     cin>>n>>m;
    20     for (int i=1;i<=m;i++)
    21         for (int j=1;j<=m;j++)
    22             cin>>map[i][j];
    23     int p,q;
    24     dfs(1,1);
    25     if (flag==0)
    26         cout<<"NO"<<endl;
    27     return 0;
    28 }
    29 void dfs(int x,int y)
    30 {
    31     book[x][y]=1;
    32     if(flag==1)
    33         return;
    34     if (x==m && y==m)
    35     {
    36         flag=1;
    37         cout<<"YES"<<endl;
    38     }
    39     for (int i=0;i<4;i++)
    40     {
    41         int nx=x+dx[i];
    42         int ny=y+dy[i];
    43         if (map[nx][ny]!='#' && book[nx][ny]==0 && islegal(nx,ny))
    44         {
    45             dfs(nx,ny);
    46             book[nx][ny]==0;
    47         }
    48     }
    49 }
  • 相关阅读:
    阿里P8架构师谈:阿里双11秒杀系统如何设计?
    秒杀系统设计的知识点
    秒杀系统架构优化思路
    秒杀系统解决方案
    Entity Framework Code First (七)空间数据类型 Spatial Data Types
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6071930.html
Copyright © 2011-2022 走看看