题意:

      给你一个n*m的格子,然后给你一个起点,让你遍历所有的垃圾,就是终点不唯一,问你最小路径是多少?

思路:

      水题,方法比较多,最省事的就是直接就一个BFS状态压缩暴搜就行了,时间复杂度20*20*1024的,完全可以接受,但是被坑了,一开始怎么交都TLE,后来又写了一个BFS+DFS优化,就是跑之前先遍历一遍图,看看是不是所有的垃圾点都能遍历到,这样还是超时,无奈看了下讨论,有人说用G++交就行了,我用G++交了结果两个方法都AC了,哎!下面是两个方法的代码,比较简单,最近就是无聊,上POJ来刷刷水题,还有这个题目,可以用DP去做,还有就是可以直接求出任意两个垃圾的最短距离,然后在枚举处理垃圾顺序,枚举可以用STL的全排列,也可以搜索,这样的时间复杂度大约是N!吧,跟直接暴搜没啥区别,想试的可以敲敲试试吧。

直接BFS状态压缩暴力625

#include<queue>

#include<stdio.h>

#include<string.h>

using namespace std;

typedef struct

{

    int x ,y ,k ,t;

}NODE;

NODE xin ,tou;

int map[22][22] ,n ,m ,w;

int mark[22][22][1025];

int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};

bool ok(int x ,int y ,int k)

{

    return x >= 1 && x <= n && y >= 1 && y <= m && map[x][y] && !mark[x][y][k];

}

int BFS(int x ,int y)

{

    queue<NODE>q;

    xin.x = x ,xin.y = y;

    xin.t = 0 ,xin.k = 0;

    memset(mark ,0 ,sizeof(mark));

    mark[xin.x][xin.y][xin.k] = 1;

    q.push(xin);

    while(!q.empty())

    {

        tou = q.front();

        q.pop();

        for(int i = 0 ;i < 4 ;i ++)

        {

            xin.x = tou.x + dir[i][0];

            xin.y = tou.y + dir[i][1];

            xin.t = tou.t + 1;

            if(map[xin.x][xin.y] && map[xin.x][xin.y] != -1)

            xin.k = tou.k | (1 << (map[xin.x][xin.y] - 1));

            else xin.k = tou.k;

            if(ok(xin.x ,xin.y ,xin.k))

            {

                mark[xin.x][xin.y][xin.k] = 1;

                q.push(xin);

                if(xin.k == (1 << w) - 1) return xin.t;

            }

        }

    }

    return -1;

}

int main ()

{

    char str[22];

    int x ,y;

    while(~scanf("%d %d" ,&m ,&n) && n+m)

    {

        w = 0;

        for(int i = 1 ;i <= n ;i ++)

        {

            scanf("%s" ,str);

            for(int j = 1 ;j <= m ;j ++)

            {

                if(str[j-1] == '.') map[i][j] = -1;

                else if(str[j-1] == '*') map[i][j] = ++w;

                else if(str[j-1] == 'x') map[i][j] = 0;

                else x = i ,y = j ,map[i][j] = -1;

            }

        }

        w ? printf("%d\n" ,BFS(x ,y)):printf("0\n");

    }

    return 0;

}

BFS+DFS剪枝594

#include<queue>

#include<stdio.h>

#include<string.h>

using namespace std;

typedef struct

{

    int x ,y ,k ,t;

}NODE;

NODE xin ,tou;

int col[22][22] ,cmk[22][22];

int map[22][22] ,n ,m ,w;

int mark[22][22][1025];

int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};

bool ok(int x ,int y ,int k)

{

    return x >= 1 && x <= n && y >= 1 && y <= m && map[x][y] && !mark[x][y][k];

}

int BFS(int x ,int y)

{

    queue<NODE>q;

    xin.x = x ,xin.y = y;

    xin.t = 0 ,xin.k = 0;

    memset(mark ,0 ,sizeof(mark));

    mark[xin.x][xin.y][xin.k] = 1;

    q.push(xin);

    while(!q.empty())

    {

        tou = q.front();

        q.pop();

        for(int i = 0 ;i < 4 ;i ++)

        {

            xin.x = tou.x + dir[i][0];

            xin.y = tou.y + dir[i][1];

            xin.t = tou.t + 1;

            if(map[xin.x][xin.y] && map[xin.x][xin.y] != -1)

            xin.k = tou.k | (1 << (map[xin.x][xin.y] - 1));

            else xin.k = tou.k;

            if(ok(xin.x ,xin.y ,xin.k))

            {

                mark[xin.x][xin.y][xin.k] = 1;

                q.push(xin);

                if(xin.k == (1 << w) - 1) return xin.t;

            }

        }

    }

    return -1;

}

