博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
广度优先搜索 BFS
阅读量:6706 次
发布时间:2019-06-25

本文共 13399 字,大约阅读时间需要 44 分钟。

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

23 5...B........X.B.....B....3 5...B........X.B.....B....

Sample Output

22
1 #include 
2 #include
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
= 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\n", 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

18S..*.....*...**..**.**...*..*..**..**.**.........***..*.....*..E
1 #include 
2 #include
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

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

4poppush 1askpop

Sample Output

error1success
1 #include 
2 #include
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\n" );19 else20 {21 pHead++;22 printf( "success\n" );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\n" );35 else36 printf("%d\n", 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

28 9 102 5 10

Sample Output

1 2 8 9 105 6 7 8 9 10 虽然很长很长,但好开心,今天终于写出来啦哈哈哈哈啊哈~
1 #include 
2 #include
3 #include
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 else199 break;200 }201 qsort( result, p, sizeof(int), cmp );202 for( i=0; i

 

 

转载于:https://www.cnblogs.com/zhongshuxin/p/3268953.html

你可能感兴趣的文章
Java使用ScriptEngine(javax.script)
查看>>
Nhibernate中 Many-To-One 中lazy="proxy" 延迟不起作用的原因
查看>>
svn权限设置
查看>>
MVC验证11-对复杂类型使用jQuery异步验证
查看>>
C++static关键字用法
查看>>
指尖下的js ——多触式web前端开发之一:对于Touch的处理(转)
查看>>
visual studio 2013使用技巧
查看>>
Sublime Text 相关
查看>>
深入理解css优先级
查看>>
Android MediaPlayer状态机
查看>>
Material Design Animation
查看>>
ASP.NET MVC搭建项目后台UI框架—3、面板折叠和展开
查看>>
(C语言)memcpy函数原型的实现
查看>>
Theano2.1.1-基础知识之准备工作
查看>>
DevExpress.Build
查看>>
ACCESS-如何多数据库查询(跨库查询)
查看>>
java并发编程学习:用 Semaphore (信号量)控制并发资源
查看>>
HDU 2070 Fibbonacci Number
查看>>
Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-级别选择ScrollView
查看>>
Win10系统菜单打不开问题的解决,难道是Win10的一个Bug ?
查看>>