CF793B:B Igor and his way to work(DFS & BFS)
来源:互联网 发布:世界历史书 知乎 编辑:程序博客网 时间:2024/06/05 10:07
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:
题意:给一个N*M的地图,问能否至多转2次弯从起点到达终点,不可以穿墙。
思路:dfs或者bfs都可以,数组[i][j][k]表示从k方向到达点(i,j)转弯的最小次数。
bfs
# include <bits/stdc++.h>using namespace std;const int N = 1e3+3;char s[N][N];int x0, y0, x1, y1, n, m, nx[4] = {0, 1, 0, -1}, ny[4] = {-1, 0, 1, 0};int vis[N][N][5];struct node{ int x, y, dir, num; node(int x, int y, int dir, int num):x(x),y(y),dir(dir),num(num){};};void bfs(){ queue<node>q; q.push(node(x0,y0,4,0)); while(!q.empty()) { node u = q.front(); if(u.x == x1 && u.y == y1) return; q.pop(); for(int i=0; i<4; ++i) { int mx = u.x + nx[i]; int my = u.y + ny[i]; if(mx<1||my<1||mx>n||my>m||s[mx][my]=='*'||(i+2)%4 == u.dir) continue; if(u.dir == 4) { q.push(node(mx,my,i,u.num)); vis[mx][my][i] = u.num; } else { if(u.num+(i!=u.dir)>2 || vis[mx][my][i] != -1 && u.num+(i != u.dir) >= vis[mx][my][i]) continue;//>=写成>就超时了。 vis[mx][my][i] = u.num + (i!=u.dir); q.push(node(mx,my,i,u.num+(i != u.dir))); } } }}int main(){ scanf("%d%d",&n,&m); memset(vis, -1, sizeof(vis)); for(int i=1; i<=n; ++i) scanf("%s",s[i]+1); for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) if(s[i][j] == 'S') x0=i, y0=j; else if(s[i][j] == 'T') x1 = i, y1 = j; bfs(); for(int i=0; i<4; ++i) if(vis[x1][y1][i] != -1) return 0*puts("YES"); puts("NO"); return 0;}
dfs
# include <bits/stdc++.h>using namespace std;const int N = 1e3+3;char s[N][N];int x0, y0, x1, y1, n, m, nx[4] = {0, 1, 0, -1}, ny[4] = {-1, 0, 1, 0};int vis[N][N][5];void dfs(int x, int y, int pre, int num){ if(vis[x][y][pre] != -1 && vis[x][y][pre] <= num || num > 2) return; vis[x][y][pre] = num; for(int i=0; i<4; ++i) { int mx = x + nx[i]; int my = y + ny[i]; if(mx<1||my<1||mx>n||my>m||s[mx][my]=='*'||(i+2)%4==pre) continue; if(pre == 4) dfs(mx, my, i, num); else if(pre == i) dfs(mx, my, i, num); else dfs(mx, my, i, num+1); }}int main(){ scanf("%d%d",&n,&m); memset(vis, -1, sizeof(vis)); for(int i=1; i<=n; ++i) scanf("%s",s[i]+1); for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) if(s[i][j] == 'S') x0=i, y0=j; else if(s[i][j] == 'T') x1 = i, y1 = j; dfs(x0, y0, 4, 0); for(int i=0; i<4; ++i) if(vis[x1][y1][i] != -1) return 0*puts("YES"); puts("NO"); return 0;}
- CF793B:B Igor and his way to work(DFS & BFS)
- BFS--cf793b Igor and his way to work
- 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 dfs
- Codeforces 793B Igor and his way to work (dfs/bfs
- codeforces B. Igor and his way to work dfs or bfs
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- Codeforces-739B:Igor and his way to work(BFS+优化)
- codeforce 793 B Igor and his way to work
- Codeforces 793B Igor and his way to work 题解
- codeforces Igor and his way to work
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- 【CodeForces 510B】 Fox And Two Dots (dfs+bfs)
- hdu1242Rescue(BFS and DFS)
- C#动态链接库的创建及使用
- Matlab批量读取文件夹文件
- Unity3d学习笔记 SendMessage
- Struts2 Web 资源获取的四种方式
- 《一》安装 TP5
- CF793B:B Igor and his way to work(DFS & BFS)
- Web基础之Request对象和Response对象项目实战
- 2017年上海金马五校程序设计竞赛(网上资格赛) B Coach(并查集)
- VS错误总结(部分)
- Linux学习笔记-003
- 总结:java中的作用域与单例和多例
- 学长教你搜狗面试怎么过!
- 2799 Simple Life @myLove (eden)
- 实用天气API总结