zoj4020 Traffic Light(bfs+状态压缩)

news/2024/7/8 11:02:36

题意:每个点有两种状态,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。

这种状压真的强势。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<vector>
  7 #define maxn 100005
  8 using namespace std;
  9 const int change[2]={-1,1};
 10 struct point{
 11     int x,y;
 12     int time;
 13 }sstart;
 14 int n,m;
 15 int x1,x2,y11,y2;
 16 vector<int>map[maxn];
 17 int check(point tmp){
 18     if (tmp.x<0 || tmp.x>=n || tmp.y<0 || tmp.y>=m) return 0;
 19     else return 1;
 20 }
 21 int bfs(){
 22     queue<point> Q;
 23     sstart.x=x1;sstart.y=y11;sstart.time=0;map[x1][y11]|=4;
 24     Q.push(sstart);
 25     while (!Q.empty()){
 26         point pre=Q.front();
 27         Q.pop();
 28         if (pre.x==x2 && pre.y==y2) return pre.time;
 29         if (pre.time%2==0){
 30             if (!(map[pre.x][pre.y]&1)){  //上下 
 31                 for (int i=0;i<2;i++){
 32                     point next;
 33                     next.x=pre.x+change[i];
 34                     next.y=pre.y;
 35                     next.time=pre.time+1;
 36                     if (check(next) && !((int)map[next.x][next.y]&4)){
 37                         Q.push(next);
 38                         map[next.x][next.y]|=4;
 39                     }
 40                 }
 41             }
 42             if (map[pre.x][pre.y]&1){  //左右 
 43                 for (int i=0;i<2;i++){
 44                     point next;
 45                     next.x=pre.x;
 46                     next.y=pre.y+change[i];
 47                     next.time=pre.time+1;
 48                     if (check(next) && !((int)map[next.x][next.y]&4)){
 49                         Q.push(next);
 50                         map[next.x][next.y]|=4;
 51                     }
 52                 }
 53             }
 54         }
 55         else{
 56             if (!(map[pre.x][pre.y]&1)){  //左右 
 57                 for (int i=0;i<2;i++){
 58                     point next;
 59                     next.x=pre.x;
 60                     next.y=pre.y+change[i];
 61                     next.time=pre.time+1;
 62                     if (check(next) && !((int)map[next.x][next.y]&2)){
 63                         Q.push(next);
 64                         map[next.x][next.y]|=2;
 65                     }
 66                 }
 67             }
 68             if (map[pre.x][pre.y]&1){  //上下 
 69                 for (int i=0;i<2;i++){
 70                     point next;
 71                     next.x=pre.x+change[i];
 72                     next.y=pre.y;
 73                     next.time=pre.time+1;
 74                     if (check(next) && !((int)map[next.x][next.y]&2)){
 75                         Q.push(next);
 76                         map[next.x][next.y]|=2;
 77                     }
 78                 }
 79             }
 80         }
 81     } 
 82     return -1;
 83 }
 84 int main(){
 85     int t;
 86     char xx;
 87     cin >> t;
 88     while (t--){
 89         cin >> n >> m;
 90         for (int i=0;i<n;i++) map[i].clear();
 91         for (int i=0;i<n;i++){
 92             for (int j=0;j<m;j++){
 93                 cin >> xx;
 94                 map[i].push_back(xx);
 95             }
 96             getchar();
 97         }
 98         cin >> x1 >> y11 >> x2 >> y2;
 99         x1--;y11--;x2--;y2--;
100         int ans;
101         ans=bfs();
102         cout << ans << endl;
103     }
104     return 0;
105 }

转载于:https://www.cnblogs.com/changer-qyz/p/8757212.html


http://www.niftyadmin.cn/n/4287408.html

相关文章

捕鸟人和鹳

捕鸟人布下捕鹤的网&#xff0c;藏在远处等飞来的猎物. 一只鹳鸟和几只鹤一同飞进了网里&#xff0c;捕鸟人马上跑过来&#xff0c;把他们全都捉住了. 鹳鸟请求把他放了&#xff0c;说他对人只有益而无害&#xff0c;他能捕杀蛇和别的害虫. 捕鸟人回答&#xff1a;“即使你不算…

捕鸟人和斑鸠

有个客人很晚才来到捕鸟人家&#xff0c;但捕鸟人没有食物招待客人&#xff0c;便跑去捉了那只自家养的斑鸠&#xff0c;想要杀了它款待客人.斑鸠痛斥他忘恩负义&#xff0c;说自己曾帮他招来了许多同自己一样的斑鸠&#xff0c;使他得到很大的好处&#xff0c;现在却要被杀掉.…

母鸡与燕子

一次母鸡发现了一个蛇蛋&#xff0c;小心翼翼地孵化它&#xff0c;细心地给它啄开蛋壳. 燕子看见后&#xff0c;说&#xff1a;“你这傻瓜&#xff0c;你为什么要孵化这个坏蛋呢&#xff1f;它一长大首先就会伤害你.”

老马

一匹老马被别人卖了去拉磨. 当他被框上轭时&#xff0c;悲痛地说&#xff1a;“我从跑马场冲到了如此一个终点.”

Excel 导出通用类

public class ExportToExcelHelper{public static void ExportExcel(DataTable dt){try{//创建一个工作簿IWorkbook workbook new HSSFWorkbook();//创建一个 sheet 表ISheet sheet workbook.CreateSheet("数据");//创建一行IRow rowH sheet.CreateRow(0);//创建一…

马、牛、狗与人

宙斯创造了人&#xff0c;虽没给人长寿&#xff0c;却给了人聪明智慧. 在冬天&#xff0c;人给自己建造好了房屋&#xff0c;舒服地住在里面.有一天&#xff0c;天气异常冷&#xff0c;还下着雨&#xff0c;马冻得再也忍受不了&#xff0c;便跑到人那里&#xff0c;恳求让它住在…

马 与 兵

战争期间&#xff0c;一个士兵用大麦细心地饲养他的马. 然而战争一结束&#xff0c;那马便被拉去服苦役&#xff0c;搬运很重新点的货物. 后来战火重燃&#xff0c;军号吹响了&#xff0c;主人预备好马鞍&#xff0c;全副武装骑着马去战斗. 这时&#xff0c;马却奄奄一息&#…