hdu1312

//简单搜索,只要走遍所有可达的路径

BFS版:

#include "iostream"
#include "cstdio"
#include "memory.h"
#include "queue"
using namespace std;
const int MAX = 22;
const int dx[] = {1,-1,0,0},dy[] = {0,0,1,-1};
char map[MAX][MAX];
int vis[MAX][MAX];
int x,y;
int n,m;
pair<int ,int> now;
int bfs()
{
    int cnt = 1;
    memset(vis,0,sizeof(vis));
    queue< pair<int,int> > que;
    que.push(make_pair(x,y));
    vis[x][y] = 1;
    while (!que.empty()) {
        now = que.front();
        que.pop();
        int i,tx,ty;
        for (i = 0;i < 4; ++ i) {
            tx = now.first + dx[i];
            ty = now.second + dy[i];
            if (tx < 0 || ty < 0|| tx >= m || ty >= n) continue;
            if (vis[tx][ty]) continue;
            if (map[tx][ty] == ‘.‘) {
                cnt++;
                que.push(make_pair(tx,ty));
            }
            vis[tx][ty] = 1;
        }
    }
    return cnt;
}
int main()
{
    int i,j;
    while (cin >> n >> m,m+n) {   //m行    n列
        for (i = 0; i < m; ++ i)
            for (j = 0;j < n ; ++ j) {
                cin >> map[i][j];
                if (map[i][j] == ‘@‘)
                    x = i,y = j;
            }
            //cout << x << " " << y << endl;
            cout << bfs() << endl;

    }
    return 0;
}

DFS:
#include "iostream"
#include "cstdio"
#include "memory.h"
#include "queue"
using namespace std;
const int MAX = 22;
const int dx[] = {1,-1,0,0},dy[] = {0,0,1,-1};
char map[MAX][MAX];
int vis[MAX][MAX];
int x,y;
int n,m;
int cnt;
void dfs(int x,int y)
{
    vis[x][y] = 1;
    int i, tx, ty;
    for (i = 0;i < 4 ; ++i) {
        tx = x + dx[i];
        ty = y + dy[i];
        if (tx >= 0 && ty >= 0 && tx < m && ty < n && map[tx][ty] != ‘#‘ && !vis[tx][ty]) {
                cnt ++;
                dfs(tx,ty);
        }
    }
}
int main()
{
    int i,j;
    while (cin >> n >> m,m+n) {   //m行    n列
        for (i = 0; i < m; ++ i)
            for (j = 0;j < n ; ++ j) {
                cin >> map[i][j];
                if (map[i][j] == ‘@‘)
                    x = i,y = j;
            }
            cnt = 1;
            dfs(x,y);
            cout << cnt << endl;
        memset(vis,0,sizeof(vis));
    }
    return 0;
}
				
时间: 2024-10-11 12:46:20

hdu1312的相关文章

HDU1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9732    Accepted Submission(s): 6060 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

题目链接:HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9902    Accepted Submission(s): 6158 Problem Description There is a rectangular

HDU1312 Red and Black 题解

一条递归搜索法题目,使用递归搜索法,但是实际不用重复计算方格. 思路是: 1 每搜索一个方格就改变当前方格的值为 '*',或者任何其他非'.'的值,代表该方格已经走过了 2 递归的时候不回复这个方格的值,就实际上不用重复搜索这个方格了,故此不用回溯 #include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; int R, C,

HDU1312——Red and Black

Red and Black Problem DescriptionThere 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 til

hdu1312(Red and Black)

点击打开杭电1312 Problem 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

HDU1312:Red and Black(DFS)

题目描述 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 only on blac

HDU1312:Red and Black

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 move only

《算法竞赛从入门到进阶》第四章 搜索技术 hdu1312 &quot;Red and Black&quot; BFS

#include<iostream> #include<cstdio> #include<queue> using namespace std; char room[25][25]; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; int Wx,Hy,num; //Wx,Hy:长宽的边界 ,num:从初始瓷砖到能到达的瓷砖的总数 #define CHECK(x,y) (x<Wx&&x>=0&&am

HDU-1312题解(DFS)

HDU-1312-DFS Written by Void-Walker    2020-02-01 09:09:25 1.题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1312 2.题目大意: 有一个矩形房间,房间里有红砖块(‘#’)和黑砖块(‘.’)组成.现在有一个人站在一个@上面,他只能走黑色方块,现在问他最多能经过多少黑色方块.(他的初始位置也算) 3.题目思路: 这道题是一个非常经典的深度优先搜索.我们从他的初始位置开始搜索: for(y