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 ...
随机推荐
- ipcs命令学习
参考这篇 http://blog.csdn.net/pyjfoot/article/details/7989097 ipcs -m -s -q 分别对应集中ipc ipcs -l 显示limits: ...
- 使用resultMap实现ibatis复合数据结构查询(1.多重属性查询;2.属性中含有列表查询)
以订单为例(订单详情包括了订单的基本信息,配送物流信息,商品信息),直接上代码: 1.多重属性查询 java实体 public class OrderDetail { @XmlElement(requ ...
- 【Unity3D】 KeyCode 键码
Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard. KeyCode是由 ...
- Java判断是否为移动端
以下为常用判断,可直接创建使用 /** * Created by kangao on 2018/3/23. */public class UAgentInfoHelper { // User-Agen ...
- It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)
Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS Memory Limit: 30000K Total Su ...
- jQuery - 当当网我的订单页
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- oc45--多对象内存管理 优化
// // main.m // Set方法的内存管理 #import <Foundation/Foundation.h> #import "Person.h" #imp ...
- 对ip数据进行分类----c++
#!/usr/bin/expect set ip [lindex $argv ] set password [lindex $argv ] spawn -l root ${ip} "host ...
- git常见冲突及解决办法
1.内容冲突 产生冲突的原因:两个用户修改了同一个文件的同一块区域,git会报告内容冲突.我们常见的都是这种. 解决冲突的办法:编辑冲突文件,修改冲突. 例如:冲突文件test.c test.c发生冲 ...
- layui富文本编译器后台获取图片路径
@RequestMapping("add") public ModelAndView add(News news){ ModelAndView mav = ne ...