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.
Input
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.
Output
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.
Examples
Input
5 5
…S…
****.
T…
****.
…
Output
YES
Input
5 5
S…
.
…
.
…T…
Output
NO
最近几乎一用scanf(“%c") 就错
比如这道题
#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
int n,m,t,vis[maxn][maxn][5][5],flag,si,sj;
char a[maxn][maxn];
void DFS(int x,int y,int c,int t){
if(x<1||y<1||x>n||y>m) return;
if(t>2||a[x][y]=='*'||vis[x][y][c][t]||flag) return;
vis[x][y][c][t]=1;
if(a[x][y]=='T') {flag=1; return;}
if(!c) DFS(x-1,y,c,t),DFS(x+1,y,c,t);
else DFS(x,y-1,c,t),DFS(x,y+1,c,t);
DFS(x,y,!c,t+1);
}
int main()
{
int x,y;
flag=0;
memset(vis,0,sizeof(0));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
x=i,y=j;
}
}
}
DFS(x,y,0,0); DFS(x,y,1,0);
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
如果把输入改成
scanf("%c",&a[i][j]);
就会在第3组数据出错,真是以后少用这句
然后就是流输入加速
加了`
ios::sync_with_stdio(false);
cin.tie(0);
两句后,时间明显缩短
但是在交题时出现了问题,因为这时用了一个scanf
int main()
{
ios::sync_with_stdio(false); //这个函数是一个“是否兼容stdio”的开关,C++为了兼容
//C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。
cin.tie(0);
//在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负
//担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
int x,y;
flag=0;
memset(vis,0,sizeof(0));
scanf("%d%d",&n,&m);
然后就出错了,后来改成cin就ac了。
还有就是不要把 ~ 和 !弄错
!是逻辑取反,只涉及到0和非0(非0并没有统一的数值)
~是按位取反,就是数值写成2进制,然后0改成1,1改成0