zoukankan      html  css  js  c++  java
  • bfs简单题-poj2251

    宽搜基础题

    思路很简单,注意细节。

    走过的节点一定要打上标记//tag数组

    三维字符串输入一定要注意

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string>
     4 #include <algorithm>
     5 #include <string>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <queue>
     9 #define MAXN 35
    10 
    11 using namespace std;
    12 
    13 int L,R,C;
    14 char a[MAXN][MAXN][MAXN];
    15 int tag[MAXN][MAXN][MAXN];
    16 int ans;
    17 struct pos{
    18     int x,y,z;
    19     int t;
    20 };
    21 pos start;
    22 int dx[6] = {1, 0, -1, 0 , 0 , 0}, dy[6] = {0, 1, 0, -1,0 ,0},dz[6] = {0, 0, 0, 0,1,-1};
    23 
    24 int judge(int nx,int ny,int nz)
    25 {
    26     if(0 <= nx && nx < L && 0 <= ny && ny < R && 0 <= nz && nz < C)
    27         return 1;
    28     else return 0;
    29 }
    30 int bfs()
    31 {
    32     queue<pos> que;
    33     que.push(start);
    34     while(!que.empty())
    35     {
    36         pos temp = que.front();
    37         que.pop();
    38         for(int i = 0;i<6;i++)
    39         {
    40             int nx = temp.x-dx[i],ny = temp.y-dy[i],nz = temp.z-dz[i];
    41             if(a[nx][ny][nz] == 'E')
    42             {
    43                 printf("Escaped in %d minute(s).
    ",temp.t+1);
    44                 return 1;
    45             }
    46             if(judge(nx,ny,nz) && a[nx][ny][nz]=='.'&&!tag[nx][ny][nz])
    47             {
    48                 pos now;
    49                 now.x = nx;now.y = ny;now.z = nz;
    50                 now.t = temp.t+1;
    51                 tag[nx][ny][nz] = 1;
    52                 que.push(now);
    53             }
    54         }
    55     }
    56     return 0;
    57 }
    58 int main()
    59 {
    60     freopen("caicai.txt","r",stdin);
    61     while(cin>>L>>R>>C , L&&R&&C)
    62     {
    63         memset(a,0,sizeof(a));
    64         memset(tag,0,sizeof(tag));
    65         int i,j,k;
    66         for(i = 0;i<L;i++)
    67             for(j = 0;j<R;j++)
    68             {
    69                 for(k = 0;k<C;k++)
    70                 {
    71                     cin>>a[i][j][k];
    72                     if(a[i][j][k] == 'S')
    73                        {
    74                            start.x = i;start.y = j;start.z = k;start.t = 0;
    75                            tag[i][j][k] = 1;
    76                        }
    77                 }
    78                 getchar();//一定要记得getchar;
    79             }
    80         if(!bfs())
    81             cout<<"Trapped!
    ";
    82     }
    83     return 0;
    84 }
    85 //如下也是对的(●'◡'●)
    86 
    87 //        for(i = 0;i<L;i++)
    88 //            for(j = 0;j<R;j++)
    89 //            {
    90 //                cin>>a[i][j];
    91 //                for(k = 0;k<C;k++)
    92 //                {
    93 //                    if(a[i][j][k] == 'S')
    94 //                       {
    95 //                           start.x = i;start.y = j;start.z = k;start.t = 0;
    96 //                           tag[i][j][k] = 1;
    97 //                       }
    98 //                }
    99 //            }

     

    附:字符串输入总结

    scanf不能接受空格、制表符Tab、回车等;

    而gets能够接受空格、制表符Tab和回车等;

    二者字符串接受结束后自动加''。

    (没明白看这句话:scanf("%s",str)在遇到' '(回车)或' '(空格)时输入结束,但' '(回车)或' '(空格)停留在出入缓冲区,如处理不慎会影响下面的输入;gets(str)遇到' '(回车)时输入结束,但' '(回车)已被替换为'', 存储于字符串中,输入缓冲中没有遗留的' '(回车),不会影响后续的输入。)

    使用scanf("%s",&s);函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。

    当需要输入带空格的字符串时用gets(),但是保证之前没有输入回车,如果有回车的话要getchar();

    cin>>:

    输入结束条件 :遇到Enter、Space、Tab键。

    对结束符的处理 :丢弃缓冲区中使得输入结束的结束符(Enter、Space、Tab)

    运算符>>的作用是跳过空白,读入后面的非空白字符,直到遇到另一个空白字符为止,并在串尾放一个字符

    输出:

    printf(“%s”,str)和puts(str)均是输出到''结束,遇到空格不停,但puts(str)会在结尾输出' ',printf(“%s”,str)不会换行。printf(“%s ”,str)可以替换puts(str)。

  • 相关阅读:
    持续集成
    持续集成
    持续集成
    持续集成
    持续集成
    持续集成
    CuDNNLSTM: UnknownError: Fail to find the dnn implementation
    Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
    django nginx uwsgi 502 Gateway
    ubuntu nginx 启动多个Django项目
  • 原文地址:https://www.cnblogs.com/caitian/p/5381618.html
Copyright © 2011-2022 走看看