Codeforces-739B:Igor and his way to work(BFS+优化)
来源:互联网 发布:手机蓝牙连接器软件 编辑:程序博客网 时间:2024/05/23 23:47
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
- "." — an empty cell;
- "*" — a cell with road works;
- "S" — the cell where Igor's home is located;
- "T" — the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
5 5..S..****.T....****......
YES
5 5S....****.......****..T..
NO
The first sample is shown on the following picture:
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
思路:BFS,代码中有处大优化。
#include<bits/stdc++.h>using namespace std;char s[2001][2001];int v[2001][2001];int d[4][2]={-1,0,0,-1,1,0,0,1};int n,m;void bfs(int sx,int sy){ memset(v,0,sizeof v); v[sx][sy]=1; queue<int>p; p.push(sx); p.push(sy); for(int i=0;i<4;i++) { int x=sx+d[i][0]; int y=sy+d[i][1]; while(x>=0&&x<n&&y>=0&&y<m&&s[x][y]!='*') { v[x][y]=1; p.push(x); p.push(y); x+=d[i][0]; y+=d[i][1]; } } while(!p.empty()) { sx=p.front();p.pop(); sy=p.front();p.pop(); if(s[sx][sy]=='T'){puts(v[sx][sy]<=3?"YES":"NO");return;} if(v[sx][sy]>=3)continue; for(int i=0;i<4;i++) { int x=sx+d[i][0]; int y=sy+d[i][1]; while(x>=0&&x<n&&y>=0&&y<m&&s[x][y]!='*') { if(v[x][y]==0) { v[x][y]=v[sx][sy]+1; p.push(x); p.push(y); } else if(v[x][y]<v[sx][sy]+1)break; //优化 x+=d[i][0]; y+=d[i][1]; } } } puts("NO");}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++)scanf("%s",s[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++)if(s[i][j]=='S'){bfs(i,j);return 0;} }}
- Codeforces-739B:Igor and his way to work(BFS+优化)
- Tinkoff B. Igor and his way to work(BFS)
- Igor and his way to work-Codeforce 793B BFS
- Codeforces 793B Igor and his way to work 题解
- Codeforces 793B Igor and his way to work dfs
- Codeforces 793B Igor and his way to work (dfs/bfs
- codeforces B. Igor and his way to work dfs or bfs
- codeforces Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- CF793B:B Igor and his way to work(DFS & BFS)
- BFS--cf793b Igor and his way to work
- codeforce 793 B Igor and his way to work
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- 【CodeForces 776B】 Sherlock and his girlfriend (求素数)
- 【codeforces 776B】Sherlock and his girlfriend
- Bailian2796 Bailian3681 数字求和【入门】
- Practical ELK Stack.pdf 英文原版 免费下载
- redhat 的telnet服务开启操作
- poj 1466 Girls and Boys
- 目标跟踪相关资源
- Codeforces-739B:Igor and his way to work(BFS+优化)
- Solr学习—入门
- Kubernetes Management Design Patterns.pdf 英文原版 免费下载
- icon-font图标介绍
- HTTP协议详解
- vs2017设置注释模板
- 无需后台接入?带你玩转VasSonic 2.0里的Local Server
- linux系统修改系统时间与时区
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)