Codeforces 196B

//题意描述,给你一张最大1500*1500的图,途中的S称为起点,图可以无限延伸,意思是能够将图能够无限平移,最后让你判断,从起点处能否到达无穷远;
//思路,如果一个图能从起点到达无穷远,一定能从图的任意一点到达下一张图与原图对应的点。那么代码就很好写了。
 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 1505;
 7 const int dx[]= {1,-1,0,0};      //方向
 8 const int dy[]= {0,0,-1,1};      //方向
 9 char str[maxn][maxn];
10 int vis[maxn][maxn][2];         //存当前点的实际坐标
11 bool flag[maxn][maxn];          //记录该店是否被访问过
12 int n,m;
13 struct node                            //结构体
14 {
15     int x,y;
16 };
17 bool bfs(node now)
18 {
19     int i;
20     node next,inthismap;
21     memset(flag,false,sizeof(flag));
22     queue<node>que;    
23     que.push(now);           //将起点加入队列
24     while(!que.empty())
25     {
26         now = que.front();
27         que.pop();
28         for(i=0;i<4;i++)
29         {
30             next.x = now.x+dx[i];
31             next.y = now.y+dy[i];
32             inthismap.x = (next.x%n+n)%n;
33             inthismap.y = (next.y%m+m)%m;
34             if(str[inthismap.x][inthismap.y]=='#')
35                 continue;
36             if(flag[inthismap.x][inthismap.y])
37             {
38                 if(next.x!=vis[inthismap.x][inthismap.y][0]||next.y!=vis[inthismap.x][inthismap.y][1])
39                     return true;
40             }
41             else
42             {
43                 flag[inthismap.x][inthismap.y]=true;
44                 vis[inthismap.x][inthismap.y][0]=next.x;
45                 vis[inthismap.x][inthismap.y][1]=next.y;
46                 que.push(next);
47             }
48         }
49     }
50     return false;
51 }
52 int main()
53 {
54 
55     node start;
56     int i,j;
57     while(scanf("%d%d",&n,&m)!=EOF)
58     {
59         for(i=0;i<n;i++)
60         {
61             scanf("%s",str[i]);
62             for(j=0;j<m;j++)
63                 if(str[i][j]=='S')
64                     start.x=i,start.y=j;
65         }
66         if(bfs(start))
67             printf("Yes\n");
68         else
69             printf("No\n");
70     }
71     return 0;
72 }
73 
74    

 

转载于:https://www.cnblogs.com/M-cag/archive/2013/03/26/2982299.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值