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: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...
随机推荐
- SVN分支的合并和同步
使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及到分支的管理,也不敢贸然使用合并功能,生怕合并出了问题对团队造成不良影响,最主要的原因是,自己对分支的目的和合并 ...
- 互联网 DBA 需要做那些事(转)
众所周知,互联网DBA与传统行业DBA有很大的不同,那就是管理的机器多,新技术更新快,面对的开发多.网络环境复杂.要求7*24待机:这样就 导致互联网DBA的工作在传统DBA工作之上,增加了更多的复杂 ...
- Java 新IO
NIO提供全新的底层I/O模型.与最初的java.io包中面向流(stream-oriented)概念不同,NIO采用了面向块的概念(block-oriented).在尽可能的情况下,I/O的操 ...
- sublime运行c++快捷建修改
打开preferences->key bingings -user 输入 [ {"keys": ["f9"], "command": ...
- BestCoder Round #85 hdu5776 sum
sum 题意: 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T,表示数据组数. 接下去有T组数据,每组数据的第一行有两个 ...
- cf666 C. Codeword 组合数学 离线分块思想
time limit per test 6 seconds memory limit per test 256 megabytes input standard i ...
- [MySQL] 常用SQL技巧--18.5
1.正则表达式使用 MySQl利用REGEXP命令,提供正则表达式功能. 例子:select 'abcdef' REGEXP '^a'; select 'efg' REGEXP '[^XYZ]'; 2 ...
- 转__Android Studio ,基于intellij idea
看到论坛里一些关于Android Studio的帖子,基本上是停留在使用教程上.在此做一些功能性的分析和测评 下载地址 :http://developer.android.com/index.html ...
- JAVA 网格布局管理器
//网格布局管理器 import java.awt.*; import javax.swing.*; public class Jiemian3 extends JFrame{ //定义组件 JBut ...
- CSS媒体查询,CSS根据不同的分辨率显示不同的样式
在写自适应网页的时候,我们需要网页有几种显示方式,我们可以用CSS实现这个功能 使用CSS提供的媒体查询,我们可以根据屏幕分辨率来使用相应的CSS样式 @media screen and (max-w ...