POJ 1324(BFS + 状态压缩)
题意:给你一条蛇,要求一以最少的步数走到1,1
思路:
最开始一直没想到应该怎样保存状态,后来发现别人用二进制保存蛇的状态,即每两个节点之间的方向和头节点,二进制最多14位(感觉状态保存都能扯到二进制)。然后就是bfs
问题:
1.最开始完全没想到状态压缩的问题
2.感觉现在做题太急,做题没有足够的思考,思路不清晰便开始写,导致在过程中经常崩盘。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
typedef long long ll;
using namespace std;
int flag;
const int maxn = 25;
const int ma = (1<<14)+10;
int vis[maxn][maxn];
int sta[maxn][maxn][ma];
struct node
{
// int snake[15][2];
int x,y;
int step;
int state;
};
node cur;
int t,n,m;
int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
int matri[20][2];
int get_state() //蛇的状态
{
int dir;
int stat = 0;
for(int i = t; i > 1; i --)
{
int x = matri[i][0]-matri[i-1][0];
int y = matri[i][1]-matri[i-1][1];
if(x == -1 && y == 0)
dir = 0;
if(x == 0 && y == -1)
dir = 1;
if(x == 1 && y == 0)
dir =2 ;
if(x == 0 && y == 1)
dir = 3;
stat <<= 2;
stat = stat |dir;
}
return stat;
} bool judge(int x,int y,int x1,int y1,int state) //是否吃自己
{
int s;
for(int i = 1; i < t; i++)
{
s = 3;
s = state & s; //取最后两位
state = state >> 2;
if(x == x1+dir[s][0] && y == y1+dir[s][1])
return true;
x1 = x1 + dir[s][0];
y1 = y1 + dir[s][1];
}
return false ;
} int get_next(int i,int state) //去掉高位,输入低位
{
int x = 0-dir[i][0];
int y = 0-dir[i][1];
int dir; int k = (1 << ((t - 1) << 1)) - 1;
if(x == -1 && y == 0)
dir = 0;
if(x == 0 && y == -1)
dir = 1;
if(x == 1 && y == 0)
dir =2 ;
if(x == 0 && y == 1)
dir = 3;
state <<= 2;
state |= dir;
state = state & k; //去掉最高位
return state;
} void bfs()
{
queue<node>q;
cur.step = 0;
sta[cur.x][cur.y][cur.state] = 1;
q.push(cur);
node fo;
while(!q.empty())
{
fo = q.front();
q.pop(); for(int i = 0; i < 4; i++)
{
int x = fo.x+dir[i][0];
int y = fo.y+dir[i][1];
int ts = get_next(i,fo.state);
if(x < 1 || x > n || y <1 || y > m || vis[x][y] || judge(x,y,fo.x,fo.y,fo.state) || sta[x][y][ts])
continue;
if(x == 1 && y == 1)
{
printf("%d\n",fo.step+1);
flag = 1;
return;
}
node tmp;
tmp.x =x ;
tmp.y = y;
tmp.step = fo.step + 1;
// for(int i = t; i >= 1; i--)
// {
// tmp.snake[i][0] = tt.snake[i-1][0];
// tmp.snake[i][1] = tt.snake[i-1][1];
// }
tmp.state = ts;
sta[x][y][tmp.state] = 1;
q.push(tmp);
}
}
} int main()
{
int cas= 1;
while(scanf("%d%d%d",&n,&m,&t) != EOF)
{
memset(vis,0,sizeof(vis));
memset(sta,0,sizeof(sta));
if(n == 0 && m == 0 && t == 0)
break;
for(int i = 1; i <= t; i++)
scanf("%d%d",&matri[i][0],&matri[i][1]); int a,b,k;
scanf("%d",&k);
for(int i = 0; i < k; i++)
{
scanf("%d%d",&a,&b);
vis[a][b] = 1;
}
cur.x = matri[1][0];
cur.y = matri[1][1];
cur.state = get_state();
flag = 0;
printf("Case %d: ",cas++);
if(cur.x == 1 && cur.y == 1)
{
printf("0\n");
continue;
}
bfs();
if(!flag)
printf("-1\n");
}
}
POJ 1324(BFS + 状态压缩)的更多相关文章
- 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 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- poj 3311(状态压缩DP)
poj 3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...
- poj 1185(状态压缩DP)
poj 1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...
- poj 3254(状态压缩DP)
poj 3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
随机推荐
- iOS开发UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- ExecutorService实际上是一个线程池的管理工具
在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用 Thread的start()更好.在新特征 ...
- 【iOS】Swift LAZY 修饰符和 LAZY 方法
延时加载或者说延时初始化是很常用的优化方法,在构建和生成新的对象的时候,内存分配会在运行时耗费不少时间,如果有一些对象的属性和内容非常复杂的话,这个时间更是不可忽略.另外,有些情况下我们并不会立即用到 ...
- 在arc模式下 CGImage 释放问题
//大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...
- Web前端性能分析
Web前端性能通常上代表着一个完全意义上的用户响应时间,包含从开始解析HTML文件到最后渲染完成开始的整个过程,但不包括在输入url之后与服务器的交互阶段.下面是整个过程的各个步骤: 开始解析html ...
- 0基础菜鸟学前端之Vue.js
简介:0基础前端菜鸟,啃了将近半月前端VUE框架,对前端知识有了初步的了解.下面总结一下这段时间的学习心得. 文章结构 前端基础 Vue.js简介 Vue.js常用指令 Vue.js组件 Vue.js ...
- VS2013 重装 无法打开项目
今天遇到的奇葩BUG,耗时我一下午,现在跟大家说道说道. 今天重装系统,让各种开发环境开发工具自然要重装一次,最后装完VS2013,然后刚好客户打电话要改点东西,然后我就双击项目准备打开改,然后奇葩来 ...
- redis入门(05)redis的key命令
一.什么是redis键命令 Redis 键(key):Redis 键命令用于管理 redis 的键. Redis 键命令的基本语法: redis 127.0.0.1:6379> COMMAND ...
- Spring Security 入门(1-3-1)Spring Security - http元素 - 默认登录和登录定制
登录表单配置 - http 元素下的 form-login 元素是用来定义表单登录信息的.当我们什么属性都不指定的时候 Spring Security 会为我们生成一个默认的登录页面. 如果不想使用默 ...
- spark2.1:在RDD[unit].foreach(s=>{})内部调用sparkSession对象抛出NullPointException
问题代码: val sample_data_combine_result=List( (0,(List(FitModel(4022,1447.92,-8.38983306721434,2.0),Fit ...