void DFS(int x ,int y)

{

    for(int i = 0 ;i < 4 ;i ++)

    {

        int xx = x + dir[i][0];

        int yy = y + dir[i][1];

        if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !cmk[xx][yy] && map[xx][yy])

        {

            cmk[xx][yy] = 1;

            DFS(xx ,yy);

        }

    }

}

int main ()

{

    char str[22];

    int x ,y;

    while(~scanf("%d %d" ,&m ,&n) && n+m)

    {

        w = 0;

        for(int i = 1 ;i <= n ;i ++)

        {

            scanf("%s" ,str);

            for(int j = 1 ;j <= m ;j ++)

            {

                if(str[j-1] == '.') map[i][j] = -1;

                else if(str[j-1] == '*') map[i][j] = ++w;

                else if(str[j-1] == 'x') map[i][j] = 0;

                else x = i ,y = j ,map[i][j] = -1;

            }

        }

        memset(cmk ,0 ,sizeof(cmk));

        DFS(x ,y);

        int mk = 0;

        for(int i = 1 ;i <= n && !mk;i ++)

        for(int j = 1 ;j <= m && !mk;j ++)

        if(map[i][j] != -1 && map[i][j] && !cmk[i][j])

        mk = 1;

        if(mk)

        {

            printf("-1\n");

            continue;

        }

        w ? printf("%d\n" ,BFS(x ,y)):printf("0\n");

    }

    return 0;

}

POJ2688状态压缩(可以+DFS剪枝)的更多相关文章

  1. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  2. uva10160(dfs+状态压缩)

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

  3. Sudoku (剪枝+状态压缩+预处理)

    [题目描述] In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. ...

  4. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  5. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  6. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  7. hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]

    题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...

  8. Preparing Olympiad---cf550B(DFS或者状态压缩模板)

    比赛链接:http://codeforces.com/problemset/problem/550/B 给你n个数,选出来只是2个然后求他们的和在L和R的区间内,并且选出来的数中最大值和最小值的差不得 ...

  9. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

随机推荐

  1. Codeforces 682C Alyona and the Tree

    题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...

  2. 用c++解一元二次方程

    解方程 github项目地址 这两天得知初二的表妹学了一元二次方程,听说还不会解,我就想着试试用C语言编写解方程. 一元二次方程 用公式法 这种方法效果很好: #include"funct. ...

  3. 深入浅出新一代跨平台抓包&调式利器Fiddler Everywhere

    什么是Fiddler Everywhere? Fiddler Everywhere is a web debugging proxy for macOS, Windows, and Linux. Ca ...

  4. FreeBSD 12.2 vmware 虚拟机镜像 bt 种子

    安装了 KDE5 火狐浏览器 Fcitx 输入法 并进行了中文设置 替换软件源为国内可用. VirtualBox虚拟机也可以用  magnet:?xt=urn:btih:E88885631B57426 ...

  5. 商品购买 & 收银台订单优化测试点疑问归纳梳理

    摘要 更新内容 更新人 更新时间 初版内容 Young 2020.11.20  16:40 贾轩审查确认 Harry 2020.11.20  17:00 和林森沟通问题答疑 参与人:林森.Harry. ...

  6. BeanShell 用法汇总

    一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...

  7. Jmeter +Jenkins +Ant 集成发送邮件报告

    [TOC] 一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系 ...

  8. 自导自演的面试现场之--你竟然不了解MySQL的组提交?

    Hi,大家好!我是白日梦!本文是MySQL专题的第 26 篇. 下文还是白日梦以自导自演的方式,围绕"组提交"展开本话题.看看你能抗到第几问吧 换一种写作风格,自导自演面试现场!感 ...

  9. [源码解析] 并行分布式框架 Celery 之架构 (2)

    [源码解析] 并行分布式框架 Celery 之架构 (2) 目录 [源码解析] 并行分布式框架 Celery 之架构 (2) 0x00 摘要 0x01 上文回顾 0x02 worker的思考 2.1 ...

  10. io流(对象流总结)

    对象流 对象流就是对引用数据类型进行操作 序列化:将对象的状态信息转换为可以存储或传输的形式的过程,因此类需要序列化后才可以存储到文件中 对象输出流: 很简单,就三句话,将把一个对象导入指定文件中,要 ...