POJ2688状态压缩(可以+DFS剪枝)
题意:
给你一个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剪枝)的更多相关文章
- 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 ...
- uva10160(dfs+状态压缩)
题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...
- Sudoku (剪枝+状态压缩+预处理)
[题目描述] In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- 最大联通子数组之和(dfs,记忆化搜索,状态压缩)
最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]
题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...
- Preparing Olympiad---cf550B(DFS或者状态压缩模板)
比赛链接:http://codeforces.com/problemset/problem/550/B 给你n个数,选出来只是2个然后求他们的和在L和R的区间内,并且选出来的数中最大值和最小值的差不得 ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
随机推荐
- [数据结构与算法-13]ST表
ST表 主要用来快速查询静态数据区间最大值 思路 数组\(A[i][j]\)存储数列\(\{a_i\}\)中区间\(i \in [i, i+2^j)\)的最大值 查询时只需要查询\(max\{A[i] ...
- Python 元类编程实现一个简单的 ORM
概述 什么是ORM? ORM全称"Object Relational Mapping",即对象-关系映射,就是把关系数据库的一行映射为一个对象,也就是一个类对应一个表,这样,写代码 ...
- Java流程控制:用户交互Scanner
java.util.Scanner工具类获取用户输入语法:Scanner scanner = new Scanner(System.in);通过Scanner类的next()与nextLine()方法 ...
- tomcat启动错误Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ofuns]];
起初遇到这个问题的时候是在实训(开发环境选择的是IDEA)的时候,检查了半天未果,上网搜索之后,说的也是各式各样,最后发现问题出在web.xml上面 在配置 filter 的 url-pattern ...
- Mysql将查询结果某个字段以逗号分隔,使用group_concat函数可以实现(配合group by使用)
示例:SELECT Id, GROUP_CONCAT(Name SEPARATOR ',') Names FROM some_table GROUP BY id
- 3.学习numyp的矩阵
Numpy提供了ndarray来进行矩阵的操作,在Numpy中 矩阵继承于NumPy中的二维数组对象,但是矩阵区别于数组,不可共用数组的运算规律 一.创建矩阵 import numpy as np m ...
- Java并发编程之同步辅助类
CountDownLatch 在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待,基于AbstractQueuedSynchronizer实现,state初始化为count,每cou ...
- Java进阶专题(二十六) 将近2万字的Dubbo原理解析,彻底搞懂dubbo
前言 前面我们研究了RPC的原理,市面上有很多基于RPC思想实现的框架,比如有Dubbo.今天就从Dubbo的SPI机制.服务注册与发现源码及网络通信过程去深入剖析下Dubbo. Dubbo架构 ...
- rpm 命令介绍
1. rpm 命令常用选项说明 1.1 功能模式选项 命令 解释 -i --install 安装软件,例:rpm -ivh tree-1.6.0-10.el7.x86_64.rpm -U --upgr ...
- [Fundamental of Power Electronics]-PART II-8. 变换器传递函数-8.4 变换器传递函数的图形化构建
8.4 变换器传递函数的图形化构建 第7章推导出的buck变换器小信号等效电路模型在图8.55中再次给出.让我们用上一节的图解方法来构造该变换器的传递函数和端阻抗. Fig. 8.55 Small-s ...