codeforces Igor and his way to work

来源:互联网 发布:软件工程项目总监职责 编辑:程序博客网 时间:2024/05/29 23:24

B. Igor and his way to work
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

1000*1000的数据 只允许你转两个弯,问你能不能从起点到终点。

一开始用的广搜。。。太久没写居然wa了。。写对了开始t。。。1000*1000还是太大了。。。。。只能先处理起点,再处理终点,再一个个处理。。。比光搜快很多。。因为这个是on复杂度。。 广搜不行

#include <bits/stdc++.h>using namespace std;char mp[1010][1010];int si,sj,ei,ej;int f1[1010][1010];int f2[1010][1010];    int n,m;void qu(int x,int y,int (&vis)[1010][1010]){    for(int i=0;i+x<n;i++)    {        if(mp[i+x][y]=='*')            break;        vis[i+x][y]=1;    }    for(int i=0;x-i>=0;i++)    {        if(mp[x-i][y]=='*')            break;        vis[x-i][y]=1;    }    for(int i=0;i+y<m;i++)    {        if(mp[x][i+y]=='*')            break;        vis[x][i+y]=1;    }    for(int i=0;y-i>=0;i++)    {        if(mp[x][y-i]=='*') break;         vis[x][y-i]=1;    }}int ch(int x,int y){    for(int i=0;i+x<n;i++)    {        if(mp[i+x][y]=='*')            break;        if(f2[i+x][y]==1) return 1;    }    for(int i=0;x-i>=0;i++)    {        if(mp[x-i][y]=='*')            break;        if(f2[x-i][y]==1) return 1;    }    for(int i=0;i+y<m;i++)    {        if(mp[x][i+y]=='*')            break;        if(f2[x][i+y]==1) return 1;    }    for(int i=0;y-i>=0;i++)    {        if(mp[x][y-i]=='*') break;         if(f2[x][y-i]==1) return 1;    }    return 0;}int main(){    scanf("%d%d",&n,&m);    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            scanf(" %c",&mp[i][j]);            if(mp[i][j]=='S')                si=i,sj=j;            if(mp[i][j]=='T')                ei=i,ej=j;        }    }    qu(si,sj,f1);    qu(ei,ej,f2);    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(f1[i][j])            {                if(ch(i,j))                {                    printf("YES\n");                    return 0;                }            }        }    }    printf("NO\n");    return 0;}

更新 广搜是可以的,只是比我写得要暴力很多。我的一个个方向走完比较慢。
贴代码

#include <iostream>#include <bits/stdc++.h>#include <queue>#define sf(i) scanf("%d",&i)#define MAXN 1002#define ii pair<int,int>#define iii pair<ii,int>;#define pii <ii,ii >using namespace std;bool vis[MAXN][MAXN][5][3];struct node{    ii pos;    int in_dir;    int turns;};int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};bool solve(){   char M[MAXN][MAXN];   queue<node> Q;   ii start,goal;   int n,m;   //sf(n);sf(m);   cin>>n>>m;   for(int i=0;i<n;i++)       for(int j=0;j<m;j++){           cin>>M[i][j];           if(M[i][j]=='S')               start=make_pair(i,j);           if(M[i][j]=='T')               goal=make_pair(i,j);       }   node s;   s.pos=start;   s.in_dir=4;   s.turns=0;   Q.push(s);   vis[start.first][start.second][4][0]=true;   while(!Q.empty()){       node u=Q.front();       Q.pop();       if(u.pos==goal)           return true;       for(int k=0;k<4;k++){           int ni=u.pos.first+dir[k][0];           int nj=u.pos.second+dir[k][1];           if(ni>=0 && ni<n && nj>=0 && nj<m){               node nu=u;               nu.in_dir=k;               nu.pos=make_pair(ni,nj);               nu.turns=((u.in_dir!=k)&&(u.in_dir<4))?u.turns+1:u.turns;               if(nu.turns<=2 && !vis[ni][nj][k][nu.turns] && M[ni][nj]!='*'){                   vis[ni][nj][k][nu.turns]=true;                   Q.push(nu);               }           }       }   }   return false;}int main(int argc, char *argv[]){    if(solve())        cout<<"YES\n";    else        cout<<"NO\n";    return 0;}
0 0
原创粉丝点击