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

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

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

 

Source

 
 
 
1 #include
2 #include
3 #include
4 using namespace std; 5 #define N 26 6 int n,m; 7 char mp[N][N]; 8 int x,y; 9 int vis[N][N];10 int ans;11 int dirx[]={
0,0,-1,1};12 int diry[]={-1,1,0,0};13 void dfs(int sx,int sy){14 for(int i=0;i<4;i++){15 int xx=sx+dirx[i];16 int yy=sy+diry[i];17 if(vis[xx][yy]) continue;18 if(mp[xx][yy]=='#') continue;19 if(xx<0 || xx>=n || yy<0 || yy>=m) continue;20 vis[xx][yy]=1;21 ans++;22 dfs(xx,yy);23 }24 }25 int main()26 {27 while(scanf("%d%d",&m,&n)==2 && n+m){28 for(int i=0;i
View Code

 

转载地址:http://upifa.baihongyu.com/

你可能感兴趣的文章
在Win2003、Win7、XP中配置IIS搭建ASP.NET Web网站或应用程序(1)
查看>>
微软反垄断案新突破 Win10系统或需剥离可信计算
查看>>
拥抱开放式网络 通往下一代数据中心
查看>>
一站式金融云托管服务 都匀融通村镇银行系统成功上线运行
查看>>
红外摄像机的功率究竟有多大
查看>>
春风十里不如春城一聚:华平解决方案巡展走进昆明
查看>>
华为NFV实验室一周年:打造开放合作生态链 加速NFV产业化
查看>>
别看360完成私有化 仍有三因素阻碍中概股回归
查看>>
打开光伏广阔市场的钥匙何在?
查看>>
"爱说说"技术原理:前后台参数约定及逻辑代码(三)
查看>>
选型OpenStack,需要谨防“舍本逐末”
查看>>
程序员需要关注的十个大数据技术
查看>>
2016年成熟亚太地区公共云服务市场规模将达到81亿美元
查看>>
人工智能大幕开启 安防行业已成先头部队
查看>>
"2016中国APP分类排行榜发布暨颁奖晚宴" —— 兰亭修禊少长有王谢 黔香阁暖高见望诸公...
查看>>
2016年这些网络新贵或被并购
查看>>
Scatec Solar拟在乌克兰建设60MW光伏电站
查看>>
你真的在正确地使用WLAN控制器吗?
查看>>
阿里技术大神:你没做错啥,你错在啥都没做
查看>>
iOS开发之FMDB
查看>>