挑战程序2.1.4 穷竭搜索>>深度优先搜索 练习题 POJ1979黑与红

http://poj.org/problem?id=1979

Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can‘t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.‘ - a black tile 
‘#‘ - a red tile 
‘@‘ - a man on a black tile(appears exactly once in a data set) 
The end of the input is indicated by a line consisting of two zeros.

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

思路:(⊙v⊙)嗯和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者真值什么的),最后遍历一遍地图就好了。

 1 #include <stdio.h>
 2 char ch[24][24];
 3 void solve(int H,int W);
 4 void find1(int H,int W);
 5 void dfs(int x,int y,int H,int W);
 6 int main()
 7 {
 8     int H,W,i;
 9     while(scanf("%d%d",&W,&H)!=EOF&&H!=0&&W!=0)
10     {
11         getchar();
12      for(i=0;i<H;i++)
13         gets(ch[i]);
14      find1(H,W);
15      solve(H,W);
16     }
17     return 0;
18 }
19
20 void solve(int H,int W)
21 {
22     int a=0;
23     for(int i=0;i<H;i++)
24         for(int k=0;k<W;k++)
25             if(ch[i][k]==‘@‘)
26                 a++;
27      printf("%d\n",a);
28 }
29
30 void find1(int H,int W)
31 {
32     for(int i=0;i<H;i++)
33         for(int k=0;k<W;k++)
34             if(ch[i][k]==‘@‘)
35                 {
36                     dfs(i,k,H,W);
37                     return;
38                 }
39 }
40
41 void dfs(int x,int y,int H,int W)
42 {
43     int x1,y1,nx,ny;
44     ch[x][y]=‘@‘;
45     for(x1=-1;x1<=1;x1++)
46     {
47         nx=x+x1;
48             if(nx>=0&&nx<H&&ch[nx][y]!=‘#‘&&ch[nx][y]!=‘@‘)
49                 dfs(nx,y,H,W);
50     }
51     for(y1=-1;y1<=1;y1++)
52     {
53         ny=y+y1;
54             if(ny>=0&&ny<W&&ch[x][ny]!=‘#‘&&ch[x][ny]!=‘@‘)
55                 dfs(x,ny,H,W);
56     }
57     return;
58 }
时间: 2024-07-30 13:40:02

挑战程序2.1.4 穷竭搜索>>深度优先搜索 练习题 POJ1979黑与红的相关文章

挑战程序2.1.4 穷竭搜索&gt;&gt;深度优先搜索

{'W','.','.','W','.'}, {'W','.','.','.','.'}, {'.','.','.','W','.'}, {'W','.','.','.','.'}, {'W','.','W','W','W'}例子,此处有5处积水 题目:有N*M格子的花园,W表示积水,'.'表示干地,积水附近8个区域只要有积水,那么这两处连通,并算作是同一处积水,问一共有几处积水? 思路:穷竭搜索,O(N*M),搜索到积水则变成'.',并搜索连通的积水将其变成'.',直到这块积水搜索结束,此时a

挑战程序2.1.5 穷竭搜索&gt;&gt;宽度优先搜索(练POJ3669)

先对比一下DFS和BFS         深度优先搜索DFS                                   宽度优先搜索BFS 明显可以看出搜索顺序不同. DFS是搜索单条路径到底部,再回溯. BFS是搜索近的状态,直到底部,一般在求解最短路径或者最短步数上应用. BFS要用到队列呢.. 队列的用法看一看http://blog.csdn.net/cindywry/article/details/51919282 练习题系列--------------------- 题目:p

POJ 2196 Computer(搜索-深度优先搜索)

Computer Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are

搜索——深度优先搜索(DFS)

设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个岔路前方是一条死路,就退回到这个岔道口,选择另一个岔路前进.如果岔路口存在新的岔道口,那么仍然按上面的方法枚举新岔道口的每一条岔道.这样,只要迷宫存在出口,那么这个方法一定能够找到它. 也就是说,当碰到岔道口时,总是以"深度"作为前进的关键词,不碰到死胡同就不回头,因此这种搜索的方式称为深

人员分配问题(简单的深度优先搜索题目)

问题描述 某单位有若干人员,现执行某任务需要一定人数人员.编写程序,输入单位人数和每位员工名字,再输入执行任务所需人数,输出所有可能派出人员方案 程序代码 #include<bits/stdc++.h> using namespace std; char s[10][20]; //存放读进来的人的名字,暂定人最多为10个 char ans[10][20]; //存放每一次生成的结果 int maxMan = 0; //存放执行任务所需人数 int times = 0; //存放总方案数 int

算法-无向图(深度优先搜索和广度优先搜索)

图中最常用到的两种搜索深度优先搜索和广度优先搜索,深度优先搜索是一种在开发爬虫早期使用较多的方法它的目的是要达到被搜索结构的叶结点(即那些不包含任何超链接的Html文件) ,广度搜索属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果.换句话说,它并不考虑结果的可能位置,彻底地搜索整张图,直到找到结果为止. 深度优先搜索 图中我们经常会遇到一个问题就是图的连通性,比如说从一个顶点到另外一个顶点,判断顶点和其他顶点之间的连通性,以下图为例: 搜索API定义: @interface

【图论】广度优先搜索和深度优先搜索

写在最前面的 这篇文章并没有非常详细的算法证明过程.导论里面有非常详细的证明过程.本文只阐述“广度优先和深度优先搜索的思路以及一些简单应用”. 两种图的遍历算法在其他图的算法当中都有应用,并且是基本的图论算法. 广度优先搜索 广度优先搜索(BFS),可以被形象的描述为“浅尝辄止”,具体一点就是每个顶点只访问它的邻接节点(如果它的邻接节点没有被访问)并且记录这个邻接节点,当访问完它的邻接节点之后就结束这个顶点的访问. 广度优先用到了“先进先出”队列,通过这个队列来存储第一次发现的节点,以便下一次的

POJ 2718 Smallest Difference (穷竭搜索)

http://poj.org/problem?id=2718 将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0.求这两个数之差的最小值. 我使用了搜索并且避免了递归,自认为是比较好的算法. 一开始我想到的是枚举,并且很快给出了实现: #ifndef ONLINE_JUDGE #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <al

POJ 3050 Hopscotch (穷竭搜索)

题目链接:http://poj.org/problem?id=3050 题面: Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2563   Accepted: 1823 Description The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered