题意:每个点有两种状态,0/1,0表示只能上下方向走,1表示只能左右方向走。每走一步整个图的状态改变一次(即0->1,1->0)。

数据范围:n,m<=1e15

开始迷之因为数组太大编译不过(但是有的人过了就不是很懂orz)。强制状态压缩,将map用vector存储。然后对于每个点奇数次访问用2标记,偶数次访问用4标记。

利用int是8字节的特点,最后一位记录map,前面两位记录访问状态。

若奇数次访问过后,map[i][j] |= 2;若偶数次访问过后,map[i][j] |= 4。

这种状压真的强势。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 100005
using namespace std;
const int change[]={-,};
struct point{
int x,y;
int time;
}sstart;
int n,m;
int x1,x2,y11,y2;
vector<int>map[maxn];
int check(point tmp){
if (tmp.x< || tmp.x>=n || tmp.y< || tmp.y>=m) return ;
else return ;
}
int bfs(){
queue<point> Q;
sstart.x=x1;sstart.y=y11;sstart.time=;map[x1][y11]|=;
Q.push(sstart);
while (!Q.empty()){
point pre=Q.front();
Q.pop();
if (pre.x==x2 && pre.y==y2) return pre.time;
if (pre.time%==){
if (!(map[pre.x][pre.y]&)){ //上下
for (int i=;i<;i++){
point next;
next.x=pre.x+change[i];
next.y=pre.y;
next.time=pre.time+;
if (check(next) && !((int)map[next.x][next.y]&)){
Q.push(next);
map[next.x][next.y]|=;
}
}
}
if (map[pre.x][pre.y]&){ //左右
for (int i=;i<;i++){
point next;
next.x=pre.x;
next.y=pre.y+change[i];
next.time=pre.time+;
if (check(next) && !((int)map[next.x][next.y]&)){
Q.push(next);
map[next.x][next.y]|=;
}
}
}
}
else{
if (!(map[pre.x][pre.y]&)){ //左右
for (int i=;i<;i++){
point next;
next.x=pre.x;
next.y=pre.y+change[i];
next.time=pre.time+;
if (check(next) && !((int)map[next.x][next.y]&)){
Q.push(next);
map[next.x][next.y]|=;
}
}
}
if (map[pre.x][pre.y]&){ //上下
for (int i=;i<;i++){
point next;
next.x=pre.x+change[i];
next.y=pre.y;
next.time=pre.time+;
if (check(next) && !((int)map[next.x][next.y]&)){
Q.push(next);
map[next.x][next.y]|=;
}
}
}
}
}
return -;
}
int main(){
int t;
char xx;
cin >> t;
while (t--){
cin >> n >> m;
for (int i=;i<n;i++) map[i].clear();
for (int i=;i<n;i++){
for (int j=;j<m;j++){
cin >> xx;
map[i].push_back(xx);
}
getchar();
}
cin >> x1 >> y11 >> x2 >> y2;
x1--;y11--;x2--;y2--;
int ans;
ans=bfs();
cout << ans << endl;
}
return ;
}

zoj4020 Traffic Light(bfs+状态压缩)的更多相关文章

  1. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

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

  2. HDU1429+bfs+状态压缩

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

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

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

  4. 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 ...

  5. BFS+状态压缩 HDU1429

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

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

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

  7. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  8. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  9. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

随机推荐

  1. 编译安装bluez5.44

    1.下载 2. configure 提示需要glib 3.yum install glib 4.还是提示glib 5.yum install glib-devel 下载编译glib make inst ...

  2. linq 使用or构建动态查询

    You can certainly do it within a Where clause (extension method). If you need to build a complex que ...

  3. 函数数组demo

    #include <stdio.h> #include <string.h> typedef int(*service_func)(char *,char *); struct ...

  4. jQuery Datepicker 插件遇到问题

    Datepicker ver 1.7.3  浏览更多 常用设置 //禁用今天之前时间 $(".datePicker").datepicker('option', { minDate ...

  5. 2018.10.14 NOIP训练 直线(二分答案+st表+切比雪夫距离转化)

    传送门 二分答案好题. 这已经是当年普及组模拟时挖的坑了233. 这道题还是很不错的. 考虑把坐标系转个45度再操作. 为了不爆精度可以直接转切比雪夫距离. 然后就直接二分答案. 其中竖线就按二分的答 ...

  6. 2018.10.14 NOIP训练 猜数游戏(决策单调性优化dp)

    传送门 一道神奇的dp题. 这题的决策单调性优化跟普通的不同. 首先发现这道题只跟r−lr-lr−l有关. 然后定义状态f[i][j]f[i][j]f[i][j]表示猜范围为[L,L+i−1][L,L ...

  7. 着重基础之—MySql 不能遗忘的索引操作

    着重基础之—MySql 不能遗忘的索引操作 关于MySql索引的基础知识我就不在这里写了,我不太想当信息的搬运工. 技巧分享:Workbench 作为一款专为MySQL设计的ER/数据库建模工具.除了 ...

  8. 教你把p标签的一行字掰弯文字换行): word-wrap: break-word;

    1 使用前的p是这样: 2  使用后: p{ word-wrap: break-word; overflow: hidden; } 3  又想变直: p{ word-wrap: normal; ove ...

  9. UVaLive 6525 Attacking rooks (二分图最大匹配)

    题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击. 析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可. 代码如下: #prag ...

  10. RepositionBars的用法和参数的意义(引用别人的)

    MFC窗口位置管理详细分析及实例 在一般用MFC编写的程序的窗口客户区中,可能有好几个子窗口(具有WM_CHILD风格的窗口).上边是工具栏,中间是视图窗口,下边是状态栏.三个窗 口在框架的客户区里和 ...