• hdu1312 Red and Black(最基础的搜索题)


    这个题就是找和'@'相连通的'.'的个数再加1。

    深搜和广搜都可以,只不过貌似广搜更省时间。

    我的是用广搜写的:

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 char map[21][21];
     6 int h,w,sum;
     7 struct node
     8 {
     9     int x;
    10     int y;
    11 };
    12 struct node start;
    13 void make_map()
    14 {
    15     for(int i=0;i<h;i++)
    16     {
    17         for(int j=0;j<w;)
    18         {
    19             scanf("%c",&map[i][j]);
    20             if(map[i][j]=='@')
    21             {
    22                 start.x=i;
    23                 start.y=j;
    24                 map[i][j]='#';
    25             }
    26             j++;
    27         }
    28         getchar();
    29     }
    30 }
    31 void bfs()
    32 {
    33     int i;
    34     int help[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    35     queue<node>myqueue;
    36     myqueue.push(start);
    37     struct node q1,q2;
    38     while(!myqueue.empty())
    39     {
    40         q1=myqueue.front();
    41         myqueue.pop();
    42         for(i=0;i<4;i++)
    43         {
    44             q2.x=q1.x+help[i][0];
    45             q2.y=q1.y+help[i][1];
    46             if(map[q2.x][q2.y]=='.'&&q2.x<h&&q2.x>=0&&q2.y>=0&&q2.y<w)
    47             {
    48                 map[q2.x][q2.y]='#';
    49                 sum++;
    50                 myqueue.push(q2);
    51 
    52             }
    53         }
    54     }
    55     printf("%d\n",sum);
    56 }
    57 int main()
    58 {
    59     while(scanf("%d%d",&w,&h)!=EOF&&(w||h))
    60     {
    61         getchar();
    62         sum=1;
    63         make_map();
    64         bfs();
    65     }
    66     return 0;
    67 }

    注意里面在输入过程中的一些\n要用getchar()吃掉,杯具的有一个没吃掉找了20分钟!!!!!!!

    作者: 点A点C

    出处: http://www.cnblogs.com/ACshasow/>

    关于作者:游戏开发、算法研究,请多多赐教!

    本文版权归作者(点A点C)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(572779130@qq.com)咨询.

  • 相关阅读:
    CentOS新系统必做的几件事
    CentOS基础指令备忘
    有向图的强连通分量
    模仿c的字符转整数函数 atoi
    N个骰子的点数和的概率分布
    c语言算法题目求职用
    copy指定目录下包括子目录中所有的文件
    js的页面交互
    js的数据类型、函数、流程控制及变量的四种声明方式
    z-index
  • 原文地址:https://www.cnblogs.com/ACshasow/p/2764136.html
Copyright © 2020-2023  润新知