HDOJ1548(DFS超内存,BFS过了)

DFS代码

#include<iostream>
#include<cstdio>
using namespace std;
#define Min(a,b) (a<b)?a:b
const int MAX_N=205;
const int INF=0x30303030;
int floors[MAX_N];
int N, A, B;
int step[MAX_N];
int dfs(int k)
{
    if(k<1||k>N)    return INF;
    if(step[k]!=-1)
    {
        return step[k];
    }
    if(k==B)
    {
        return 0;
    }
    return step[k]=Min(dfs(k-floors[k])+1,dfs(k+floors[k])+1);
}

int main()
{
      while(scanf("%d",&N)!=EOF&&N!=0)
      {
          scanf("%d %d",&A, &B);
          for(int i=1; i<=N; i++)
          {
              scanf("%d",&floors[i]);
          }
          memset(step,-1,sizeof(step));
          int ans=dfs(A);
          if(ans>=INF)
          {
              printf("-1\n");
          }
          else
          {
              printf("%d\n",ans);
          }
      }

    return 0;
}

BFS:AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAX_N=205;
const int INF=0x30303030;
int floors[MAX_N];
int N, A, B;
typedef pair<int, int> P;
int vis[MAX_N];

int bfs()
{
    memset(vis, 0, sizeof(vis));
    queue<P> que;
    que.push(P(0,A));
    vis[A]=1;
    while(!que.empty())
    {
        P pos=que.front();que.pop();
        int k=pos.second;
        int step=pos.first;
        if(k==B)
        {
            return step;
        }
        for(int i=-1; i<=1; i++)
        {
            int next=(k + i*floors[k]);
            if(next>=1&&next<=N&&!vis[next])
            {
                vis[next]=1;
                que.push(P(step+1,next));
            }
        }

    }

    return INF;
}

int main()
{
      while(scanf("%d",&N)!=EOF&&N!=0)
      {
          scanf("%d %d",&A, &B);
          for(int i=1; i<=N; i++)
          {
              scanf("%d",&floors[i]);
          }
          int ans=bfs();
          if(ans==INF)
          {
              printf("-1\n");
          }
          else
          {
              printf("%d\n",ans);
          }
      }

    return 0;
}
时间: 2024-08-01 15:01:14

HDOJ1548(DFS超内存,BFS过了)的相关文章

杭电4608(I-number) java写很容易 就是超内存!!!

不用java就用大数模板做见hdu1002,java写很容易 就是超内存!!! Problem Description The I-number of x is defined to be an integer y, which satisfied the the conditions below: 1. y>x; 2. the sum of each digit of y(under base 10) is the multiple of 10; 3. among all integers t

CodeForces 589J Cleaner Robot (DFS,或BFS)

题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子. 析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能转向,其次就是走过的地方也能走,注意这两点,就可以AC了,可以用DFS,也可以用BFS, 我用的DFS. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

图的dfs遍历和bfs遍历

对如下图进行广度和深度遍历; dfs遍历,(依次输出遍历顶点): 用邻接矩阵存图(用一个二维数组把图存起来)! <span style="font-size:18px;">#include<stdio.h> #define MAX 9999999//当顶点之间不相通时,标记为一个很大的数 int sum=0;//记录遍历的顶点的个数 int v,s;//顶点数和边数 int book[50]={0},p[30][30];//标记数组和矩阵 void dfs(in

UVA - 10410 Tree Reconstruction (根据dfs序和bfs序恢复一颗树)

题意: 分析: 这题一开始完全没有思路, 一直没有找出规律. 参考了http://www.cnblogs.com/Wade-/p/6358859.html 和 http://www.cnblogs.com/jerryRey/p/4622927.html 在dfs序列中,相邻的两个结点u,v之间(dfs_pos(u) + 1 =  dfs_pos(v)),有父子,兄弟,其他3种关系. 父子关系:在bfs中u,v并不是相邻, bfs_pos(v) > bfs_pos(u) , 那么u为v父亲, v为

POJ 1426 Find The Multiple(大数取模)【DFS】||【BFS】

<题目链接> 题目大意: 给一个小于200的正整数n,问只有0和1组成的位数小于100的最小能被n整除的数是多少. 解题分析: 用DFS或者BFS沿着位数进行搜索,每一次搜索到下一位都有两种情况,0或者1,还要注意的是,大数取模的方法.但是我有一个疑问,这题位数最高为100,用dfs为什么不会超时?还是说只是此题数据太弱吗? BFS解法 #include<iostream> #include<cstdio> #include<queue> #include&

深度优先dfs与广度bfs优先搜索总结+例题

DFS(Deep First Search)深度优先搜索 深度优先遍历(dfs)是对一个连通图进行遍历的算法.它的思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念. 简而言之:不撞南墙不回头 模板如下: void dfs(int t)//t代表目前dfs的深度 { if(满足输出条件||走不下去了) { 输出解; return; } else { for(int i=1;i<=尝试方法数;

POJ1420 Spreadsheet(拓扑排序)注意的是超内存

Spreadsheet Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 617   Accepted: 290 Description In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a huge success and, at that time, was the ki

【Python算法】遍历(Traversal)、深度优先(DFS)、广度优先(BFS)

图结构: 非常强大的结构化思维(或数学)模型.如果您能用图的处理方式来规范化某个问题,即使这个问题本身看上去并不像个图问题,也能使您离解决问题更进一步. 在众多图算法中,我们常会用到一种非常实用的思维模型--遍历(traversal):对图中所有节点的探索及访问操作. 图的一些相关概念: 简单图(Simple graph):无环并且无平行边的图. 路(path):内部点互不相同的链. 如果无向图G中每一对不同的顶点x和y都有一条路,(即W(G)=1,连通分支数)则称G是连通图,反之称为非连通图.

树的遍历——A1004.Counting Leaves(30) 给出一棵树,问每一层有多少叶子节点(可DFS也可BFS)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 100010; struct node{ //bool leave;//是否是叶节点 vector<int> child; }Node[maxn]; void BFS(int root){ if(Node[roo