ZOJ2165 Red and Black(dfs)
来源:互联网 发布:阿里php招聘 编辑:程序博客网 时间:2024/06/06 21:40
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 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.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0
Sample Output
4559613
翻译:
题目编号:ZOJ2165
详见:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2165
1.2题目描述:
有一个长方形的房间,房间里的地面上布满了正方形的瓷砖,瓷砖要么是红色,要么是黑色。一男子站在其中一块黑色的瓷砖上。男子可以向他四周的瓷砖上移动,但不能移动到红色的瓷砖上,只能在黑色的瓷砖上移动。
本题的目的就是要编写程序,计算他在这个房间里可以到达的黑色瓷砖的数量。
1.3输入描述
输入文件中包含多个测试数据。每个测试数据的第1 行为两个整数W和H,分别表示长方形房间里x方向和y方向上瓷砖的数目。W和H的值不超过20。接下来有H 行,每行有W个字符,每个字符代表了瓷砖的颜色,这些字符的取值及含义为:
1) ‘.’ - 黑色的瓷砖;
2) ‘#’ - 红色的瓷砖;
3) ‘@’ - 表示该位置为黑色瓷砖,且一名男子站在上面,注意每个测试数据中只有一个
‘@’符号。
输入文件中最后一行为两个0,代表输入文件结束。
1.4 输出描述
对输入文件中每个测试数据,输出占一行,为该男子从初始位置出发可以到达的黑色瓷砖的数目(包括他初始时所处的黑色瓷砖)
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>#include<cstdlib>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int maxn = 21;bool vis[maxn][maxn]; //标记是否走过int m, n;char oil[maxn][maxn];int dir[4][2] = {1,0, 0,1, 0,-1, -1,0}; //记录该点周围的方向void dfs(int x, int y){ vis[x][y] = 0; int i; int k, g; for(i=0; i<4; i++){ k = x + dir[i][0]; g = y + dir[i][1]; //printf("%c %d %d %d \n",oil[k][g],vis[k][g],k,g); if(oil[k][g]=='.' && vis[k][g]!=0 && k>=0 && k< n && g>=0 && g<m){ // cout<<k<<" "<<g<<endl; dfs(k, g); } }}int main(){ int i, j, sum, e, t; while(scanf("%d%d", &m, &n)!=EOF&& m+n) { sum = 0; memset(vis, 1, sizeof(vis)); memset(oil, 0, sizeof(oil)); for(i=0; i<n; i++){ for(j=0; j<m; j++){ cin >> oil[i][j]; if(oil[i][j]=='@'){ t=i; e=j; } } } dfs(t, e); for(i=0; i<n; i++){ for(j=0; j<m; j++){ if(vis[i][j]==0){ sum++; } } } cout << sum << endl; } return 0;}
- ZOJ2165 Red and Black(dfs)
- HDU1312 ZOJ2165 Red and Black
- Red and Black(DFS)
- Red and Black(DFS)
- Red and Black(DFS)
- dfs Red and Black
- 【DFS】RED AND BLACK
- Red and Black(DFS)
- HIT-Red and Black DFS
- poj1979 Red and Black dfs
- HDU1312:Red and Black(DFS)
- hdu1312(DFS Red and Black )
- HDU1312:Red and Black(DFS)
- 【搜索-DFS】Red and Black
- Red and Black(DFS)
- poj1979 Red and Black(dfs)
- HDU1312 Red and Black DFS
- B - Red and Black+dfs
- 文件类型与权限
- shell排序法-改良的插入排序(C)
- POJ
- sorted
- 通讯录草稿
- ZOJ2165 Red and Black(dfs)
- Android:ListView之ViewHolder
- Android之:了解MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST)
- 关于SSH整合的那些事
- 按位操作的应用
- 总结-2017/11/24
- 近期 wnTKYg Linux 恶意软件简介
- 解析xml的4种方法详解
- c\c++ 指针,地址小理解 (一)