zoukankan      html  css  js  c++  java
  • P1363-幻象迷宫

     1 #include <bits/stdc++.h>
     2 #define _for(i,a,b) for(int i = (a);i < b;i ++)
     3 typedef long long ll;
     4 using namespace std;
     5 struct point
     6 {
     7     int x;
     8     int y;
     9     bool operator == (point b)
    10     {
    11         return this->x==b.x && this->y==b.y;
    12     }
    13 };
    14 typedef pair<point,point> pir;
    15 
    16 inline ll read()
    17 {
    18     ll ans = 0;
    19     char ch = getchar(), last = ' ';
    20     while(!isdigit(ch)) last = ch, ch = getchar();
    21     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    22     if(last == '-') ans = -ans;
    23     return ans;
    24 }
    25 inline void write(ll x)
    26 {
    27     if(x < 0) x = -x, putchar('-');
    28     if(x >= 10) write(x / 10);
    29     putchar(x % 10 + '0');
    30 }
    31 
    32 int N,M;
    33 char m[1504][1504];
    34 bool vis[1504][1504];
    35 point pre[1504][1504];
    36 
    37 point st;
    38 int dx[] = {1,-1,0,0};
    39 int dy[] = {0,0,1,-1};
    40 bool bfs()
    41 {
    42     queue<pir> q;
    43     q.push({st,st});
    44     vis[st.x][st.y] = 1;
    45     pre[st.x][st.y] = st;
    46     while(!q.empty())
    47     {
    48         pir cur = q.front();q.pop();
    49         point a = cur.first;
    50         point b = cur.second;
    51 
    52         _for(i,0,4)
    53         {
    54             point ta;
    55             ta.x = (a.x+dx[i]+N)%N;
    56             ta.y = (a.y+dy[i]+M)%M;
    57             point tb;
    58             tb.x = b.x+dx[i];
    59             tb.y = b.y+dy[i];
    60 
    61             if(vis[ta.x][ta.y] && !(tb==pre[ta.x][ta.y]))
    62                 return true;
    63 
    64             if(vis[ta.x][ta.y] || m[ta.x][ta.y]=='#')
    65                 continue;
    66 
    67             vis[ta.x][ta.y] = 1,pre[ta.x][ta.y] = tb;
    68             q.push({ta,tb});
    69         }
    70     }
    71     return false;
    72 }
    73 int main()
    74 {
    75     while(scanf("%d%d",&N,&M)!=EOF)
    76     {
    77         memset(m,0,sizeof(m));
    78         memset(vis,0,sizeof(vis));
    79         memset(pre,0,sizeof(pre));
    80         _for(i,0,N)
    81         _for(j,0,M)
    82         {
    83             cin >> m[i][j];
    84             if(m[i][j]=='S')
    85                 st.x = i,st.y = j,m[i][j] = '.';
    86         }
    87 
    88         if(bfs())
    89             printf("Yes
    ");
    90         else
    91             printf("No
    ");
    92     }
    93     return 0;
    94 }
    https://www.luogu.org/blog/Asurudo/solution-p1363
  • 相关阅读:
    html 按钮跳转问题(及其相关)
    用JS写的一个简单的时钟
    JS里面function和Function的区别
    Sharepoint常见概念
    整理个人学习方向,技术列表,通过这个来明确方向
    关于SqlServer的内连接,外链接以及left join,right join之间的一些问题与区别。
    display:none与visibility:hidden的区别
    给自己立下一个flag先
    linux 打包和压缩的概念和区别
    Hadoop和Spark的比较区别
  • 原文地址:https://www.cnblogs.com/Asurudo/p/11366684.html
Copyright © 2011-2022 走看看