POJ1979 Red and Black (简单DFS)
POJ1979
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.
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.
'.' - 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
#include <stdio.h>
#define MAX_H 20
int map[MAX_H][MAX_H];
int visited[MAX_H][MAX_H];
int height;
int width;
int resultCount = 0;
int arr[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //right down left up
int dfs(int x, int y);
int main()
{
//freopen("input.txt","r",stdin);
int i = 0;
int j = 0;
char tempChar;
scanf("%d %d",&width,&height);
int startWidth = 0;
int startHeight = 0;
while(!(width==0 && height==0))
{
resultCount = 1;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
visited[i][j] = 0;
scanf(" %c",&tempChar);
if(tempChar=='.')
{
map[i][j] = 0;
}
else if(tempChar=='#')
{
map[i][j] = 1;
}
else if(tempChar=='@')
{
map[i][j] = 2;
startWidth = j;
startHeight = i;
}
}
}
dfs(startHeight,startWidth);
printf("%d\n",resultCount);
scanf(" %d %d",&width,&height);
}
return 0;
}
int dfs(int x, int y)
{
int i = 0;
int tempx = 0;
int tempy = 0;
for(i=0;i<4;i++)
{
tempx = x + arr[i][0];
tempy = y + arr[i][1];
if(tempx>=0 && tempx<height && tempy>=0 && tempy<width)
{
if(map[tempx][tempy]==0 && visited[tempx][tempy]==0)
{
visited[tempx][tempy] = 1;
resultCount++;
dfs(tempx,tempy);
}
}
}
return 0;
}
POJ1979 Red and Black (简单DFS)的更多相关文章
- Red and Black(简单dfs)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ 1979 Red and Black (简单dfs)
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...
- poj-1979 && hdoj - 1312 Red and Black (简单dfs)
http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...
- Poj1979 Red and Black (DFS)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 47466 Accepted: 25523 D ...
- POJ-1979 Red and Black(DFS)
题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj1979 Red And Black(DFS)
题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...
- 【POJ - 1979 】Red and Black(dfs+染色)
-->Red and Black Descriptions: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...
随机推荐
- 基于.net开发chrome核心浏览器
本文转载自:http://www.cnblogs.com/liulun/archive/2013/04/20/3031502.html 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一 ...
- php require和include区别
require的使用方法如:require("myfile.php"),这个语句通常放在PHP脚本程序的最前面.PHP程序在执行前,就会先读入require()语句所引入的文件,使 ...
- rabbitmq例子
安装 sudo apt-get install rabbitmq-server 开启后台管理 sudo rabbitmq-plugins enable rabbitmq_management;sudo ...
- Linux启动过程详解(转)
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- RMQ问题ST算法 (还需要进一步完善)
/* RMQ(Range Minimum/Maximum Query)问题: RMQ问题是求给定区间中的最值问题.当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的 ...
- 1.scala语法
对象的apply方法 (1)对象调用apply()方法,可省略成() (2)string对象的apply方法返回第n个字符 "hello"(4) //'o' if语句的返回值 ja ...
- try catch 怎么写?
除非必要,否则不在底层写try catch. 比如说,需要在catch里做一些处理,然后再抛出,一般不建议使用try catch掩盖程序出现的异常. try { BuildQueryComma ...
- JVM 类型的生命周期学习
Java虚拟机通过装载.连接和初始化一个JAVA类型,使该类型可以被正在运行的JAVA程序所使用,其中,装载就是把二进制形式的JAVA类型读入JAVA虚拟机中:而连接就是把这种读入虚拟机的二进制形式的 ...
- DG_Oracle DataGuard Switchover主备节点切换(案例)
2014-06-09 Created By BaoXinjian Thanks and Regards http://wenku.baidu.com/view/dc9f00d349649b6648d7 ...
- PLSQL_性能优化工具系列05_SQL Trace/Event 10046 Trace
2014-06-25 Created By BaoXinjian