POJ 1979 Red and Black (BFS)
**链接 : ** Here!
**思路 : ** 简单的搜索, 直接广搜就ok了.
/*************************************************************************
> File Name: E.cpp
> Author:
> Mail:
> Created Time: 2017年11月26日 星期日 10时51分05秒
************************************************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
#define MAX_N 30
int N, M, total_num;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int vis[MAX_N][MAX_N];
char G[MAX_N][MAX_N];
struct Point {
Point() {}
Point(int x, int y) : x(x), y(y) {}
int x, y;
};
Point st;
bool check(Point pt) {
if (pt.x < 0 || pt.x >= N || pt.y < 0 || pt.y >= M) return false;
if (vis[pt.x][pt.y]) return false;
if (G[pt.x][pt.y] == '#') return false;
return true;
}
void DFS(Point pt) {
++total_num;
vis[pt.x][pt.y] = 1;
for (int i = 0 ; i < 4 ; ++i) {
Point temp(pt.x + dx[i], pt.y + dy[i]);
if(!check(temp)) continue;
vis[temp.x][temp.y] = 1;
DFS(temp);
}
}
void solve() {
memset(vis, 0, sizeof(vis));
for (int i = 0 ; i < N ; ++i) {
for (int j = 0 ; j < M ; ++j) {
if (G[i][j] == '@') {
st.x = i; st.y = j;
break;
}
}
}
DFS(st);
printf("%d\n", total_num);
}
void read() {
for (int i = 0 ; i < N ; ++i) {
getchar();
scanf("%s", G[i]);
}
}
int main() {
// freopen("./in.in", "r", stdin);
while (scanf("%d%d", &M, &N) != EOF) {
if (N == 0 && M == 0) break;
memset(G, 0, sizeof(G));
total_num = 0;
read();
solve();
}
return 0;
}
POJ 1979 Red and Black (BFS)的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 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 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- 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
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #inc ...
- HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
随机推荐
- 在shell脚本中使用函数的返回值
#!/bin/bash - function mytest() { echo "arg1 = $1" if [ $1 = "1" ] ;then return ...
- java 效率编程 的一些小知识点
1.在程序中若出现字符串连接的情况.请使用StringBuffer取代String,这样能够降低多次创建String以及垃圾回收所带来的内存消耗 2.尽量使用局部变量. 调用方法时传递的參数以及调用中 ...
- activeMQ公布订阅模式中中经常使用工具类
package com.jms; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.j ...
- 2014年辛星解读css第二节
第一节我们简单介绍了一下CSS的工作流程,我相信读者会有一个大体的认识,那么接下来我们将会深入的研究一下CSS的细节问题,这些问题的涉及将会使我们的工作更加完好. *************凝视*** ...
- windows server使用 LetsEncrypt-Win-Simple来安装和使用用Let's Encrypt免费SSL证书
一.网站部署 LetsEncrypt-Win-Simple可以自动发现已经部署的网站供我们选择要生成证书的网站,而且还需要进行验证.所以在生成证书之前,确保网站已经部署好并可以正常访问. 二.生成证书 ...
- Android之——常见Bug及其解决方式
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46942139 1.android.view.WindowManager$BadTo ...
- bzoj2115
线性基+dfs树 我们先搞出dfs树,其实最终路径就是最初的路径和一些环异或. 环最多只有m-n+1,因为一共有m条边,然后有n-1条边在dfs树上,所以还剩m-n+1条边,都可以构成环. 所以dfs ...
- C++ 对象的赋值和复制 基本的
对象的赋值 如果对一个类定义了两个或多个对象,则这些对象之间是可以进行赋值,或者说,一个对象的值可以赋值给另一个同类的对象.这里所指的值是指对象中所有数 据的成员的值.对象之间进行赋值是“ ...
- [Apple开发者帐户帮助]一、开始(1)关于您的开发者帐户
Apple开发人员网站提供了为Apple平台制作出色应用所需的工具和信息.如果您不熟悉Apple平台上的开发,可以免费使用.只需接受Apple开发者协议,即可为您创建一个帐户.使用此帐户下载测试版软件 ...
- Vue 区别
computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.