[DFS] & [BFS] poj1979 poj3009 poj3669

都比较简单,直接贴代码吧。

poj1979 DFS

题目大意:给你一个二维数组,.表示可以到达,#表示障碍,@表示起始位置,问你能到达的最大地点有多少个,每次只能走上下左右

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, m, sx, sy, ans;
int pd[30][30];
char maze[30][30];

int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void dfs(int x, int y)
{
    ans++, pd[x][y] = 1;
    for(int i = 0; i < 4; i++)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
        if(pd[nx][ny] == 1 || maze[nx][ny] != ‘.‘) continue;
        dfs(nx, ny);
    }
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        if(n == 0 && m == 0) break;
        int t = n;
        n = m, m = t;
        for(int i = 0; i < n; i++)
            scanf("%s",maze[i]);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(maze[i][j] == ‘@‘) sx = i, sy = j;
        ans = 0;
        memset(pd, 0, sizeof(pd));
        dfs(sx, sy);
        printf("%d\n", ans);
    }
    return 0;
}

poj3009 DFS

题目大意:

就是要求把一个冰壶从起点“2”用最少的步数移动到终点“3”

其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1 或者 到达终点 3

要注意:可能会出边界,边界是没有障碍物抵挡的!

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, m, sx, sy, tx, ty, ans;
int maze[30][30];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int dfs(int k, int x, int y)
{
    if(x == tx && y == ty) return k - 1;
    if(k > 10) return -1;
    int nx, ny, min = -1, res;
    for(int i = 0; i < 4; i++)
    {
        nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 || ny < 0 || nx > n-1 || ny > m-1 || maze[nx][ny] == 1) continue;
        while(1)
        {
            if(nx == tx && ny == ty) return k;
            nx += dx[i], ny += dy[i];
            if(nx < 0 || ny < 0 || nx > n-1 || ny > m-1) break;
            if(nx == tx && ny == ty) return k;
            if(maze[nx][ny] == 1)
            {
                maze[nx][ny] = 0;
                res = dfs(k + 1, nx - dx[i], ny - dy[i]);
                maze[nx][ny] = 1;
                if(min == -1) min = res;
                else if(res != -1 && res < min) min = res;
                break;
            }
        }
    }
    return min;
}

int main()
{
    while(scanf("%d%d", &m, &n) != EOF)
    {
        memset(maze, 0, sizeof(maze));
        if(n == 0 && m == 0) break;
        for(int i = 0; i < n; i++)
            for(int j = 0; j  < m; j++)
            {
                scanf("%d", &maze[i][j]);
                if(maze[i][j] == 2) sx = i, sy = j, maze[i][j] = 0;
                if(maze[i][j] == 3) tx = i, ty = j, maze[i][j] = 0;
            }
        ans = dfs(1, sx, sy);
        printf("%d\n", ans);
    }
    return 0;
}

poj3669 BFS

题目大意:

给定几个坐标,在这些坐标上 t 时刻会有陨石雨。

怎样在最短的时间内找到一个安全的地方。

方法:预处理,把每个坐标有陨石的地方预处理出来,这样在bfs的时候会很简单,比如不用考虑待在原点不懂,或者往回走之类的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int dt[400][400];
int dx[5] = {0, 0, 1, 0, -1}, dy[5] = {0, 1, 0, -1, 0};

struct node {
    int x, y, t;
};

int bfs()
{
    if(dt[0][0] == 0) return -1;
    if(dt[0][0] == -1 ) return 0;
    queue<node> q;
    node temp,newt;
    temp.x = 0, temp.y = 0, temp.t = 0;
    q.push(temp);
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        for(int i = 1; i < 5; i++)
        {
            newt.x = temp.x + dx[i];
            newt.y = temp.y + dy[i];
            newt.t = temp.t + 1;
            if(newt.x < 0 || newt.y < 0) continue;
            if(dt[newt.x][newt.y] == -1) return newt.t;
            if(dt[newt.x][newt.y] <= newt.t) continue;
            q.push(newt);
            dt[newt.x][newt.y] = newt.t;
        }
    }
    return -1;
}

int main()
{
    int m;
    while(scanf("%d", &m) != EOF)
    {
        int x, y, t;
        memset(dt, -1, sizeof(dt));
        for(int j = 0; j < m; j++)
        {
            scanf("%d%d%d", &x, &y, &t);
            for(int i = 0; i < 5; i++)
            {
                int nx = x + dx[i], ny = y + dy[i];
                if(nx < 0 || ny < 0) continue;
                if(dt[nx][ny] == -1) dt[nx][ny] = t;
                else if(dt[nx][ny] > t) dt[nx][ny] = t;
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}

时间: 2024-11-06 19:41:31

[DFS] & [BFS] poj1979 poj3009 poj3669的相关文章

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

HDU 4771 (DFS+BFS)

Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know,

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因为广搜需要的就是队列,所以相比递归队列更耗内存? 当然DFS并不像上图所说,需要用栈,而是运用递归即可. BFS: 因为BFS是要一个接一个的遍历,所以用到了结构体,来保存坐标和当前所走步数 1.每走一步,通过定义的结构体,从队列中提取a(即上一步的坐标.步数(步数每次累加)) 2.在a的基础上进行

DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门 1 /* 2 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 3 在树上的路径权值都小于这两个点 4 DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案.因为排序后,他们之间的路径, 5 可定都是从当前节点u连过去的,那么都是小于这两个节点的.DFS需手动加栈,BFS类似拓扑排序的思路 6 */ 7 #pragma comment (linker, "/STACK:1024000000,10240000

Dfs/Bfs/记忆化搜索问题 | 问题集合

写在前面 动归和搜索似乎我打得特憋懒. 可能是因为搜索打的太少了??? 然后之前做过的一些题我就不再写了,比如填涂颜色/海战啥的? 然后每一题打两种解法(:Dfs/Bfs 前提是在题目里两种都能A P1596 湖计数 题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <=