11.17 dfs poj1979 Red and Black

---恢复内容开始---

https://www.cnblogs.com/OctoptusLian/p/7429645.html

解救小哈——dfs入门

//果然博客园质量高丫

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};
int ans,n,m;
char a[25][25];
void dfs(int x,int y)
{
int i,rx,ry;
for(i=0; i<4; i++)
{
rx=x+dx[i];
ry=y+dy[i];
if(rx<0||rx>=m||ry<0||ry>=n)
continue;
if(a[rx][ry]==‘.‘)
{
ans++;
a[rx][ry]=‘#‘;
dfs(rx,ry);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n||m)
{
int i,j;
ans=0;
getchar();
for(i=0; i<m; i++)
{
gets(a[i]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(a[i][j]==‘@‘)
{
dfs(i,j);
ans++;
break;
}
}
}
printf("%d\n",ans);
}
}

原文地址:https://www.cnblogs.com/guanwen769aaaa/p/9974625.html

时间: 2024-10-04 19:04:22

11.17 dfs poj1979 Red and Black的相关文章

DFS/BFS-A - Red and Black

A - Red and Black 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

POJ1979 Red and Black

问题链接:POJ1979 Red and Black. 题意简述:输入正整数w和h,w为列数,h为行数.输入h×w矩阵 (1 <= h <= 20; 1 <= w <= w),其中'.'代表可到达,'#'代表不可到达,'@'代表开始点.问从'@'开始可以到达最多多少个点. 问题分析:本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大.需要注意的是'@'也算一个可以到达的点. 程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即

2015.11.17 新起点,出发。

今天注册了博客,要开始认真仔细的学习前端技术了.这个博客将作为我学习进度记录.学习笔记整理.心得体会整理等书写记录的工具. —希望苍天不负有心人 2015.11.17

11.14/11.15 Apache和PHP结合 11.16/11.17 Apache默认虚拟主机

11.14/11.15 Apache和PHP结合 编辑:httpd主配置文件/usr/local/apache2.4/conf/httpd.conf 去掉#号 将php7加# Telnet IP+80 端口不通,需要开启iptables防火墙 更改require 更改配置后需要操作才能生效 重新加载服务 增加一行php进行解析 页面显示 结果访问的是源代码 如果遇到php无法解析,需要检查相关的apache的配置文件 (1)检查apache的php 有没有加载php5 查看是否有libphp5.

[dfs] [FloodFill] POJ1979 Red And Black

分析题目: 用dfs实现FloodFill. @和.实际上都是表示该格可走,#则表示该格不能走,想到用 bool map[i][j] 存储是否可走. 用visit[i][j]表示是否已经访问过. 从一个黑格只能到达上,下,左,右四块相邻的格,该格应该未访问过且该格不能是红格. inmap(i, j)判断该格是否在map中. 解题代码: 1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 boo

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 mo

poj1979 Red and Black(DFS)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25797   Accepted: 13967 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

poj1979 Red and Black【搜索】

点击打开题目 Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23194   Accepted: 12513 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.

poj-1979 red and black(搜索)

Time limit1000 ms Memory limit30000 kB 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 r