题意:每个点有两种状态,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. How to Set Up an Rsync Daemon on Your Linux Server

    Introduction This tutorial will take you through setting up an rsync daemon on your Linux server. Yo ...

  2. mybatis 和 mybatis-spring

    一. 1. 实体bean  package com.mybatisBean; public class User { private Integer id; private String name; ...

  3. yii2缓存

    use yii\caching\Cache;$cache = Yii::$app->cache;$cache['var1'] = $value1;  // equivalent to: $cac ...

  4. mysql CONCAT用法

    1.全表查询 SELECT * FROM `wh_statistics_service_api_request`; 由于上面时间是按year,month,day三个数值字段来存时间的,现在想通过时间段 ...

  5. NATAPP打穿内网

    前一篇博文写了ngrok作为内网穿透工具现在开始开始学习另外一种内 网穿透natapp(他也是基于ngrok的一种高速内网穿透服务)多的介绍就 不说了,开始进入正题. 第一步:先登录natapp官网( ...

  6. BSD Socket (java)

    服务器 import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java ...

  7. spring boot docker 初尝试

    Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口. 在centos7.2下,可以进行这 ...

  8. docker 命令介绍

    查看镜像 docker images: 列出imagesdocker images -a :列出所有的images(包含历史)docker images --tree :显示镜像的所有层(layer) ...

  9. Codeforces735B Urbanization 2016-12-13 11:58 114人阅读 评论(0) 收藏

    B. Urbanization time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  10. codeforces 678C. Joty and Chocolate(容斥) 2016-10-15 21:49 122人阅读 评论(0) 收藏

    C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standar ...