POJ-1979 Red and Black
来源:互联网 发布:redis mysql同步 编辑:程序博客网 时间:2024/06/06 09:16
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int maxn = 21;char room[maxn][maxn];int sign[maxn][maxn];int stp[4][2] = {{0,1}, {0,-1}, {-1,0}, {1,0}};int w, h, ans;struct Room{ int x; int y;} start;bool judge(int x, int y){ if(x >= 0 && y >= 0 && x < w && y < h) return true; else return false;}void dfs(Room temp){ sign[temp.x][temp.y] = 1; for(int i = 0; i < 4; i ++ ) { int nx = temp.x + stp[i][0]; int ny = temp.y + stp[i][1]; if(judge(nx, ny) && !sign[nx][ny] && room[nx][ny] == '.') { Room next; next.x = nx; next.y = ny; ans ++; dfs(next); } } return ;}int main(){ while(~scanf("%d %d", & w, & h)) { getchar(); if(!w && !h) break; for(int i = 0; i < h; i ++) { for(int j = 0; j < w; j ++) { scanf("%c", & room[j][i]); if(room[j][i] == '@') { start.x = j; start.y = i; } } getchar(); } memset(sign, 0, sizeof(sign)); ans = 1; dfs(start); cout << ans << endl; }}
题意:就是给一个迷宫一样的东西。例如:
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
@代表起点。#代表墙(不能过)。.代表路。问从@出发最多可以走过多少个.(@初始位置也算1)。
题解:差不多算是完全自己着手的一个dfs..也是一道水题。这道水题也让我对dfs更了解。dfs就是沿着一个方向一直走到底,再换另一个方向。bfs是四个方向同时进行。注意return的位置。(一开始放在上一个{}中 即for循环里出错)输入的时候注意getchar()。
0 0
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 - Red and Black
- POJ-1979-Red and Black
- poj 1979 red and black
- poj 1979 Red and Black
- poj 1979 Red and Black
- POJ:1979 Red and Black
- POJ 1979 - Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979-Red and Black
- 关于中国教育的反思
- javabeanUtil泛型操作类
- Python 的列表排序
- iO开发资料
- WPF中WebBrowser的使用方法及分析
- POJ-1979 Red and Black
- android unit test
- [优先队列+贪心]poj3614 Sunscreen
- C# 获取当前路径方法
- 结构体的定义及其初始化方法And typedef的使用-----------权当笔记了
- [kuangbin带你飞]专题四 最短路练习
- 数据结构基础(22)--红黑树的设计与实现(上)
- Mybatis学习笔记-事务管理详解
- Android中inflate简介