题意:给你一条蛇,要求一以最少的步数走到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 + 状态压缩)的更多相关文章

  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. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  3. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  4. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  5. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  7. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  8. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

  9. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

随机推荐

  1. django搭建web (二) urls.py

    URL模式: 在app下的urls.py中 urlpatterns=[ url(正则表达式,view函数,参数,别名,前缀)] urlpatterns=[ url(r'^hello/$',hello. ...

  2. 偶遇vue-awesome-swiper的坑

    最近用vue重构一个移动端的项目,碰到了不少坑,今天拿移动端最著名的轮播插件swiper为例来说,由于这个项目没用UI库,纯手写的样式,沿用老的插件,自然而然的选择了vue-awesome-swipe ...

  3. Column Addition~DP(脑子抽了,当时没有想到)

    Description A multi-digit column addition is a formula on adding two integers written like this:

  4. 第二章 Idea搭建maven

    第二章 Idea搭建maven 1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME(MAVEN_HOME)的环境变量,然后 ...

  5. Jmeter入门(01)Jmeter的下载和安装

    一.什么是Jmeter 1.一款优秀的.开源的.免费的.功能测试和性能测试 工具 Jmeter ,使用Java开发的一款优秀的开源免费测试工具,主要用来做功能测试和性能测试(压力测试/负载测试),用J ...

  6. GridControl的常用操作

    1.GridView的回车跳转单元格和换行 private void gridView1_KeyPress(object sender, KeyPressEventArgs e)        {   ...

  7. express实践(一)

    涉及以下这些内容: 主体. cookie.session 数据 模板引擎 服务器基本结构: const express=require('express'); const static=require ...

  8. HashMap就是这么简单【源码剖析】

    前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合以及散列表.Map集合.红黑树的基础了: Collection总览 List集合就这么简单[源码剖析] Ma ...

  9. Python之函数的进阶(带参数的装饰器)

    函数篇--装饰器二 带参数的装饰器 def outer(flag): def timer(func): def inner(*args,**kwargs): if flag: print('''执行函 ...

  10. Ubuntu下安装最新sublime

    1. Install the GPG key: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key ...