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: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...
随机推荐
- UCloud内核热补丁技术揭秘
- Android外派(安卓外派) — 长年提供安卓开发工程师外派业务(可签合同)
北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验2.熟练掌握java技术,熟悉面向对象编程设计3.熟悉Android应用开发框架及Activit ...
- jQuery制作视频网站的展示效果
效果:如图所示,用户可以单击左上角的左右箭头,来控制视频展示的左右滚动. 当单击向右箭头时下面的展示视频会向左滚动隐藏,同时新的视频展示会以滚动方式显示出来.向左同理. css: ;; word-br ...
- java-多态性
1 多态性 主要表现在上转型对象 2 强制类型转换 2.1 基本类型的强制类型转换 转换只能在数值间进行.包括整数型.字符型.浮点型.数值类型和布尔类型间不能转换. 2.2 引用类型变量转换成其子类型 ...
- js封常用类
ajax刷 下拉框联动 /*基础初始化类*/ $.EBC = { setCourse:function(obj){ $(obj).empty(); $.get('../Index/getCoursei ...
- 免安装Oracle客户端使用PLSQL Developer 7/8 连接Oracle10/11g
众所周知,Oralce的客户端几百兆太大,网上也有许多DIR的处理.这里的处理使用官方提供ORALCE工具包Instant Client Package! 下载地址:http://www.oracle ...
- Env:Winmanager插件使用
转自:http://www.cnblogs.com/feichexia/archive/2012/11/06/vim_WinManager.html 1.准备winmanger插件,从下面网址下即可: ...
- IO - IOUtils
Commons IO is a library of utilities to assist with developing IO functionality. There are four main ...
- centos7扩展磁盘空间
[root@hn ~]# fdisk /dev/sdb The device presents a logical sector size that is smaller thanthe physic ...
- Group by与having理解
注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用 group ...