看了好多天的DFS,基本上的程序都是要借助解题报告才可以实现
但是这一题,确实依照自己的思路,完全靠自己写出来的,或许比较简单吧,所以特此发表下文章,犒劳一下自己,再接再厉
http://acm.hdu.edu.cn/showproblem.php?pid=1312
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2364 Accepted Submission(s): 1551
Problem DescriptionThere 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.
InputThe 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)
OutputFor 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 Input6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output45 59 6 13#include<iostream> #include<string.h> #include<stdio.h> using namespace std; int direct[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 }; /*定义方向, 左右,上下*/ char map[21][21]; /*输入的字符串*/ bool mark[21][21]; /*标记走过的路程*/ bool flag; int W, H; int Dx, Dy; //记录起始位置@,从这里开始进行搜索 int ans; //记录满足的个数。初始化为1,因为@也包含在内 /*****底下是核心算法,主要是从 上下左右这四个方向进行搜索,注意 满足搜索的条件是不能越界,不能是#, 还有就是没有搜索过的--》主要是靠mark[i][j] 来实现*******/ void DFS( int x, int y ) { mark[x][y] = true; for( int k = 0; k < 4; k ++ ) { int p = x + direct[k][0]; int q = y + direct[k][1]; if( p >= 0 && q >= 0 && p < H && q < W && !mark[p][q] && map[p][q] != '#' ) { ans ++; DFS( p, q ); } } return; } int main() { int i, j, k; while( cin >> W >> H && ( W || H ) ) // W -> column, H -> row; { memset( mark, false, sizeof( mark ) ); for( i = 0; i < H; i ++ ) for( j = 0; j < W; j ++ ) { cin >> map[i][j]; if( map[i][j] == '@' ) { Dx = i; Dy = j; } } ans = 1; DFS( Dx, Dy ); cout << ans << endl; } }
相关推荐
HDU1312 Red and Black(黑红砖块)(dfs入门水题)
题目链接:点击打开链接 题目大意:有一个长方形的房间,铺满了正方形瓷砖。每个瓷砖都是红色或黑色的。一个人站在一块黑色的瓷砖上。从一个瓷砖上,他可以移动到四个相邻(上下左右)的瓷砖中的一个。但是他不能移动到红色的瓷砖,只能在黑色的瓷砖上移动。通过重复上面描述的动作,编写一个程序来计算他能达到的黑瓷砖的数量。 输入:多个数据。第一行给出两个数m,n(0,0代表结束输入);m代表列,n代表行。m,n...
hdu1312 “Red and Black”——DFS与递归的应用
Red and Black 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 b
Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312
DFS不解释。。 #include #include #include #include #define N 50 using namespace std; int n,m; char map[N][N]; int sum; void dfs(int x,int y) { if(map[x+1][y]=='.') {sum++;map[x+1][y]='#';dfs(x
HDU 1312:Red and Black (广度优先搜索)
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1312问题和上一篇博客一样,都是图的搜索问题上一篇博客采用深度优先搜索,这次采用广度优先搜索对于这个问题两种搜索算法都能AC,时间空间开销也差不多,实现上也差不多,就是把数据结构改一改,point类的核心算法代码改一改1312: Red and BlackTime Limit: 2000/1000 MS...
HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)
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
hdu 1312 Red and Black
hdu 1312 Red and Black 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 DFS水 题目大意:一个人被困在一个迷宫里,问他的生存空间。 题目分析:DFS就好了。 code: #include #include char mp[26][26],vis[26][26]; int m
HDU T1312 Red and Black
HDU T1312 Red and Black 题解: 因为是遍历所有可以走的点,所以不需要回溯。需要注意的是写完要测试最小值 例如:1 1 @ 答案应该为1,本身也算一个。 Dfs代码 #include<cstdio> #include<iostrea...
Hdu 1312 Red and black(BFS)
HDU 1312 Red and black#include<iostream> #include<cstring> #include<algorithm> #include<queue> using namespace std; char map[25][25]; int flag[25][25];//标记 已经走过的路 int step[4][2] = { { 1, 0 }, { 0, 1 },
HDU-1312 Red and Black
HDU 1312 Red and Black 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...
hdu1312 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 adjacen
HDU1312——Red and Black
http://acm.hdu.edu.cn/showproblem.php?pid=1312 这倒题用的是广搜:广搜用的是队列。就是从一个点有四个方向,所以把四个可能都放进队列里。 广搜也是状态改变。就像是泼水的感觉。 题意:就是从@位置最多能走多少步。明显的广搜 #include<stdio.h> #include<cstring> #inclu...
HDU 1312 Red and Black【递归】
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13111 Accepted Submission(s): 8127 Problem Description There is a re
hdu 1312 Red And Black
hdu 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
hdu 1312 Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29940Accepted Submission(s): 18118 Problem Description There is a rectan...
hdu_1312 Red and Black
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 分析:简单的BFS。使用广搜,使每个满足添加的点进队列,统计删除的点,有多少个点被删除就有多少个满足题意的点。 我的代码: #include #include #include using namespace std; char map[21][21]; bool visited[21][
HDU 1312 Red and Black <BFS>
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2364 Accepted Submission(s): 1551 Problem Description There is a recta
hdu 1312 Red and Black
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312点击打开链接 Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11962 Accepte
1303




被折叠的 条评论
为什么被折叠?



