题意:给你一条蛇,要求一以最少的步数走到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. Flask 学习 十四 测试

    获取代码覆盖报告 安装代码覆盖工具 pip install coverage manage.py 覆盖检测 COV = None if os.environ.get('FLASK_COVERAGE') ...

  2. 常用的 html 标签及注意事项

    <a> 标签 用法:用于定义超链接 清除浏览器默认样式: a { text-decoration: none;/* 去除下划线 */ color: #333;/* 改变链接颜色 */ } ...

  3. JAVA_SE基础——16.方法

    接触过C语言的同学,这小章节很容易接受.Java中的方法是类似与C语言中的函数  功能和调用方法都类似  只不过叫法不一样  因为java是面向对象  c是面向过程    仅仅是叫法不同.. . 看到 ...

  4. Linux的rsync 配置,用于服务器之间远程传大量的数据

    [教程主题]:rsync [课程录制]: 创E [主要内容] [1] rsync介绍 Rsync(Remote Synchronize) 是一个远程资料同步工具,可通过LAN/WAN快速同步多台主机, ...

  5. HashMap 的底层原理

    1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...

  6. ibatis的优缺点及可行性分析

    1.优点 简单: 易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现. 实用: 提供了数据映射功能,提供了对底层数据访问的封装(例如ado.net),提供了DAO框架,可以使我 ...

  7. java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...

  8. priority queue优先队列初次使用

    题目,排队打印问题 Input Format One line with a positive integer: the number of test cases (at most 20). Then ...

  9. POJ-3026 Borg Maze---BFS预处理+最小生成树

    题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...

  10. tagName与nodeName的区别

    首先介绍DOM里常见的三种节点类型(总共有12种,如docment):元素节点,属性节点以及文本节点,例如<h2 class="title">head</h2&g ...