题意:

      给你一个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. Python接口测试-保持登录状态

    #coding:utf-8import requestsimport json#登录url ="https://passport.cnblogs.com/user/signin"h ...

  2. 【odoo14】第三章、创建插件

    现在我们已经有了开发环境并了解了如何管理实例及数据库,现在让我们来学习下如何创建插件模块. 本章内容如下: 创建和安装模块 完成manifest文件 组织模块文件结构 添加模型 添加菜单及视图 添加访 ...

  3. CentOS7.8搭建STF

    安装命令插件(rz.sz): yum install -y lrzsz wget unzip zip编辑配置文件导致命令无法使用时:export PATH=/usr/local/sbin:/usr/l ...

  4. [HDU5592] ZYB's Premutation

    [HDU5592] ZYB's Premutation 题目大意:一个由\([1,n]\)组成的数列,但不知道具体排列,但给出每个前缀的逆序对数目,让你还原排列 Solution 创造一颗\([1,n ...

  5. 在 .NET Core 中构建 REST API

    翻译自 Camilo Reyes 2020年8月26日的文章 <Build a REST API in .NET Core> [1] REST API 可以使用简单的动词(如 POST.P ...

  6. 为 .NET 打 Call,为国产平台 Gitee 打 Call,我的 .NET/C# 开源项目清单,同步维护于 Github 和 Gitee

    所有项目遵循 MIT 开源协议.可以随意使用,但是需在源代码和产品关于画面保留版权声明和我的网站链接,谢谢. Sheng.Winform.IDE Github:https://github.com/i ...

  7. [Kong 与 Konga 与 Postgres数据库] 之 Kuberneres 部署

    1.Kong的概述 Kong是一个clould-native.快速的.可扩展的.分布式的微服务抽象层(也称为API网关.API中间件或在某些情况下称为服务网格)框架.Kong作为开源项目在2015年推 ...

  8. HTML5-本地存储浅谈

    Web Storage是HTML5里面引入的一个类似于cookie的本地存储功能,可以用于客户端的本地存储 sessionStorage && localStorage session ...

  9. 解决跨域问题chrome浏览器插件

    https://www.crx4chrome.com/crx/53489/ 解决chrome浏览器跨域的问题

  10. Python是啥?为什么这么多职业人和学生就算报班也要学它?!

    嗨,大家好 这里是汐仔 首先我们先来考究一下近几年的头条和新闻. 1.早在2018年python就已经被纳入高考之一了 2.Python加入全国计算机等级考试,从2018年九月起新增为大学计算机二级考 ...