ahu-595-撒哈拉大冒险

8 篇文章 0 订阅

坑点:输入不能一个个字符输入,跟hd不一样数据坑
思路 循环 x=(x%n+n)%n+搜索队列
交了我的和硕神的代码,200ms和80ms,只是因为我的是用queue库函数,硕神手撸stack,所以说亲手撸的要节省很多时间。
第一道无限循环的题学会很多。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
struct node{
    int x,y;
};
 
int i,j,k,n,m,t,starx,stary,color[1005][1005],color2[1015][1015][2],bu[4][2] = {1,0,-1,0,0,1,0,-1};
 
char mp[1015][1015];
 
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        bool SYM = false;
        scanf("%d%d",&n,&m);
        memset(color,0,sizeof(color));
        for(i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
           for(j=0; j<m; j++)
           {
               if(mp[i][j]=='S'){
                  starx = i;
                  stary = j;
               }
           }
        }
        // bfs----------------------------------------------------------------
        queue<node>point;
        node node1,node2;
        node1.x = starx;
        node1.y = stary;
        point.push(node1);
        color[starx][stary] = 1;
        color2[starx][stary][0] = starx;
        color2[starx][stary][1] = stary;
        while(!point.empty())
        {
            node1 = point.front();
            point.pop();
            if(SYM == true)break;  
            for(i=0; i<4; i++)
            {
                int X = node1.x+bu[i][0],Y = node1.y+bu[i][1];
                int XX = (X%n+n)%n,YY = (Y%m+m)%m;
                    if(mp[XX][YY] !='#')
                    {
                       if(color[XX][YY]==0)
                       { 
                        node2.x = X;
                        node2.y = Y;
                        color[XX][YY] = 1;
                        point.push(node2);
                        color2[XX][YY][0] = X;
                        color2[XX][YY][1] = Y;
                       }
                       else if(X!=color2[XX][YY][0] || Y!=color2[XX][YY][1])SYM = true;  
                    }       
            } 
        }
        if(SYM == true)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值