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 ...
随机推荐
- Java 面向对象 03
面向对象·三级 代码块的概述和分类 * A:代码块概述 * 在Java中,使用 { } 括起来的代码被称为代码块. * B:代码块分类 * 根据其位置和声明的不同,可以分为局部代码块, ...
- 050_Servlet详解
目录 Servlet Servlet简介 HelloServlet Servlet原理 servlet-mapping Servlet请求路径 ServletContext Servlet上下文 Se ...
- pta 简单求和
6-1 简单求和 (10 分) 本题要求实现一个函数,求给定的N个整数的和. 函数接口定义: int Sum ( int List[], int N ); 其中给定整数存放在数组List[]中,正 ...
- 开源的 Switch 模拟器——GitHub 热点速览 v.21.12
作者:HelloGitHub-小鱼干 脸滚键盘操作选手小鱼干这里要推荐一个超酷 Switch 模拟器,不能埋没你的游戏天赋.Ryujinx 是一个 C# 写的 Switch 模拟器,1700+ 游戏可 ...
- 如何在Google Web Toolkit环境下Getshell
出品|MS08067实验室(www.ms08067.com) 本文作者:大盗贼卡卡 Google Web Toolkit简称(GWT),是一款开源Java软件开发框架.今天这篇文章会介绍如何在这样的环 ...
- Mysql之案例分析(一)
可见性分析 CREATE TABLE `t` ( `id` int(11) NOT NULL, `k` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGIN ...
- 扩展中国剩余定理(EXCRT)学习笔记
扩展中国剩余定理(EXCRT)学习笔记 用途 求解同余方程组 \(\begin{cases}x\equiv c_{1}\left( mod\ m_{1}\right) \\ x\equiv c_{2} ...
- MongoDB教程--配置与入门
MongoDB简介 阿里云配置MongoDB 数据库的增删查改 MongoDB 数据最重要的操作是Key-Value的映射.有了这样的映射,可以直接通过关键字去寻找想要的值.例如,通过用户的ID寻找与 ...
- 神奇的魔方阵--(MagicSquare)(1)
本篇文章只对奇数阶以及偶数阶中阶数n = 4K的魔方阵进行讨论.下面就让我们进入正题: 1 :魔方阵的相关信息:(百度百科) https://baike.baidu.com/item/%E9%AD%9 ...
- BUAA_2021_SE_Pair_Work_#3_Review
结对项目第三阶段博客 项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 结对项目-第三阶段 我在这个课程的目标是 通过课程学习,完成第一个可以称之为 ...