• 广度优先搜索 BFS


    SZU  Problem(A58):City Map

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 10000MS
    • Time Limit: 10000MS
    • Judger: Number Only Judger

    Description

    You are given a map representing the layout of a city. The city consists of blocks. The first row of map represents the first row of blocks, etc. A 'B' character indicates a location where here is a bus stop. There will be exactly one 'X' character, indicating your location. All other characters will be '.'. You are also given the maximum distance you are willing to walk to a bus stop. The distance should be calculated as the number of blocks vertically plus the number of blocks horizontally. Calculate the number of bus stops that are within walking distance of your current location.

    Input

    The input will contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 100) which is the number of test cases. T test cases follow.

    Each test case contains two positive integers n(1 <= n <= 100) and m(1 <=m <=50) --the maximum distance you are willing to walk to a bus stop and the rows of the map. The next m lines describes the map, each line has a same length which is always no more than 100, and each character is 'B','X' or '.' which are described above.

    Output

    For each input test case, you are to output a single integer - the number of bus stops that are within walking distance of your current location.

    Sample Input

    2
    
    3 5
    ...B.
    .....
    ..X.B
    .....
    B....
    
    
    3 5
    ...B.
    .....
    ..X.B
    .....
    B....
    

    Sample Output

    2
    2
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 600
     4 char array[N][N]; // 用来输入的数组
     5 int flag[N][N]; // 用来标记走过的路
     6 int queuex[N*N]; // 队列用来存x
     7 int queuey[N*N]; // 队列用来存y
     8 int distance[N][N]; // 用来记录步长
     9  
    10 int dx[] = { 1, -1, 0, 0 }; // 记录x的可能变化
    11 int dy[] = { 0, 0, 1, -1 }; // 记录y的可能变化
    12  
    13 int main()
    14 {
    15     int t;
    16     scanf( "%d", &t );
    17     while( t-- )
    18     {
    19         // 将所有数组的值初始化为零
    20         memset( flag, 0, sizeof(flag) );
    21         int max;
    22         int n;
    23         scanf( "%d%d", &max, &n );
    24  
    25         /// 将那个地图输入 = = 。。
    26         int i;
    27         for( i=0; i<n; i++ )
    28             scanf( "%s", array[i] );
    29  
    30         int m = strlen( array[i-1] );
    31         int j;
    32         int a, b; // 用来记录人当前的位置。。
    33         for( i=0; i<n; i++ )
    34             for( j=0; j<m; j++ )
    35                 if( array[i][j] == 'X' )
    36                 {
    37                     a = i;
    38                     b = j;
    39                 }
    40  
    41         int p = 0, q = 0, head = 0; // 队列中表示头尾的指针~
    42         int num = 0; // 用来计算找到车站的数量
    43         i = a;
    44         j = b;
    45         queuex[p++] = a;
    46         queuey[q++] = b;
    47         distance[a][b] = 0;
    48         flag[a][b] = 1;
    49         // 进入循环找车站 = = 。。。
    50         while( head < p )
    51         {
    52             int k;
    53             int xx;
    54             int yy;
    55             for( k=0; k<4; k++ )
    56             {
    57                 xx = i+dx[k];
    58                 yy = j+dy[k];
    59                 if( xx >= 0 && yy >= 0 && xx < n && yy < m && flag[xx][yy] != 1 )
    60                 {
    61                     queuex[p++] = xx;
    62                     queuey[q++] = yy;
    63                     distance[xx][yy] = distance[i][j] + 1;
    64                     flag[xx][yy] = 1;
    65                     if( array[xx][yy] == 'B' && distance[xx][yy] <= max )
    66                         num++;
    67                 }
    68             }
    69             head++;
    70             i = queuex[head];
    71             j = queuey[head];
    72         }
    73         printf( "%d
    ", num );
    74     }
    75     return 0;
    76 }

    SZU  Problem(H70):Ali and Snoopy

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 1000MS
    • Time Limit: 1000MS
    • Judger: Number Only Judger

    Description

    阿狸被困在迷宫,Snoopy要去救他,Snoopy可以向上、下、左、右四个方向行走,每走一步(格)就要喝掉一瓶益力多。

    现在给他一个迷宫地图请问:snoopy最少需要多少瓶益力多才能找到阿狸。

    Input

    先输入一个数t,表示测试的数据个数

    下面输入的就是t个迷宫

    每个迷宫的输入都应包含以下数据

    输入迷宫的大小 n(n<=15),表示迷宫大小为n*n,接下来的n行表示迷宫。 用大写字母“S”表示snoopy的位置,用大写字母“E”表示阿狸被困的位置 用“.”表示空白,用“*”表示障碍 你知道的阿狸和snoopy都只有一个

    Output

    输出需要的最少的益力多的瓶数m (数据保证一定有最少需要的益利多的瓶数)

    Sample Input

    1
    8
    S..*....
    .*...**.
    .**.**..
    .*..*..*
    *..**.**
    ........
    .***..*.
    ....*..E
      1 #include <stdio.h>
      2 #include <string.h>
      3 #define N 18
      4 char array[N][N];  //输入数组
      5 int queuex[N*N]; // 队列数组记录x
      6 int queuey[N*N]; // 队列数组记录y
      7 int distance[N][N]; // 记录步长
      8 int flag[N][N];  // 标记走过的路
      9  
     10 int main()
     11 {
     12     int t;
     13     scanf( "%d", &t );
     14     while( t-- )
     15     {
     16         // 将所有数组的值初始化为零
     17         memset( array, 0, sizeof(array) );
     18         memset( queuex, 0, sizeof(queuex) );
     19         memset( queuey, 0, sizeof(queuey) );
     20         memset( distance, 0, sizeof(distance) );
     21         memset( flag, 0, sizeof(flag) );
     22         int n;
     23         scanf( "%d", &n );
     24         int i, j;
     25         int a, b; // 记录开始点的x, y;
     26         int c, d; // 记录结束点的x, y;
     27         int p = 0, q = 0, head = 0; // 记录队列的头指针,尾指针
     28         for( i=0; i<n; i++ )
     29             scanf( "%s", &array[i] );
     30  
     31         // 两重for找出开始点和结束点的x, y并记录下来
     32         for( i=0; i<n; i++ )
     33             for( j=0; j<n; j++ )
     34             {
     35                 if( array[i][j] == 'S' )
     36                 {
     37                     a = i;
     38                     b = j;
     39                 }
     40                 if( array[i][j] == 'E' )
     41                 {
     42                     c = i;
     43                     d = j;
     44                 }
     45             }
     46         // 然后找啊找啊ALi了 = = 。。路漫漫其远兮。。
     47         i = a;
     48         j = b;
     49         array[i][j] = array[a][b];
     50         queuex[p++] = a; // 把头头的x放进队列
     51         queuey[q++] = b; // 把头头的y放进队列
     52         distance[a][b] = 0;  // 世界的中心^^
     53         flag[a][b]=1;
     54         while( head < p )
     55         {
     56             if( 0<=i+1 && i+1<n )
     57             {
     58                 if( array[i+1][j] != '*' && flag[i+1][j] != 1 )
     59                 {
     60                     flag[i+1][j] = 1;  // 我到此一游,以此纪念
     61                     distance[i+1][j] = distance[i][j] + 1;  // 步长加1
     62                     queuex[p++] = i + 1; // 放入队列中
     63                     queuey[q++] = j; // 放入队列中
     64                 }
     65             }
     66             if( 0<=i-1 && i-1<n )
     67             {
     68                 if( array[i-1][j] != '*' && flag[i-1][j] != 1 )
     69                 {
     70                     flag[i-1][j] = 1;  // 我到此一游,以此纪念
     71                     distance[i-1][j] = distance[i][j] + 1;  // 步长加1
     72                     queuex[p++] = i - 1; // 放入队列中
     73                     queuey[q++] = j; // 放入队列中
     74                 }
     75             }
     76             if( 0<=j+1 && j+1<n )
     77             {
     78                 if( array[i][j+1] != '*' && flag[i][j+1] != 1 )
     79                 {
     80                     flag[i][j+1] = 1;  // 我到此一游!!
     81                     distance[i][j+1] = distance[i][j] + 1;  // 步长加1
     82                     queuex[p++] = i; // 放入队列中
     83                     queuey[q++] = j + 1; // 放入队列中
     84                 }
     85             }
     86             if( 0<=j-1 && j-1<n )
     87             {
     88                 if( array[i][j-1] != '*' && flag[i][j-1] != 1 )
     89                 {
     90                     flag[i][j-1] = 1;  // 标记这一格已经走过了;
     91                     distance[i][j-1] = distance[i][j] + 1;  // 步长加1
     92                     queuex[p++] = i; // 放入队列中
     93                     queuey[q++] = j - 1; // 放入队列中
     94                 }
     95             }
     96  
     97             if( queuex[head + 1] != c || queuey[head + 1] != d )
     98             {
     99                 head++;
    100                 i = queuex[head];
    101                 j = queuey[head];
    102             }
    103             else
    104                 break;
    105         }
    106         printf( "%d
    ", distance[c][d] );
    107     }
    108  
    109     return 0;
    110 }

    SZU  Problem(J37):Queue

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 1000MS
    • Time Limit: 1000MS
    • Judger: Normal

    Description

    商场中,现在有一队人正在排队。每个人都有一个属于自己唯一的编号X(0<=X<=100)。假设开始时队列为空,现在有以下三种指令: push X:编号X的人进入了这个队列(每个人只进队一次) pop:队列的第一个人离开了队列 ask:询问现在队列头的那个人是谁。

    Input

    从第一行开始的正整数n(1<=n<=100),代表指令的个数。 接下来n行,分别只会有以上的三个指令。

    Output

    对于pop指令,如果当前队列为空,则在一行里输入error,否则输出success; 对于ask指令,则在一行里输出当前队头的人的编号,如果没有输出empty。

    Sample Input

    4
    pop
    push 1
    ask
    pop
    

    Sample Output

    error
    1
    success
     1 #include <stdio.h>
     2 #include <string.h>
     3  
     4 int array[1005];
     5  
     6 int main()
     7 {
     8     int pHead = 0, pEnd = 0; // 初始化头指针和尾指针都为零;
     9     int t;
    10     scanf( "%d", &t );
    11     while( t-- )
    12     {
    13         char s[10];
    14         scanf( "%s", s );
    15         if( s[1] == 'o' )
    16         {
    17             if( pHead == pEnd )
    18                 printf( "error
    " );
    19             else
    20             {
    21                 pHead++;
    22                 printf( "success
    " );
    23             }
    24         }
    25         if( s[1] == 'u' )
    26         {
    27             int num;
    28             scanf( "%d", &num );
    29             array[pEnd++] = num;
    30         }
    31         if( s[1] == 's' )
    32         {
    33             if( pHead == pEnd )
    34                 printf( "empty
    " );
    35             else
    36                 printf("%d
    ", array[pHead] );
    37         }
    38     }
    39  
    40     return 0;
    41 }

    SZU  Problem(B56):Mother's Milk

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 10000MS
    • Time Limit: 10000MS
    • Judger: Normal

    Description

    Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.

    Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.

    Input

    The first line of input contains T(1 leq T leq 1000), the number of test cases. For each test case, a single line with the three integers A, B, and C.

    Output

    For each test case, output a single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.

    Sample Input

    2
    8 9 10
    2 5 10
    

    Sample Output

    1 2 8 9 10
    5 6 7 8 9 10

    虽然很长很长,但好开心,今天终于写出来啦哈哈哈哈啊哈~
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #define N 8080
      5 int milk[3];
      6 int queuea[N];
      7 int queueb[N];
      8 int queuec[N];
      9 int flag[25][25][25];
     10 int result[N];
     11 int cmp( const void* a, const void* b )
     12 {
     13     return *(int*)a-*(int*)b;
     14 }
     15 int main()
     16 {
     17     int t, p, q, a, b, c, head, i;
     18     scanf( "%d", &t );
     19     while( t-- )
     20     {
     21         memset( milk, 0, sizeof(milk) );
     22         memset( flag, 0, sizeof(flag) );
     23         head = q = p = 0;
     24         scanf( "%d%d%d", &a, &b, &c );
     25         milk[2] = c; // 只有c妈妈口袋里面满是吃的^^
     26         result[p++] = milk[2];
     27         queuea[q] = milk[0];
     28         queueb[q] = milk[1];
     29         queuec[q++] = milk[2];
     30         flag[milk[0]][milk[1]][milk[2]] = 1; // 这种分享方法已经试过啦;
     31         while(1)
     32         {
     33             int tempa = queuea[head];
     34             int tempb = queueb[head];
     35             int tempc = queuec[head];
     36  
     37             // c妈妈把零食分给b妈妈, c妈妈很大方,她一定要把别人的口袋装满,不然她就不乐意了^^
     38             if( tempc != 0 )
     39             {
     40                 if( tempc >= b-tempb )
     41                 {
     42                     tempc = tempc - (b-tempb); // 一定要把别人的口袋装满零食^^
     43                     tempb = b; // b妈妈的口袋满满的全是零食^^
     44                 }
     45                 else // c妈妈把零食全给别人了
     46                 {
     47                     tempb += tempc;
     48                     tempc = 0;
     49                 }
     50                 if( flag[tempa][tempb][tempc] == 0 )
     51                 {
     52                     if( tempa == 0 )
     53                         result[p++] = tempc;
     54                     queuea[q] = tempa;
     55                     queueb[q] = tempb;
     56                     queuec[q++] = tempc;
     57                     flag[tempa][tempb][tempc] = 1;
     58                 }
     59             }
     60  
     61             tempa = queuea[head];
     62             tempb = queueb[head];
     63             tempc = queuec[head];
     64             // c妈妈把零食分给a妈妈
     65             if( tempc != 0 )
     66             {
     67                 if( tempc >= a-tempa )
     68                 {
     69                     tempc = tempc - (a-tempa); // 一定要把别人的口袋装满零食^^
     70                     tempa = a; // a妈妈的口袋满满的全是零食^^
     71                 }
     72                 else // c妈妈把零食全给别人了
     73                 {
     74                     tempa += tempc;
     75                     tempc = 0;
     76                 }
     77                 if( flag[tempa][tempb][tempc] == 0 )
     78                 {
     79                     if( tempa == 0 )
     80                         result[p++] = tempc;
     81                     queuea[q] = tempa;
     82                     queueb[q] = tempb;
     83                     queuec[q++] = tempc;
     84                     flag[tempa][tempb][tempc] = 1;
     85                 }
     86             }
     87  
     88             tempa = queuea[head];
     89             tempb = queueb[head];
     90             tempc = queuec[head];
     91             // b妈妈也来分零食给a妈妈了,记住,只有把你的口袋儿装得满满的,她才开心呢~
     92             if( tempb != 0 )
     93             {
     94                 if( tempb >= a-tempa )
     95                 {
     96                     tempb = tempb - (a-tempa); // 一定要把别人的口袋装满零食^^
     97                     tempa = a; // a妈妈的口袋满满的全是零食^^
     98                 }
     99                 else // b妈妈把零食全给别人了
    100                 {
    101                     tempa += tempb;
    102                     tempb = 0;
    103                 }
    104                 if( flag[tempa][tempb][tempc] == 0 )
    105                 {
    106                     if( tempa == 0 )
    107                         result[p++] = tempc;
    108                     queuea[q] = tempa;
    109                     queueb[q] = tempb;
    110                     queuec[q++] = tempc;
    111                     flag[tempa][tempb][tempc] = 1;
    112                 }
    113             }
    114  
    115             tempa = queuea[head];
    116             tempb = queueb[head];
    117             tempc = queuec[head];
    118             // b妈妈也来分零食给c妈妈了,记住,只有把你的口袋儿装得满满的,她才开心呢~
    119             if( tempb != 0 )
    120             {
    121                 if( tempb >= c-tempc )
    122                 {
    123                     tempb = tempb - (c-tempc); // 一定要把别人的口袋装满零食^^
    124                     tempc = c; // c妈妈的口袋满满的全是零食^^
    125                 }
    126                 else // b妈妈把零食全给别人了
    127                 {
    128                     tempc += tempb;
    129                     tempb = 0;
    130                 }
    131                 if( flag[tempa][tempb][tempc] == 0 )
    132                 {
    133                     if( tempa == 0 )
    134                         result[p++] = tempc;
    135                     queuea[q] = tempa;
    136                     queueb[q] = tempb;
    137                     queuec[q++] = tempc;
    138                     flag[tempa][tempb][tempc] = 1;
    139                 }
    140             }
    141  
    142             tempa = queuea[head];
    143             tempb = queueb[head];
    144             tempc = queuec[head];
    145             // a妈妈也来分零食给b妈妈了,记住,只有把你的口袋儿装得满满的,她才开心呢~
    146             if( tempa != 0 )
    147             {
    148                 if( tempa >= b-tempb )
    149                 {
    150                     tempa = tempa - (b-tempb); // 一定要把别人的口袋装满零食^^
    151                     tempb = b; // b妈妈的口袋满满的全是零食^^
    152                 }
    153                 else // a妈妈把零食全给别人了
    154                 {
    155                     tempb += tempa;
    156                     tempa = 0;
    157                 }
    158                 if( flag[tempa][tempb][tempc] == 0 )
    159                 {
    160                     if( tempa == 0 )
    161                         result[p++] = tempc;
    162                     queuea[q] = tempa;
    163                     queueb[q] = tempb;
    164                     queuec[q++] = tempc;
    165                     flag[tempa][tempb][tempc] = 1;
    166                 }
    167             }
    168  
    169             tempa = queuea[head];
    170             tempb = queueb[head];
    171             tempc = queuec[head];
    172             // a妈妈也来分零食给c妈妈了,记住,只有把你的口袋儿装得满满的,她才开心呢~
    173             if( tempa != 0 )
    174             {
    175                 if( tempa >= c-tempc )
    176                 {
    177                     tempa = tempa - (c-tempc); // 一定要把别人的口袋装满零食^^
    178                     tempc = c; // c妈妈的口袋满满的全是零食^^
    179                 }
    180                 else // a妈妈把零食全给别人了
    181                 {
    182                     tempc += tempa;
    183                     tempa = 0;
    184                 }
    185                 if( flag[tempa][tempb][tempc] == 0 )
    186                 {
    187                     if( tempa == 0 )
    188                         result[p++] = tempc;
    189                     queuea[q] = tempa;
    190                     queueb[q] = tempb;
    191                     queuec[q++] = tempc;
    192                     flag[tempa][tempb][tempc] = 1;
    193                 }
    194             }
    195  
    196             if( head+1 < q )
    197                 head++;
    198             else
    199                 break;
    200         }
    201         qsort( result, p, sizeof(int), cmp );
    202         for( i=0; i<p-1; i++ )
    203             printf( "%d ", result[i] );
    204         printf( "%d
    ", result[i] );
    205     }
    206     return 0;
    207 }
     
  • 相关阅读:
    MySQL优化点
    MySQL中lock tables和unlock tables浅析
    在深度计算框架MindSpore中如何对不持续的计算进行处理——对数据集进行一定epoch数量的训练后,进行其他工作处理,再返回来接着进行一定epoch数量的训练——单步计算
    Attributes should be specified via @SpringBootApplication
    base-package的路径不对,导致@Autowire提示trainingRepository错误。
    测试框架之-断言与预期结果 AssertJ
    无效的目标版本8 和 Unsupported major.minor version 52
    UML类图中箭头和线条的含义和用法
    苏宁乔新亮:世界上最好的研发管理十条经验
    4-8 路由实战
  • 原文地址:https://www.cnblogs.com/zhongshuxin/p/3268953.html
Copyright © 2020-2023  润新知