博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Red and Black(poj-1979)
阅读量:5107 次
发布时间:2019-06-13

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

原题目:

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).

Sample Input

6 9....#......#..............................#@...#.#..#.11 9.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0

Sample Output

4559613 题目大意: 给出一个w列和h行的方阵,方阵中有'.','#'和'@'。其中'.'是可以移动的地方,'#'是红色的砖,是不可以移动的地方,'@'是最初人的地点就(图的入口),人可以上下左右移动,以0 0 为结束。 要求输出人可以移动的砖的个数。该题利用搜索。 注意: 先输入w,然后输入h,但是w是列数,h是行数。 代码:
#include
#include
using namespace std; void dfs(int x,int y); int w,h,tmp=0; char ma[100][100]; int dx[4]={ 0,0,1,-1}; int dy[4]={ 1,-1,0,0}; int main(){ while(cin>>h>>w){ if(w==0&&h==0) break; memset(ma,'0',sizeof(ma)); int ax,ay; for(int i=0;i
>ma[i][j]; if(ma[i][j]=='@'){ ax=i; ay=j; } } } tmp=0; dfs(ax,ay); cout<
<
=0&&nx
=0&&ny
Select Code
#include
#include
using namespace std; void dfs(int x,int y); int w,h,tmp=0; char ma[100][100]; int dx[4]={ 0,0,1,-1}; int dy[4]={ 1,-1,0,0}; int main(){ while(cin>>h>>w){ if(w==0&&h==0) break; memset(ma,'0',sizeof(ma)); int ax,ay; for(int i=0;i
>ma[i][j]; if(ma[i][j]=='@'){ ax=i; ay=j; } } } tmp=0; dfs(ax,ay); cout<
<
=0&&nx
=0&&ny
 

转载于:https://www.cnblogs.com/sunowsir/p/7142187.html

你可能感兴趣的文章
Linux操作系统 和 Windows操作系统 的区别
查看>>
《QQ欢乐斗地主》山寨版
查看>>
文件流的使用以及序列化和反序列化的方法使用
查看>>
Android-多线程AsyncTask
查看>>
第一个Spring冲刺周期团队进展报告
查看>>
C++函数基础知识
查看>>
红黑树 c++ 实现
查看>>
Android 获取网络链接类型
查看>>
linux中启动与终止lnmp的脚本
查看>>
gdb中信号的处理[转]
查看>>
LeetCode【709. 转换成小写字母】
查看>>
如何在Access2007中使用日期类型查询数据
查看>>
Jzoj4757 树上摩托
查看>>
CF992E Nastya and King-Shamans(线段树二分+思维)
查看>>
oracle 几个时间函数探究
查看>>
第一个Java Web程序
查看>>
树状数组_一维
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
linux install ftp server
查看>>
嵌入式软件设计第8次实验报告
查看>>