A - Igor and his way to work

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛阳八中我最棒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值