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 ...
随机推荐
- 动态加入的HTML的自己主动渲染
这两天在写一个用EasyUI的前台,遇到动态向Layout加入HTML内容时没有自己主动渲染的问题.查了一下网上的资料后得以解决.详细例如以下: $("#content").htm ...
- Myeclipse10完美破解过程
Myeclipse10完美破解过程 1.假设还没有破解文件的话,能够先到这里去下载破解件 http://download.csdn.net/download/wangcunhuazi/7874155 ...
- cppunit 单元测试程序
原文:http://www.cnblogs.com/wang-kai/p/5106740.html 极限编程(XP)推崇测试优先原则,由此引发了软件开发方法从传统的瀑布模型转向以测试为驱动的敏捷开发模 ...
- android屏幕适配之精准适配
(1554068430@qq.com)(android精准适配工具)近期这段时间项目要做适配,在网上方便的方法.后来依据http://blog.csdn.net/jdsjlzx/article/det ...
- LeetCode208:Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- Android源码编译全过程记录(基于最新安卓5.1.0)【转】
本文转载自:http://blog.csdn.net/drg1612/article/details/44802533 我的编译条件: 1 Ubuntu Kylin 14.04 长期支持版 下载地址 ...
- Codeforces--596A--Wilbur and Swimming Pool(数学)
A - Wilbur and Swimming Pool Crawling in process... Crawling failed Time Limit:1000MS Memory ...
- P2532 [AHOI2012]树屋阶梯 卡特兰数
这个题是一个卡特兰数的裸题,为什么呢?因为可以通过划分来导出递推式从而判断是卡特兰数,然后直接上公式就行了.卡特兰数的公式见链接. https://www.luogu.org/problemnew/s ...
- PCB MVC启动顺序与各层之间数据传递对象关系
准备着手基于MVC模式写一套Web端流程指示查看,先着手开发WebAPI打通数据接口,后续可扩展手机端 这里将MVC基本关系整理如下: 一.MVC启动顺序 二.MVC各层之间数据传递对象关系
- PCB MS SQL 行转列(动态拼SQL)
一.原数据: SELECT inman,indate FROM [fp_db].[dbo].[ppezhpbb] WHERE indate > '2016-5-1' AND indate < ...