codeforces 793B
来源:互联网 发布:淘宝直通车创意主图 编辑:程序博客网 时间:2024/06/03 03:20
传送
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
真菜啊,最近连个bfs都不会写了。
这个就是bfs从起点看能不能到达终点。只不过限制条件有点多。
1.其中一个状态就是当前点还能转几次弯。最开始的自然是能转两次。
2.一个状态就是当前点的方向。
所以vis标记数组出了横纵坐标外,应该还有上面说的那两维。
确定好这个之后就是一个简单的bfs可到达问题了。
真菜啊,最近连个bfs都不会写了。
这个就是bfs从起点看能不能到达终点。只不过限制条件有点多。
1.其中一个状态就是当前点还能转几次弯。最开始的自然是能转两次。
2.一个状态就是当前点的方向。
所以vis标记数组出了横纵坐标外,应该还有上面说的那两维。
确定好这个之后就是一个简单的bfs可到达问题了。
#include <bits/stdc++.h> using namespace std; const int MAXN=1e3+7; const int inf=1e9; int n,m,k; int sx,sy,ex,ey; char tu[MAXN][MAXN]; int dx[4]= {0,0,1,-1}; int dy[4]= {-1,1,0,0}; bool vis[MAXN][MAXN][4][2]; struct node { int x,y,d,sum;//横坐标,纵坐标,当前的方向,剩余的可转弯次数 node(int x1,int y1,int d1,int sum1) { x=x1; y=y1; d=d1; sum=sum1; } }; int bfs() { queue<node>q; for(int i=0; i<4; ++i)//先把相邻的满足条件的4个点加进来 { int x=sx+dx[i]; int y=sy+dy[i]; if(x>=0&&x<n&&y>=0&&y<m&&tu[x][y]!='*') { vis[x][y][i][2]=1; q.push(node(x,y,i,2)); } } while(!q.empty()) { node u=q.front(); q.pop(); if(u.x==ex&&u.y==ey)return 1;//表示可以到达 for(int i=0; i<4; ++i) { int x=u.x+dx[i]; int y=u.y+dy[i]; if(x>=0&&x<n&&y>=0&&y<m&&tu[x][y]!='*') { if(i==u.d)//如果方向相同 { if(!vis[x][y][i][u.sum]) { vis[x][y][i][u.sum]=1; q.push(node(x,y,i,u.sum)); } } else if(u.sum>0) { if(!vis[x][y][i][u.sum-1]) { vis[x][y][i][u.sum-1]=1; q.push(node(x,y,i,u.sum-1)); } } } } } return 0; } int main() { scanf("%d%d",&n,&m); for(int i=0; i<n; ++i) { scanf("%s",tu[i]); for(int j=0; j<m; ++j) { if(tu[i][j]=='S') { sx=i; sy=j; } else if(tu[i][j]=='T') { ex=i; ey=j; } } } if(bfs())puts("YES"); else puts("NO"); return 0; }
- codeforces 793B
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Codeforces 166B - Polygons
- codeforces B. Coins
- codeforces----193B Xor
- codeforces----208B Solitaire
- Codeforces 1B - Spreadsheet
- codeforces 214B Hometask
- Codeforces Round #136 B
- CodeForces 224B Array
- Oracle数据库性能调优第一节
- 简单的瀑布流的实现
- STL中查找方法用法总结-adjacent_find, binary_search, includes, lower_bound, upper_bound, equal_range
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- Ambari数据库分析-配置相关
- codeforces 793B
- 用一台笔记本跑ROVIO
- python-大智慧-BIAS乖离率编写
- 由传统武术和综合格斗所想到的
- SQL notes
- HAproxy 配置详解
- JSTL日期格式化
- orcale插入数据 返回插入数据的主键
- Activity中多Fragment使用方案1