HDU1312——Red and Black

Red and Black

Problem 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)
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).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13

题目大意:

    一个瓦片地图,‘.‘代表黑色的瓦片,‘#‘代表红色的瓦片,‘#‘是主人公站的位置,主人公只会下上左右四种移动方式,且只能去黑色的瓦片(初始位置也是黑色的瓦片);

    求可以去的黑色瓦片个数,包括初始位置。

结题思路:

    简单的DFS,搜索一下与初始位置上下左右相连的所有黑色瓦片,并记录输出即可。

Code;

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #define MAXN 50
 5 using namespace std;
 6 bool vis[MAXN+10][MAXN+10],is_black[MAXN+10][MAXN+10]; //黑色瓦片标记
 7 char tile[MAXN+10][MAXN+10];
 8 int n,m;
 9 int dfs(int i,int j)
10 {
11
12     if (is_black[i][j]==0||vis[i][j]==1) return 0; //搜索时遇到已经搜索过的或者红色瓦片则返回,不记录瓦片数。
13     vis[i][j]=1;
14     int sum=1;
15     if (i-1>=1) sum+=dfs(i-1,j); //搜索上下左右四种情况
16     if (i+1<=n) sum+=dfs(i+1,j);
17     if (j-1>=1) sum+=dfs(i,j-1);
18     if (j+1<=m) sum+=dfs(i,j+1);
19     return sum;
20 }
21 int main()
22 {
23     int first_i,first_j;
24     while (cin>>m>>n)
25     {
26         if (m==0&&n==0) break;
27         memset(is_black,0,sizeof(is_black));
28         memset(vis,0,sizeof(vis));
29         getchar();
30         for (int i=1; i<=n; i++)
31         {
32             for (int j=1; j<=m; j++)
33             {
34                 cin>>tile[i][j];
35                 if (tile[i][j]==‘@‘) first_i=i,first_j=j,tile[i][j]=‘.‘; //记录初始位置用于调用DFS,并用题意将初始位置转换成黑色瓦片(貌似没有必要--!)
36                 if (tile[i][j]==‘#‘) is_black[i][j]=0;//用Is_Black数组标记瓦片颜色
37                 else is_black[i][j]=1;
38             }
39             getchar();
40         }
41
42         printf("%d\n",dfs(first_i,first_j));
43     }
44     return 0;
45 }

HDU1312——Red and Black

时间: 2024-10-04 09:14:36

HDU1312——Red and Black的相关文章

HDU1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9732    Accepted Submission(s): 6060 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU1312 Red and Black 题解

一条递归搜索法题目,使用递归搜索法,但是实际不用重复计算方格. 思路是: 1 每搜索一个方格就改变当前方格的值为 '*',或者任何其他非'.'的值,代表该方格已经走过了 2 递归的时候不回复这个方格的值,就实际上不用重复搜索这个方格了,故此不用回溯 #include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; int R, C,

《算法竞赛从入门到进阶》第四章 搜索技术 hdu1312 &quot;Red and Black&quot; BFS

#include<iostream> #include<cstdio> #include<queue> using namespace std; char room[25][25]; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; int Wx,Hy,num; //Wx,Hy:长宽的边界 ,num:从初始瓷砖到能到达的瓷砖的总数 #define CHECK(x,y) (x<Wx&&x>=0&&am

小试牛刀-搜索基础入门(杭电五题)

hdu 1241 Oil Deposits 水题,BFS,判断区域的块数. 代码清单: #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef pair<int,int>P; int m,n; char s[105][105]; int xy[8][2]=

HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

题目链接:HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9902    Accepted Submission(s): 6158 Problem Description There is a rectangular

hdu1312(Red and Black)

点击打开杭电1312 Problem 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

HDU1312:Red and Black(DFS)

题目描述 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 blac

HDU1312:Red and Black

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

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版