zoukankan      html  css  js  c++  java
  • luogu 4554 小明的游戏

    分析

    没事干不要乱建图

    隔壁白银莲花池是因为不能有零环才重新建图的

    这道题,直接01走起就好

    听说是什么01BFS

    0插队首,1插队尾,时间复杂度,可以自己算,也可以造个大数据

    莫名RE,一直以为应该是TLE来着

    代码

      1 /**************************
      2 User:Mandy.H.Y
      3 Language:c++
      4 Problem:luogu4554
      5 Algorithm: 
      6 Date:2019.9.4
      7 **************************/
      8 
      9 #include<bits/stdc++.h>
     10 
     11 using namespace std;
     12 
     13 const int maxn = 505;
     14 const int maxm = 1e6 + 5;
     15 
     16 int n,m;
     17 int dis[maxn][maxn];
     18 char a[maxn][maxn];
     19 bool vis[maxn][maxn];
     20 
     21 struct Node{
     22     int x,y;
     23     Node(int _x = 0,int _y = 0):x(_x),y(_y){}
     24 }q[300005],s,t;
     25 int l,r;
     26 
     27 int dx[5] = {0,0,1,-1};
     28 int dy[5] = {1,-1,0,0};
     29 
     30 template<class T>inline void read(T &x){
     31     x = 0;bool flag = 0;char ch = getchar();
     32     while(!isdigit(ch)) flag |= ch == '-',ch = getchar();
     33     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
     34     if(flag) x = -x;
     35 }
     36 
     37 template<class T>void putch(const T x){
     38     if(x > 9) putch(x / 10);
     39     putchar(x % 10 | 48);
     40 }
     41 
     42 template<class T>void put(const T x){
     43     if(x < 0) putchar('-'),putch(-x);
     44     else putch(x);
     45 }
     46 
     47 void file(){
     48     freopen("4554.in","r",stdin);
     49 //    freopen("4554.out","w",stdout);
     50 }
     51 
     52 void readdata(){
     53     for(int i = 0;i < n; ++ i) scanf("%s",a[i]);
     54     read(s.x);read(s.y);
     55     read(t.x);read(t.y);
     56 }
     57 
     58 void SPFA(){
     59     memset(vis,0,sizeof(vis));
     60     memset(dis,0x3f3f3f3f,sizeof(dis));
     61     l = r = 0;
     62     q[r++] = s;
     63     dis[s.x][s.y] = 0;
     64     while(l != r){
     65         Node u = q[l];
     66         int x = u.x;
     67         int y = u.y;
     68         ++l;
     69         l = l % 260000;
     70         int rr = (r - 1 + 260000) % 260000;
     71         if(l != r && dis[q[l].x][q[l].y] > dis[q[rr].x][q[rr].y]) swap(q[l],q[rr]);
     72         vis[x][y] = 0;
     73         for(int i = 0;i < 4; ++ i){
     74             int nx = x + dx[i];
     75             int ny = y + dy[i];
     76             if(nx >= n || ny >= m || nx < 0 || ny < 0) continue;
     77             int w;
     78             if(a[x][y] == a[nx][ny]) w = 0;
     79             else w = 1;
     80             if(dis[nx][ny] > dis[x][y] + w){
     81                 dis[nx][ny] = dis[x][y] + w;
     82                 if(!vis[nx][ny]){
     83                     q[r++] = Node(nx,ny);
     84                     vis[nx][ny] = 1;
     85                     r %= 260000;
     86                     int rr = (r - 1 + 260000) % 260000;
     87                     if(l != r && dis[q[l].x][q[l].y] > dis[q[rr].x][q[rr].y]) swap(q[l],q[rr]);
     88                 }
     89                 
     90             }
     91         }
     92     }
     93 }
     94 
     95 void work(){
     96     
     97     SPFA();
     98     
     99     put(dis[t.x][t.y]);
    100     putchar('
    ');
    101 }
    102 
    103 int main(){
    104 //    file();
    105     while(1){
    106         scanf("%d%d",&n,&m);
    107         if((!n) && (!m)) break;
    108         readdata();
    109         work();
    110     }
    111     return 0;
    112 }
    View Code
  • 相关阅读:
    深度学习训练模型时保存Log输出信息为文件
    Docker 测试场景领域应用剖析
    kafka性能测试
    [标点符] 初学机器学习算法之LightGBM (直方图+单边梯度采样优化+互斥特征绑定优化)
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
    Linux安装Redis
  • 原文地址:https://www.cnblogs.com/Mandy-H-Y/p/11460055.html
Copyright © 2011-2022 走看看