POJ 1979:Red and Black
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 26058 | Accepted: 14139 | 
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
给一张图,@为起始点,'.'能走,‘#’不能走,问一共能走到多少'.'。
在深夜能做到这种水题也真是很令人高兴的事情。
深度搜索水题。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int row,col,sum;
char value[30][30];
int flag[30][30]; void dfs(int x,int y)
{
flag[x][y]=1; if(x>1&&flag[x-1][y]==0&&value[x-1][y]=='.')
{
dfs(x-1,y);
}
if(y>1&&flag[x][y-1]==0&&value[x][y-1]=='.')
{
dfs(x,y-1);
}
if(x<row&&flag[x+1][y]==0&&value[x+1][y]=='.')
{
dfs(x+1,y);
}
if(y<col&&flag[x][y+1]==0&&value[x][y+1]=='.')
{
dfs(x,y+1);
}
} void solve1()
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(value[i][j]=='@')
{
dfs(i,j);
return;
}
}
}
} void solve2()
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(flag[i][j])
{
sum++;
}
}
}
} int main()
{
int i,j;
while(cin>>col>>row)
{
if(col+row==0)
break;
memset(flag,0,sizeof(flag));
sum=0;
for(i=1;i<=row;i++)
{
cin>>value[i]+1;
}
solve1();
solve2(); cout<<sum<<endl;
} return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1979:Red and Black的更多相关文章
- 【POJ - 1979 】Red and Black(dfs+染色)
		-->Red and Black Descriptions: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ... 
- POJ 1979 Red and Black (红与黑)
		POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ... 
- 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器
		原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ... 
- POJ 3321:Apple Tree  +  HDU 3887:Counting Offspring(DFS序+树状数组)
		http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ... 
- POJ 3252:Round Numbers
		POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ... 
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
		1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ... 
- poj 1979 Red and Black(dfs)
		题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ... 
- POJ 1979 Red and Black (zoj 2165) DFS
		传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ... 
- poj  1979 Red and Black 题解《挑战程序设计竞赛》
		地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ... 
随机推荐
- Java For 循环
			章节 Java 基础 Java 简介 Java 环境搭建 Java 基本语法 Java 注释 Java 变量 Java 数据类型 Java 字符串 Java 类型转换 Java 运算符 Java 字符 ... 
- scala文件通过本地命令运行
			1.准备(检查) a.本地环境安装jdk b.安装scala 2.sublime编辑scala文件,并存放到F:\plan_next\scala_compile下 3.文件目录中切换到cmd中(文件目 ... 
- ExcelPackage导入导出,命名空间一定要是EPPlus
			1.引入EPPlus.dll,旧版的是OfficeOpenXml.dll,最好使用EPPlus2.调用 string path = UploadExecl(batchUpload.BinaryExce ... 
- python基础(变量,字符串,列表,元组)
			#列表的操作list1 = ['wuqiang','lichang','changhao'] #列表的定义print(list1) #操作列表print(list1[-1]) #操作列表的最后一位li ... 
- 十五、SAP自定义结构体
			一.SAP的结构体是以BEGIN OF开始,以END OF结尾,代码如下: 二.输出结果如下 
- Linux基础操作及概念
			Linux基础操作及概念 终端的概念 通常我们在使用 Linux 时,并不是直接与系统打交道,而是通过一个叫做 Shell 的中间程序来完成的,在图形界面下为了实现让我们在一个窗口中完成用户输入和显示 ... 
- UVA - 10539 Almost Prime Numbers (几乎是素数)
			题意:输入两个正整数L.U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子. 分析: 1.满足条件的数是素数的倍数. 2.枚举所有的素数, ... 
- UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)
			题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小.输出最小的和. 分析: 1.将n分解为a1p1*a2p2……,每个aipi作为一个单独 ... 
- jQuery文档加载事件
			$(document).ready(handler) $().ready(handler) (this is not recommended) $(handler) 相当于: $(document). ... 
- realme发布会将于本周三在北京·幻艺术举行
			5月13日消息,realme宣布将于5月15日(本周三)在北京·幻艺术中心发布realme X. 根据官方公布的海报,realme X采用了升降全面屏方案,无挖孔.无刘海.无水滴,支持屏幕指纹识别,正 ... 
